Skip to content

Avoid std::fmt in serialization code #190

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 4 commits into from
Aug 30, 2017
Merged

Avoid std::fmt in serialization code #190

merged 4 commits into from
Aug 30, 2017

Conversation

SimonSapin
Copy link
Member

@SimonSapin SimonSapin commented Aug 29, 2017

This change is Reviewable

Copy link
Member

@emilio emilio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=me, with comments addressed.

Thanks!

@@ -90,7 +75,7 @@ impl<'a> ToCss for Token<'a> {
serialize_unquoted_url(&**value, dest)?;
dest.write_str(")")?;
},
Token::Delim(value) => write!(dest, "{}", value)?,
Token::Delim(value) => dest.write_str(value.encode_utf8(&mut [0_u8; 4]))?,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't this use write_char?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why I didn’t think of that… Done :)

impl<W: fmt::Write> io::Write for AssumeUtf8<W> {
#[inline]
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
// Safety: itoa only emits ASCII
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we assert it?

debug_assert!(buf.iter().all(|b| b.is_ascii()));

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. (AsciiExt is also implemented on [u8], no need to iterate ourselves.)

b3 = [b'\\', to_hex_byte(ascii_byte), b' '];
&b3[..]
};
dest.write_str(unsafe { str::from_utf8_unchecked(&bytes) })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could use write_char, I think, but not sure if worth it, this would be faster and it's trivially ascii so...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When writing more than one byte/char it might be better to minimize the number of calls, but I haven’t measured.

\\1 \\2 \\3 \\4 \\5 \\6 \\7 \\8 \\9 \\A \\B \\C \\D \\E \\F \\10 \
\\11 \\12 \\13 \\14 \\15 \\16 \\17 \\18 \\19 \\1A \\1B \\1C \\1D \\1E \\1F \\20 \
\\1 \\2 \\3 \\4 \\5 \\6 \\7 \\8 \\9 \\a \\b \\c \\d \\e \\f \\10 \
\\11 \\12 \\13 \\14 \\15 \\16 \\17 \\18 \\19 \\1a \\1b \\1c \\1d \\1e \\1f \\20 \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe run this through try to ensure this doesn't break anything? (seems unlikely)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ll do a Gecko Try run before landing in Servo.

… since it involves costly dynamic dispatch.
@SimonSapin
Copy link
Member Author

@bors-servo r=emilio

@bors-servo
Copy link
Contributor

📌 Commit c42f384 has been approved by emilio

@bors-servo
Copy link
Contributor

⌛ Testing commit c42f384 with merge 3792662...

bors-servo pushed a commit that referenced this pull request Aug 30, 2017
Avoid std::fmt in serialization code

<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-cssparser/190)
<!-- Reviewable:end -->
@bors-servo
Copy link
Contributor

☀️ Test successful - status-travis
Approved by: emilio
Pushing 3792662 to master...

@bors-servo bors-servo merged commit c42f384 into master Aug 30, 2017
@SimonSapin SimonSapin deleted the no-fmt branch August 31, 2017 12:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants