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

perf(css/codegen): Reduce allocations #6599

Merged
merged 7 commits into from Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions crates/swc_css_codegen/src/lib.rs
Expand Up @@ -2,7 +2,7 @@
#![allow(clippy::needless_update)]

pub use std::fmt::Result;
use std::{str, str::from_utf8};
use std::{borrow::Cow, str, str::from_utf8};

use serde::{Deserialize, Serialize};
use swc_atoms::*;
Expand Down Expand Up @@ -1565,9 +1565,9 @@ where
fn emit_ident(&mut self, n: &Ident) -> Result {
if self.config.minify {
let value = if self.ctx.allow_to_lowercase && self.config.minify {
n.value.to_ascii_lowercase().to_string()
Cow::Owned(n.value.to_ascii_lowercase())
} else {
n.value.to_string()
Cow::Borrowed(&n.value)
};
let serialized = serialize_ident(&value, n.raw.as_deref(), true);

Expand Down
53 changes: 49 additions & 4 deletions crates/swc_css_utils/src/lib.rs
@@ -1,6 +1,6 @@
#![deny(clippy::all)]

use std::{char::REPLACEMENT_CHARACTER, str};
use std::{borrow::Cow, char::REPLACEMENT_CHARACTER, str};

use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -153,8 +153,53 @@ pub static NAMED_COLORS: Lazy<AHashMap<JsWord, NamedColor>> = Lazy::new(|| {
named_colors
});

#[inline]
fn is_escape_not_required(value: &str, raw: Option<&str>) -> bool {
if value.is_empty() {
return true;
}

if (b'0'..=b'9').contains(&value.as_bytes()[0]) {
return false;
}

if value.len() == 1 && value.as_bytes()[0] == b'-' {
return false;
}

if value.len() >= 2
&& value.as_bytes()[0] == b'-'
&& (b'0'..=b'9').contains(&value.as_bytes()[1])
{
return false;
}

value.chars().all(|c| {
match c {
REPLACEMENT_CHARACTER if raw.is_some() => false,
'\x00' => false,
'\x01'..='\x1f' | '\x7F' => false,
'-' | '_' => true,
_ if !c.is_ascii()
|| c.is_ascii_digit()
|| c.is_ascii_uppercase()
|| c.is_ascii_lowercase() =>
{
true
}
// Otherwise, the escaped character.
_ => false,
}
})
}

// https://drafts.csswg.org/cssom/#serialize-an-identifier
pub fn serialize_ident(value: &str, raw: Option<&str>, minify: bool) -> String {
pub fn serialize_ident<'a>(value: &'a str, raw: Option<&str>, minify: bool) -> Cow<'a, str> {
// Fast-path
if is_escape_not_required(value, raw) {
return Cow::Borrowed(value);
}

let mut result = String::with_capacity(value.len());

//
Expand All @@ -175,7 +220,7 @@ pub fn serialize_ident(value: &str, raw: Option<&str>, minify: bool) -> String {
REPLACEMENT_CHARACTER if raw.is_some() => {
result.push_str(raw.unwrap());

return result;
return Cow::Owned(result);
}
// If the character is NULL (U+0000), then the REPLACEMENT CHARACTER (U+FFFD).
'\x00' => {
Expand Down Expand Up @@ -225,7 +270,7 @@ pub fn serialize_ident(value: &str, raw: Option<&str>, minify: bool) -> String {
}
}

result
Cow::Owned(result)
}

// https://github.com/servo/rust-cssparser/blob/4c5d065798ea1be649412532bde481dbd404f44a/src/serializer.rs#L166
Expand Down