-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
core::ascii::escape_default: reduce struct size #89015
Conversation
(rust-highfive has picked a reviewer for you, use r? to override) |
Is there some concrete motivation for this? Generally speaking I don't think the trade off of the unsafe code is worth it here; the escape_default iterator is unlikely to be in a particularly hot path. Further, it's not inlined today (and can't be without -Zbuild-std or equivalent), since all of this code is not generic and so is codegen'd into std. (This is not to say it should be inlined; I think this is fine). |
Motivation was that i looked around std and checked if there some needless bound checks or similar things (plus looking for function usage in compiler or deps). Btw, can i replace things like this collect-via-iterator with to_string in this PR? rust/compiler/rustc_ast/src/util/literal.rs Lines 170 to 172 in e0c38af
|
I don't feel like this is sufficient justification for introducing unsafe code here. While it is not too hard to justify soundness here, it doesn't seem warranted to me, since it makes the code harder to read and edit and for little benefit. Bounds checking is not that expensive. Making the struct smaller also seems unlikely to be of much help, though I'm not really opposed. Practically speaking it'll almost always be put on the stack, and there it doesn't really matter that much whether we are using 24 or 6 bytes -- it's a bump allocation anyway. But I'm OK r+'ing the first commit here.
This PR does not seem like the right place for changes to the compiler, no. Not sure I follow your question entirely, but that feels like a change best left for separate PR(s). |
library/core/src/ascii.rs
Outdated
@@ -138,7 +138,7 @@ impl FusedIterator for EscapeDefault {} | |||
impl fmt::Display for EscapeDefault { | |||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |||
// SAFETY: ok because `escape_default` created only valid utf-8 data | |||
f.write_str(unsafe { from_utf8_unchecked(&self.data[self.range.clone()]) }) | |||
f.write_str(unsafe { from_utf8_unchecked(&self.data[..self.range.end as usize]) }) |
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.
This is a change in behavior if the iterator has already been advanced from the front, right?
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.
Nope, when dropped second commit noticed, that i changed this in first.
This comment has been minimized.
This comment has been minimized.
…ize 24 -> 6 bytes
@rustbot label: -S-waiting-on-author +S-waiting-on-review |
@bors r+ rollup |
📌 Commit cccd6e0 has been approved by |
Rollup of 12 pull requests Successful merges: - rust-lang#88795 (Print a note if a character literal contains a variation selector) - rust-lang#89015 (core::ascii::escape_default: reduce struct size) - rust-lang#89078 (Cleanup: Remove needless reference in ParentHirIterator) - rust-lang#89086 (Stabilize `Iterator::map_while`) - rust-lang#89096 ([bootstrap] Improve the error message when `ninja` is not found to link to installation instructions) - rust-lang#89113 (dont `.ensure()` the `thir_abstract_const` query call in `mir_build`) - rust-lang#89114 (Fixes a technicality regarding the size of C's `char` type) - rust-lang#89115 (:arrow_up: rust-analyzer) - rust-lang#89126 (Fix ICE when `indirect_structural_match` is allowed) - rust-lang#89141 (Impl `Error` for `FromSecsError` without foreign type) - rust-lang#89142 (Fix match for placeholder region) - rust-lang#89147 (add case for checking const refs in check_const_value_eq) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Rollup of 12 pull requests Successful merges: - rust-lang#88795 (Print a note if a character literal contains a variation selector) - rust-lang#89015 (core::ascii::escape_default: reduce struct size) - rust-lang#89078 (Cleanup: Remove needless reference in ParentHirIterator) - rust-lang#89086 (Stabilize `Iterator::map_while`) - rust-lang#89096 ([bootstrap] Improve the error message when `ninja` is not found to link to installation instructions) - rust-lang#89113 (dont `.ensure()` the `thir_abstract_const` query call in `mir_build`) - rust-lang#89114 (Fixes a technicality regarding the size of C's `char` type) - rust-lang#89115 (:arrow_up: rust-analyzer) - rust-lang#89126 (Fix ICE when `indirect_structural_match` is allowed) - rust-lang#89141 (Impl `Error` for `FromSecsError` without foreign type) - rust-lang#89142 (Fix match for placeholder region) - rust-lang#89147 (add case for checking const refs in check_const_value_eq) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
No description provided.