Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize string concatenation #10

Merged
merged 4 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Optimize join()
  • Loading branch information
letmutex committed Jun 24, 2024
commit 7e701fe7a6f129b1648a3230a20eef45b4e2eb66
9 changes: 6 additions & 3 deletions benches/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ Memory: 15.9 GB
# Result

```
convert() time: [89.826 ms 90.520 ms 91.225 ms]
change: [-0.0736% +1.0704% +2.2298%] (p = 0.06 > 0.05)
convert() time: [88.528 ms 89.545 ms 90.670 ms]
change: [-0.2785% +1.0525% +2.5466%] (p = 0.15 > 0.05)
No change in performance detected.
Found 5 outliers among 100 measurements (5.00%)
4 (4.00%) high mild
1 (1.00%) high severe
```

*Updated at Mon, 24 Jun 2024 04:03:55 GMT*
*Updated at Mon, 24 Jun 2024 07:51:50 GMT*

*Generated by [bench.ts](bench.ts)*
3 changes: 1 addition & 2 deletions src/element_handler/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use markup5ever_rcdom::Node;

use crate::{
options::{LinkReferenceStyle, LinkStyle, Options},
text_util::{concat_strings, StripWhitespace, TrimAsciiWhitespace},
text_util::{concat_strings, JoinOnStringIterator, StripWhitespace, TrimAsciiWhitespace},
};

use super::ElementHandler;
Expand Down Expand Up @@ -58,7 +58,6 @@ impl ElementHandler for AnchorElementHandler {
text.lines()
.map(|line| line.trim_ascii_whitespace().replace("\"", "\\\""))
.filter(|line| !line.is_empty())
.collect::<Vec<String>>()
.join("\n")
};

Expand Down
3 changes: 1 addition & 2 deletions src/element_handler/blockquote.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
text_util::{concat_strings, TrimAsciiWhitespace},
text_util::{concat_strings, JoinOnStringIterator, TrimAsciiWhitespace},
Element,
};

Expand All @@ -9,7 +9,6 @@ pub(super) fn blockquote_handler(element: Element) -> Option<String> {
.trim_end_ascii_whitespace()
.lines()
.map(|line| concat_strings!("> ", line))
.collect::<Vec<String>>()
.join("\n");
Some(concat_strings!("\n\n", content, "\n\n"))
}
5 changes: 2 additions & 3 deletions src/element_handler/code.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
node_util::get_parent_node_tag_name,
options::{CodeBlockFence, CodeBlockStyle},
text_util::{concat_strings, TrimAsciiWhitespace},
text_util::{concat_strings, JoinOnStringIterator, TrimAsciiWhitespace},
Element,
};

Expand Down Expand Up @@ -32,7 +32,7 @@ fn handle_code_block(element: Element) -> Option<String> {
.to_string()
.split(" ")
.find(|cls| cls.starts_with("language-"))
.map(|lang| lang.split("-").skip(1).collect::<Vec<&str>>().join("-"))
.map(|lang| lang.split("-").skip(1).join("-"))
})
.unwrap_or(None);
let mut result = String::from(&fence);
Expand All @@ -48,7 +48,6 @@ fn handle_code_block(element: Element) -> Option<String> {
let code = content
.lines()
.map(|line| concat_strings!(" ", line))
.collect::<Vec<String>>()
.join("\n");
Some(code)
}
Expand Down
3 changes: 1 addition & 2 deletions src/element_handler/img.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
text_util::{concat_strings, TrimAsciiWhitespace},
text_util::{concat_strings, JoinOnStringIterator, TrimAsciiWhitespace},
Element,
};

Expand Down Expand Up @@ -28,7 +28,6 @@ pub(super) fn img_handler(element: Element) -> Option<String> {
text.lines()
.map(|line| line.trim_ascii_whitespace().replace("\"", "\\\""))
.filter(|line| !line.is_empty())
.collect::<Vec<String>>()
.join("\n")
};

Expand Down
23 changes: 23 additions & 0 deletions src/text_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,29 @@ where
}
}

pub(crate) trait JoinOnStringIterator {
fn join<S: AsRef<str>>(&mut self, separator: S) -> String;
}

impl<T, S> JoinOnStringIterator for T
where
S: AsRef<str>,
T: Iterator<Item = S>,
{
fn join<SE: AsRef<str>>(&mut self, separator: SE) -> String {
let Some(first) = self.next() else {
return String::new();
};
let separator = separator.as_ref();
let mut result = String::from(first.as_ref());
for next in self {
result.push_str(separator);
result.push_str(next.as_ref());
}
result
}
}

pub(crate) fn compress_whitespace(input: &str) -> String {
let mut result = String::new();
if input.len() == 0 {
Expand Down