Skip to content

perf: remove redundant clones in ESM package maps#31025

Closed
Meltedd wants to merge 1 commit into
oven-sh:mainfrom
Meltedd:perf/resolver-borrow-esm-lookup-maps
Closed

perf: remove redundant clones in ESM package maps#31025
Meltedd wants to merge 1 commit into
oven-sh:mainfrom
Meltedd:perf/resolver-borrow-esm-lookup-maps

Conversation

@Meltedd
Copy link
Copy Markdown

@Meltedd Meltedd commented May 19, 2026

What does this PR do?

Makes ESM package map resolution faster by removing clones left over from the Rust port.

The resolver was cloning the active ConditionsMap when constructing ESModule, then cloning the matched Entry when reading from package exports or imports. This changes both paths to borrow from the existing resolver and package JSON data instead.

Resolution behavior stays the same for package exports, package imports, and CSS @import package resolution.

I timed the changed resolver paths with hyperfine:

case before after change
package root conditions 92.4 ms ± 1.2 79.8 ms ± 1.2 1.16x faster
exact exports/imports lookup 141.2 ms ± 2.0 124.9 ms ± 1.9 1.13x faster

How did you verify your code works?

  • cargo fmt --all --check
  • git diff --check
  • cargo check -p bun_resolver --target x86_64-pc-windows-msvc
  • bun bd test test/bundler/esbuild/css.test.ts test/bundler/esbuild/packagejson.test.ts test/js/bun/resolve/import-custom-condition.test.ts
  • Release build benchmarks with hyperfine

Copy link
Copy Markdown
Contributor

@claude claude Bot left a comment

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 19, 2026

Review Change Stack

Walkthrough

Module export/import resolution now borrows Entry and ConditionsMap references instead of cloning them. Type signatures for Entry::value_for_key and ESModule::conditions changed to borrowed forms. Resolver callsites now pass borrowed condition references. A test verifies CSS @import selects the "style" export condition.

Changes

Module resolution ownership optimization

Layer / File(s) Summary
Entry and ESModule type contracts
src/resolver/package_json.rs
Entry::value_for_key(&self, key_: &[u8]) -> Option<&Entry> now returns a borrowed reference instead of cloning. ESModule<'a> now borrows its conditions field as &'a ConditionsMap instead of owning it.
Local export/import resolution
src/resolver/package_json.rs
resolve_exports computes and passes a borrowed Option<&Entry> for the main export path. resolve_imports_exports passes the borrowed target directly to resolve_target.
Resolver callsites for ESModule construction
src/resolver/resolver.rs
All ESModule construction sites in load_node_modules (main exports, retry-without-extension paths, and cached/global resolution blocks) and load_package_imports now pass kind-dependent borrowed &self.opts.conditions.* references instead of computing cloned conditions.
CSS conditional export test
test/bundler/esbuild/css.test.ts
New test case css/CSSAtImportPackageExportsStyleCondition verifies CSS @import "pkg" resolves using the "style" condition from package.json exports and bundles the correct style content.

Possibly related PRs

  • oven-sh/bun#30845: Updates resolve_target invocation in imports resolution to preserve forward slashes in Posix mode when handling specifier-like targets, complementing this PR's borrowed reference changes to how targets and conditions are passed into the resolution function.
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title directly describes the main change: removing redundant clones in ESM package maps, which aligns with the core modification across the three modified files.
Description check ✅ Passed The description fully covers both required sections: 'What does this PR do?' explains the changes and their impact, and 'How did you verify your code works?' lists comprehensive verification steps including testing and benchmarking.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@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: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/resolver/resolver.rs`:
- Around line 3221-3227: The match on import `kind` currently routes CSS-at
imports to `import` conditions; update the match arms in both places where you
build `conditions: match kind { ... }` (the blocks that currently list
ast::ImportKind::Require | ast::ImportKind::RequireResolve =>
&self.opts.conditions.require and fallback to &self.opts.conditions.import) to
explicitly handle ast::ImportKind::At and ast::ImportKind::AtConditional and
return &self.opts.conditions.style for those cases, leaving other import kinds
to return &self.opts.conditions.import; apply the same change at the second
construction site as well so CSS `style` conditions are restored consistently.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 8051f639-bb21-442b-9b89-d648574eb9c2

📥 Commits

Reviewing files that changed from the base of the PR and between a3408a2 and 06fc4cf.

📒 Files selected for processing (3)
  • src/resolver/package_json.rs
  • src/resolver/resolver.rs
  • test/bundler/esbuild/css.test.ts

Comment thread src/resolver/resolver.rs
@Meltedd Meltedd force-pushed the perf/resolver-borrow-esm-lookup-maps branch from 06fc4cf to 0d40b24 Compare May 19, 2026 03:55
Copy link
Copy Markdown
Contributor

@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: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/resolver/resolver.rs`:
- Around line 4837-4842: The match that selects condition sets for package
imports currently maps only Require/RequireResolve to
self.opts.conditions.require and everything else to import; update the logic in
load_package_imports() so that ast::ImportKind::At and
ast::ImportKind::AtConditional are routed to self.opts.conditions.style, keep
Require/RequireResolve -> require, and use import for the remaining kinds;
locate the match around the `conditions:` selection in resolver.rs and replace
or extend the arms to return &self.opts.conditions.style for the
At/AtConditional cases.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: d4fa8c81-64cb-47ab-9d1c-19178cc58bf5

📥 Commits

Reviewing files that changed from the base of the PR and between 06fc4cf and 0d40b24.

📒 Files selected for processing (3)
  • src/resolver/package_json.rs
  • src/resolver/resolver.rs
  • test/bundler/esbuild/css.test.ts

Comment thread src/resolver/resolver.rs
@Meltedd
Copy link
Copy Markdown
Author

Meltedd commented May 20, 2026

Closing this as superseded by #30971, which already landed the resolver borrowing changes for ConditionsMap and Entry.

@Meltedd Meltedd closed this May 20, 2026
@Meltedd Meltedd deleted the perf/resolver-borrow-esm-lookup-maps branch May 20, 2026 02:30
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.

1 participant