Skip to content

perf: Slightly improve performance of HTML serialization #431

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

Merged
merged 1 commit into from
Jun 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Performance

- Slightly improve performance of HTML serialization.

## [0.14.4] - 2024-12-27

### Changed
Expand Down
14 changes: 8 additions & 6 deletions css-inline/src/html/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,14 @@
.expect("Invalid substring")
.as_bytes(),
)?;
match part {
"&" => self.writer.write_all(b"&")?,
"\u{00A0}" => self.writer.write_all(b" ")?,
"<" => self.writer.write_all(b"&lt;")?,
">" => self.writer.write_all(b"&gt;")?,
_ => unreachable!("Only the variants above are searched"),
// This is slightly faster than matching on `char`
// Notably, this approach does not work in `write_attributes` below
match (part.as_bytes()[0] & 0b0000_1110) >> 1 {
1 => self.writer.write_all(b"&nbsp;")?,
3 => self.writer.write_all(b"&amp;")?,
6 => self.writer.write_all(b"&lt;")?,
7 => self.writer.write_all(b"&gt;")?,
_ => unreachable!(),

Check warning on line 181 in css-inline/src/html/serializer.rs

View check run for this annotation

Codecov / codecov/patch

css-inline/src/html/serializer.rs#L181

Added line #L181 was not covered by tests
}
last_end = start.checked_add(part.len()).expect("Size overflow");
}
Expand Down
Loading