Skip to content

Commit d1aca74

Browse files
committed
perf: Use memchr for writing declaration values
Signed-off-by: Dmitry Dygalo <dmitry@dygalo.dev>
1 parent e44132e commit d1aca74

File tree

9 files changed

+37
-15
lines changed

9 files changed

+37
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Performance
6+
7+
- Faster HTML serialization for styles containing double quotes.
8+
59
## [0.19.1] - 2026-01-23
610

711
### Changed

bindings/c/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Performance
6+
7+
- Faster HTML serialization for styles containing double quotes.
8+
59
## [0.19.1] - 2026-01-23
610

711
### Changed

bindings/java/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Performance
6+
7+
- Faster HTML serialization for styles containing double quotes.
8+
59
## [0.19.1] - 2026-01-23
610

711
### Changed

bindings/javascript/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Performance
6+
7+
- Faster HTML serialization for styles containing double quotes.
8+
59
## [0.19.2] - 2026-01-23
610

711
### Changed

bindings/php/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Performance
6+
7+
- Faster HTML serialization for styles containing double quotes.
8+
59
## [0.19.1] - 2026-01-23
610

711
### Changed

bindings/python/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Performance
6+
7+
- Faster HTML serialization for styles containing double quotes.
8+
59
## [0.19.1] - 2026-01-23
610

711
### Changed

bindings/ruby/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Performance
6+
7+
- Faster HTML serialization for styles containing double quotes.
8+
59
## [0.19.1] - 2026-01-23
610

711
### Changed

css-inline/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ stylesheet-cache = ["lru"]
3232
cssparser = "0.36.0"
3333
html5ever = "0.38.0"
3434
lru = { version = "0.16.0", optional = true }
35+
memchr = "2.7"
3536
precomputed-hash = "0.1.1"
3637
rayon = { version = "1.10", optional = true }
3738
reqwest = { version = "0.12.0", optional = true, default-features = false, features = ["rustls-tls", "blocking"] }

css-inline/src/html/serializer.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use super::{
66
};
77
use crate::{html::ElementStyleMap, parser, InlineError};
88
use html5ever::{local_name, ns, tendril::StrTendril, LocalName, QualName};
9+
use memchr::memchr_iter;
910
use smallvec::{smallvec, SmallVec};
1011
use std::io::Write;
1112

@@ -459,26 +460,18 @@ fn write_declaration<Wr: Write>(
459460
}
460461

461462
#[inline]
463+
#[allow(clippy::arithmetic_side_effects)]
462464
fn write_declaration_value<Wr: Write>(writer: &mut Wr, value: &str) -> Result<(), InlineError> {
463465
let value = value.trim();
464-
// Roughly based on `str::replace`
466+
let bytes = value.as_bytes();
467+
465468
let mut last_end = 0;
466-
for (start, part) in value.match_indices('"') {
467-
writer.write_all(
468-
value
469-
.get(last_end..start)
470-
.expect("Invalid substring")
471-
.as_bytes(),
472-
)?;
469+
for idx in memchr_iter(b'"', bytes) {
470+
writer.write_all(&bytes[last_end..idx])?;
473471
writer.write_all(b"'")?;
474-
last_end = start.checked_add(part.len()).expect("Size overflow");
472+
last_end = idx + 1;
475473
}
476-
writer.write_all(
477-
value
478-
.get(last_end..value.len())
479-
.expect("Invalid substring")
480-
.as_bytes(),
481-
)?;
474+
writer.write_all(&bytes[last_end..])?;
482475
Ok(())
483476
}
484477

0 commit comments

Comments
 (0)