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

Slightly improve InternedString #5287

Merged
merged 1 commit into from
Apr 3, 2018
Merged

Conversation

matklad
Copy link
Member

@matklad matklad commented Apr 3, 2018

  • Use &'static str instead of (ptr, len) pair to reduce unsafety.
  • try make hash calculation O(1) instead of O(n), fail miserably,
    document findings.
  • Rename to_inner -> as_str().

@matklad
Copy link
Member Author

matklad commented Apr 3, 2018

r? @Eh2406

@bors delegate=Eh2406

@bors
Copy link
Contributor

bors commented Apr 3, 2018

✌️ @Eh2406 can now approve this pull request

ptr: s.as_ptr(),
len: s.len(),
}
let s = match cache.get(str).map(|&s| s) {
Copy link
Contributor

@Eh2406 Eh2406 Apr 3, 2018

Choose a reason for hiding this comment

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

Would this be clearer as unwrap_or_else?

Copy link
Member Author

@matklad matklad Apr 3, 2018

Choose a reason for hiding this comment

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

Awesome observation @Eh2406, updated!

I've actually spent about ten minutes trying to figure out why on earth cach.entry(str) does not work, only to find out that entry API for sets does not exist yet: rust-lang/rfcs#1490 :-)

Using unwrap_or_else didn't occur to me at all!

@Eh2406
Copy link
Contributor

Eh2406 commented Apr 3, 2018

Thanks for doing this cleanup, and for documenting the problem with hash. I am sorry I did not add a comment to that effect after #5153 (comment). I wish we could separate hash for fingerprints from hash for HashMaps, but I don't see how to do that.

@matklad
Copy link
Member Author

matklad commented Apr 3, 2018

I wish we could separate hash for fingerprints from hash for HashMaps.

Interesting idea! Note that we have to do some funny business with hashes of Paths as well... I wonder if we can introduce trait FingerprintHash? That probably would be over engineering...

@matklad
Copy link
Member Author

matklad commented Apr 3, 2018

and for documenting the problem with hash.

For posterity, another potential interaction is that if we ever implement Borrow<str> for InternedString, than we'll have to provide the same Hash as well.

@Eh2406
Copy link
Contributor

Eh2406 commented Apr 3, 2018

I wonder if we can introduce trait FingerprintHash?

Or a serde serializer that returns the hash of the json representation or something.

implement Borrow for InternedString, than we'll have to provide the same Hash as well.

Do you want to add a comment to that effect as well?

@matklad
Copy link
Member Author

matklad commented Apr 3, 2018

Do you want to add a comment to that effect as well?

Not really: already afk and probably we’ll think about it anyway when/if implementing Borrow :-)

I’ve actually did add the Borrow impl at first, but then reasoned that WAGNI

fn identity(s: &InternedString) -> (*const u8, usize) {
(s.inner.as_ptr(), s.inner.len())
}
identity(self) == identity(other)
Copy link
Contributor

Choose a reason for hiding this comment

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

ptr::eq(self.inner, other.inner)?

Copy link
Member Author

Choose a reason for hiding this comment

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

Wow, I haven't realized that *const str is a thing!

Copy link
Contributor

Choose a reason for hiding this comment

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

@matklad I am willing to approve when you are happy. Do you want to make this change, or should I say the magick spell?

* Use `&'static str` instead of (ptr, len) pair to reduce unsafety.
* try make hash calculation O(1) instead of O(n), fail miserably,
  document findings.
* Rename `to_inner` -> `as_str()`.
@Eh2406
Copy link
Contributor

Eh2406 commented Apr 3, 2018

@bors r+

@bors
Copy link
Contributor

bors commented Apr 3, 2018

📌 Commit d9880c3 has been approved by Eh2406

@bors
Copy link
Contributor

bors commented Apr 3, 2018

⌛ Testing commit d9880c3 with merge 9da0b7c...

bors added a commit that referenced this pull request Apr 3, 2018
Slightly improve InternedString

* Use `&'static str` instead of (ptr, len) pair to reduce unsafety.
* try make hash calculation O(1) instead of O(n), fail miserably,
  document findings.
* Rename `to_inner` -> `as_str()`.
@bors
Copy link
Contributor

bors commented Apr 3, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: Eh2406
Pushing 9da0b7c to master...

@bors bors merged commit d9880c3 into rust-lang:master Apr 3, 2018
@matklad matklad deleted the safer-intern branch April 7, 2018 15:24
@ehuss ehuss added this to the 1.27.0 milestone Feb 6, 2022
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.

6 participants