-
-
Notifications
You must be signed in to change notification settings - Fork 561
feat(span): introduce format_atom!
macro
#10722
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
CodSpeed Instrumentation Performance ReportMerging #10722 will not alter performanceComparing Summary
|
c4ce75e
to
c1b758a
Compare
WalkthroughA new macro named 📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
⏰ Context from checks skipped due to timeout of 90000ms (15)
✨ Finishing Touches
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
c1b758a
to
9dfed3d
Compare
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.
Pull Request Overview
This PR introduces the new macro format_atom! to create an Atom directly in the arena without intermediate String allocation.
- Adds a re-export of the arena-specific String (ArenaString) in lib.rs for internal use.
- Implements and documents the format_atom! macro in atom.rs.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
File | Description |
---|---|
crates/oxc_span/src/lib.rs | Adds a re-export for ArenaString to support the new macro. |
crates/oxc_span/src/atom.rs | Implements and documents the format_atom! macro. |
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.
Actionable comments posted: 0
🧹 Nitpick comments (1)
crates/oxc_span/src/atom.rs (1)
234-269
: Well-designed macro with clear documentation.The implementation is clean and follows Rust best practices:
- Takes an allocator as first argument, consistent with other allocator-based APIs
- Directly constructs the string in the arena without intermediate allocations
- Error handling matches the standard
format!
macro (panics on errors)- Documentation is clear and includes a usage example
Consider enhancing the documentation example to specifically demonstrate the efficiency improvement mentioned in the PR objectives - showing how this avoids an intermediate
String
when converting a type implementingDisplay
to anAtom
:/// # Example /// /// ``` /// use oxc_allocator::Allocator; /// use oxc_span::format_atom; /// let allocator = Allocator::new(); /// /// let s1 = "foo"; /// let s2 = "bar"; /// let formatted = format_atom!(&allocator, "{s1}.{s2}"); /// assert_eq!(formatted, "foo.bar"); +/// +/// // More efficient than creating an intermediate String +/// struct MyType(i32); +/// impl std::fmt::Display for MyType { +/// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +/// write!(f, "MyType({})", self.0) +/// } +/// } +/// +/// let my_value = MyType(42); +/// // Avoid intermediate String allocation +/// let atom = format_atom!(&allocator, "{my_value}"); +/// assert_eq!(atom, "MyType(42)"); /// ```
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
crates/oxc_span/src/atom.rs
(1 hunks)crates/oxc_span/src/lib.rs
(1 hunks)
🔇 Additional comments (1)
crates/oxc_span/src/lib.rs (1)
35-36
: Well structured internal re-export for the new macro.Clean implementation that follows the existing pattern in this module (similar to line 34 for
format_compact_str!
). Appropriately placed within the#[doc(hidden)]
__internal
module, indicating this is not part of the public API.
Merge activity
|
Introduce a `format_atom!` macro. This behaves the same as `std`'s `format!` macro, but constructs the string directly in arena, and returns an `Atom` instead of a `String`. ```rs let foo_dot_bar: Atom = format_atom!(&allocator, "{}.{}", foo, bar); ``` Can also be used to avoid creating a temp `String` when creating an `Atom` using a `Display` impl: ```rs // Before let atom = ctx.ast.atom(&my_display_type.to_string()); // After let atom = format_atom!(ctx.ast.allocator, "{my_display_type}"); ```
9dfed3d
to
0105253
Compare
…#10723) Avoid temp `String` and copying string data by: 1. Reusing existing `Atom` where possible, instead of copying the string data and creating a new `Atom`. 2. Use `format_atom!` macro (introduced in #10722) to avoid creating a temporary `String` when converting `oxc_regular_expression::Pattern` to a string.
Introduce a
format_atom!
macro. This behaves the same asstd
'sformat!
macro, but constructs the string directly in arena, and returns anAtom
instead of aString
.Can also be used to avoid creating a temp
String
when creating anAtom
using aDisplay
impl: