-
Notifications
You must be signed in to change notification settings - Fork 135
Reduce the size of Token #159
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
Conversation
Upgrade cssparser to 0.15 Depends on servo/rust-cssparser#159
Upgrade cssparser to 0.15 Depends on servo/rust-cssparser#159 <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/17345) <!-- Reviewable:end -->
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Some comments.
src/compact_cow_str.rs
Outdated
// * If the tag is 0, the memory is borrowed. | ||
// `ptr` and `len` are the components of a `&'a str`. | ||
|
||
ptr: *const u8, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Add a TODO for NonZero
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, done. Although I think we never use this in Option
or other places where NonZero
would make a difference.
#[inline] | ||
fn from(s: &'a str) -> Self { | ||
let len = s.len(); | ||
assert!(len <= MAX_LEN); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quick question, why add the tag to the length instead of the pointer?
Both seem fine, but with the second you get the benefit of not having the MAX_LENGTH restriction (though with the first you need to remove the tag).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least in the current implementation of libstd, String
and Vec
in Rust are already restricted to a maximum capacity of isize::MAX
, so this doesn't add any further restrictions on the length.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don’t know if any bit on the pointer is guaranteed to be available. Maybe not high bits since it seems like a userland process can get up to 3 GB on 32-bit. Not low bits since a subslice of &str
can start at any byte.
src/compact_cow_str.rs
Outdated
} | ||
} | ||
|
||
/// Convert into `String`, re-using the memory allocation it was already owned. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: if it was already owned
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
if self.is_borrowed() { | ||
CompactCowStr { ..*self } | ||
} else { | ||
Self::from(String::from(&**self).into_boxed_str()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why can't this create a borrowed version when cloning, given it has the same lifetime?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
err, nevermind, I think I see why.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If self
is borrowed, the new value will be borrowed too. If not, the lifetime parameter is irrelevant: it could be 'static
, and the owned memory does not necessarily live that long.
Tests need an update, and feel free to wait for @mbrubeck to review too, in case he has any other comment :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Just a few more minor comments:
#[inline] | ||
fn from(s: &'a str) -> Self { | ||
let len = s.len(); | ||
assert!(len <= MAX_LEN); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least in the current implementation of libstd, String
and Vec
in Rust are already restricted to a maximum capacity of isize::MAX
, so this doesn't add any further restrictions on the length.
src/compact_cow_str.rs
Outdated
const MAX_LEN: usize = !0 >> 1; | ||
|
||
// Only the highest bit | ||
const TAG: usize = MAX_LEN + 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reading this code, I have trouble remembering whether the tag bit is set for borrowed or owned strings. Perhaps it should be renamed to OWNED
or similar.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
src/compact_cow_str.rs
Outdated
} | ||
|
||
impl<'a> CompactCowStr<'a> { | ||
/// Wether this string refers to borrowed memory |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo: "Whether"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
servo/servo#17345 (comment) has lots of failures, let’s not land this until we figure that out. |
… to require less alignment and reduce the size of Token
* Avoid using some missing From impls * Manually re-order fields
Upgrade cssparser to 0.15 Depends on servo/rust-cssparser#159 <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/17345) <!-- Reviewable:end -->
Upgrade cssparser to 0.15 Depends on servo/rust-cssparser#159 <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/17345) <!-- Reviewable:end -->
@emilio, @mbrubeck, I got a green Servo try run, after fixing another double-free. (Missing @bors-servo r=emilio,mbrubeck |
📌 Commit 60b953e has been approved by |
Reduce the size of Token Hopefully improving parsing performance: https://bugzilla.mozilla.org/show_bug.cgi?id=1347408 <!-- 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/159) <!-- Reviewable:end -->
Rebase at the same base that I had been testing. @bors-servo r=emilio,mbrubeck |
📌 Commit 2033c23 has been approved by |
Reduce the size of Token Hopefully improving parsing performance: https://bugzilla.mozilla.org/show_bug.cgi?id=1347408 <!-- 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/159) <!-- Reviewable:end -->
☀️ Test successful - status-travis |
Upgrade cssparser to 0.15 Depends on servo/rust-cssparser#159 <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/17345) <!-- Reviewable:end -->
…up); r=<try> Depends on servo/rust-cssparser#159 Source-Repo: https://github.com/servo/servo Source-Revision: 75876a0e2220b7b95541feef4393288b195b090e
…up); r=<try> Depends on servo/rust-cssparser#159 Source-Repo: https://github.com/servo/servo Source-Revision: 75876a0e2220b7b95541feef4393288b195b090e --HG-- extra : subtree_source : https%3A//hg.mozilla.org/projects/converted-servo-linear extra : subtree_revision : 3adf5873dad10451b7d57cc1a5feec40cabeb7d7
…up); r=<try> Depends on servo/rust-cssparser#159 Source-Repo: https://github.com/servo/servo Source-Revision: 75876a0e2220b7b95541feef4393288b195b090e
…up); r=<try> Depends on servo/rust-cssparser#159 Source-Repo: https://github.com/servo/servo Source-Revision: 75876a0e2220b7b95541feef4393288b195b090e
…up); r=<try> Depends on servo/rust-cssparser#159 Source-Repo: https://github.com/servo/servo Source-Revision: 75876a0e2220b7b95541feef4393288b195b090e
Hopefully improving parsing performance: https://bugzilla.mozilla.org/show_bug.cgi?id=1347408
This change is