8000 feat(span): introduce `format_atom!` macro by overlookmotel · Pull Request #10722 · oxc-project/oxc · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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

Merged
merged 1 commit into from
May 1, 2025

Conversation

overlookmotel
Copy link
Contributor
@overlookmotel overlookmotel commented Apr 30, 2025

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.

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:

// Before
let atom = ctx.ast.atom(&my_display_type.to_string());
// After
let atom = format_atom!(ctx.ast.allocator, "{my_display_type}");

Copy link
Contributor Author
overlookmotel commented Apr 30, 2025

How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

Copy link
codspeed-hq bot commented Apr 30, 2025

CodSpeed Instrumentation Performance Report

Merging #10722 will not alter performance

Comparing 04-30-feat_span_introduce_format_atom_macro (0105253) with main (9ebf3d4)

Summary

✅ 36 untouched benchmarks

@overlookmotel overlookmotel marked this pull request as ready for review April 30, 2025 13:53
@overlookmotel overlookmotel marked this pull request as draft April 30, 2025 13:54
@overlookmotel overlookmotel force-pushed the 04-30-feat_span_introduce_format_atom_macro branch from c4ce75e to c1b758a Compare April 30, 2025 14:10
Copy link
coderabbitai bot commented Apr 30, 2025

Walkthrough

A new macro named format_atom! has been introduced, designed to function similarly to Rust's standard format! macro but tailored for use with an allocator and returning an Atom type. The macro requires an allocator as its first argument and formats the provided string and expressions into an ArenaString allocated within the specified arena. The result is then converted into an Atom. If formatting fails, the macro will panic. Additionally, the internal module __internal in crates/oxc_span/src/lib.rs now publicly re-exports oxc_allocator::String as ArenaString, specifically for use by the new macro. Documentation and example usage have also been added to illustrate how to use the macro with an allocator and dynamic formatting. No existing exports or functionalities were removed or changed in this update.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9dfed3d and 0105253.

📒 Files selected for processing (2)
  • crates/oxc_span/src/atom.rs (1 hunks)
  • crates/oxc_span/src/lib.rs (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • crates/oxc_span/src/lib.rs
  • crates/oxc_span/src/atom.rs
⏰ Context from checks skipped due to timeout of 90000ms (15)
  • GitHub Check: Test wasm32-wasip1-threads
  • GitHub Check: Clippy
  • GitHub Check: Test (ubuntu-latest)
  • GitHub Check: Conformance
  • GitHub Check: Test NAPI
  • GitHub Check: Test (macos-latest)
  • GitHub Check: Benchmark (codegen)
  • GitHub Check: Benchmark (semantic)
  • GitHub Check: Benchmark (transformer)
  • GitHub Check: Benchmark (formatter)
  • GitHub Check: Benchmark (minifier)
  • GitHub Check: Benchmark (lexer)
  • GitHub Check: Benchmark (isolated_declarations)
  • GitHub Check: Benchmark (parser)
  • GitHub Check: Build Linter Benchmark
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

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

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
@Copilot Copilot AI left a 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.

Copy link
@coderabbitai coderabbitai bot left a 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 implementing Display to an Atom:

 /// # 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

📥 Commits

Reviewing files that changed from the base of the PR and between d5f66fb and 9dfed3d.

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

@Boshen Boshen added the 0-merge Merge with Graphite Merge Queue label May 1, 2025
Copy link
Member
Boshen commented May 1, 2025

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}");
```
@graphite-app graphite-app bot force-pushed the 04-30-feat_span_introduce_format_atom_macro branch from 9dfed3d to 0105253 Compare May 1, 2025 02:03
@graphite-app graphite-app bot merged commit 0105253 into main May 1, 2025
25 checks passed
@graphite-app graphite-app bot deleted the 04-30-feat_span_introduce_format_atom_macro branch May 1, 2025 02:09
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label May 1, 2025
graphite-app bot pushed a commit that referenced this pull request May 1, 2025
…#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category - New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0