Skip to content

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

Merged
merged 9 commits into from
Jun 16, 2017
Merged

Reduce the size of Token #159

merged 9 commits into from
Jun 16, 2017

Conversation

SimonSapin
Copy link
Member

@SimonSapin SimonSapin commented Jun 15, 2017

Hopefully improving parsing performance: https://bugzilla.mozilla.org/show_bug.cgi?id=1347408


This change is Reviewable

@SimonSapin SimonSapin requested a review from mbrubeck June 15, 2017 19:58
bors-servo pushed a commit to servo/servo that referenced this pull request Jun 15, 2017
bors-servo pushed a commit to servo/servo that referenced this pull request Jun 15, 2017
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 -->
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.

Looks good! Some comments.

// * If the tag is 0, the memory is borrowed.
// `ptr` and `len` are the components of a `&'a str`.

ptr: *const u8,
Copy link
Member

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?

Copy link
Member Author

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);
Copy link
Member

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).

Copy link
Contributor

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.

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 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.

}
}

/// Convert into `String`, re-using the memory allocation it was already owned.
Copy link
Member

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.

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.

if self.is_borrowed() {
CompactCowStr { ..*self }
} else {
Self::from(String::from(&**self).into_boxed_str())
Copy link
Member

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?

Copy link
Member

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.

Copy link
Member Author

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.

@emilio
Copy link
Member

emilio commented Jun 15, 2017

Tests need an update, and feel free to wait for @mbrubeck to review too, in case he has any other comment :)

Copy link
Contributor

@mbrubeck mbrubeck left a 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);
Copy link
Contributor

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.

const MAX_LEN: usize = !0 >> 1;

// Only the highest bit
const TAG: usize = MAX_LEN + 1;
Copy link
Contributor

@mbrubeck mbrubeck Jun 15, 2017

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.

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.

}

impl<'a> CompactCowStr<'a> {
/// Wether this string refers to borrowed memory
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo: "Whether"

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.

@SimonSapin
Copy link
Member Author

servo/servo#17345 (comment) has lots of failures, let’s not land this until we figure that out.

bors-servo pushed a commit to servo/servo that referenced this pull request Jun 16, 2017
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 -->
bors-servo pushed a commit to servo/servo that referenced this pull request Jun 16, 2017
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 -->
@SimonSapin
Copy link
Member Author

@emilio, @mbrubeck, I got a green Servo try run, after fixing another double-free. (Missing mem::forget in a method that consumes self.) I’ll take your respective "Looks good" above as "r+" so that I can publish to make it easier to do a Gecko try run. Let me know if there is something else you’d like to fix and I’ll follow up.

@bors-servo r=emilio,mbrubeck

@bors-servo
Copy link
Contributor

📌 Commit 60b953e has been approved by emilio,mbrubeck

@bors-servo
Copy link
Contributor

⌛ Testing commit 60b953e with merge ad98a45...

bors-servo pushed a commit that referenced this pull request Jun 16, 2017
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 -->
@SimonSapin
Copy link
Member Author

Rebase at the same base that I had been testing.

@bors-servo r=emilio,mbrubeck

@bors-servo
Copy link
Contributor

📌 Commit 2033c23 has been approved by emilio,mbrubeck

@bors-servo
Copy link
Contributor

⌛ Testing commit 2033c23 with merge 13cce38...

bors-servo pushed a commit that referenced this pull request Jun 16, 2017
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 -->
@bors-servo
Copy link
Contributor

☀️ Test successful - status-travis
Approved by: emilio,mbrubeck
Pushing 13cce38 to master...

@bors-servo bors-servo merged commit 2033c23 into master Jun 16, 2017
@SimonSapin SimonSapin deleted the size_of branch June 16, 2017 11:48
bors-servo pushed a commit to servo/servo that referenced this pull request Jun 16, 2017
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 -->
jryans pushed a commit to jryans/gecko-dev that referenced this pull request Jun 16, 2017
…up); r=<try>

Depends on servo/rust-cssparser#159

Source-Repo: https://github.com/servo/servo
Source-Revision: 75876a0e2220b7b95541feef4393288b195b090e
moz-v2v-gh pushed a commit to mozilla/gecko-dev that referenced this pull request Jun 17, 2017
…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
aethanyc pushed a commit to aethanyc/gecko-dev that referenced this pull request Jun 19, 2017
…up); r=<try>

Depends on servo/rust-cssparser#159

Source-Repo: https://github.com/servo/servo
Source-Revision: 75876a0e2220b7b95541feef4393288b195b090e
JerryShih pushed a commit to JerryShih/gecko-dev that referenced this pull request Jun 19, 2017
…up); r=<try>

Depends on servo/rust-cssparser#159

Source-Repo: https://github.com/servo/servo
Source-Revision: 75876a0e2220b7b95541feef4393288b195b090e
Manishearth pushed a commit to Manishearth/gecko-dev that referenced this pull request Jun 23, 2017
…up); r=<try>

Depends on servo/rust-cssparser#159

Source-Repo: https://github.com/servo/servo
Source-Revision: 75876a0e2220b7b95541feef4393288b195b090e
@SimonSapin SimonSapin mentioned this pull request Jun 25, 2017
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.

4 participants