Skip to content

⚡ Bolt: [O(N^2) complexity checking dependency elevation capabilities] - #153

Merged
Tcode-Motion merged 2 commits into
mainfrom
perf-capability-validation-4119314015324881544
Sep 15, 2026
Merged

Tcode-Motion merged 2 commits into
mainfrom
perf-capability-validation-4119314015324881544

Conversation

@Tcode-Motion

Copy link
Copy Markdown
Owner

💡 What:
Optimized CapabilityValidator::validate_elevation to be $O(N)$ rather than $O(N^2)$. Previously, the function checked allowed_elevations.contains(&dep_name.to_string()) inside the dependency capability loop. We now pre-collect the allowed_elevations into a HashSet<&str> before iterating.

🎯 Why:
The original implementation performed an $O(N)$ lookup through a slice on every iteration of the loop, and concurrently performed a heap allocation (dep_name.to_string()). Over many capabilities or deeply nested dependency graphs, this adds significant overhead.

📊 Measured Improvement:
I set up a Criterion benchmark (capability_validation_bench.rs) that simulated 100 dependency capabilities and 100 allowed elevation package names.

  • Baseline: ~21-23 µs
  • Optimized: ~11 µs
  • Result: ~50% improvement in validation time and zero allocations inside the loop.

PR created automatically by Jules for task 4119314015324881544 started by @Tcode-Motion

In `CapabilityValidator::validate_elevation`, the original loop performed an $O(N)$ scan of `allowed_elevations` inside the loop, and worse, allocated a new `String` for `dep_name` on every iteration (`dep_name.to_string()`).

This optimization pre-collects `allowed_elevations` into a `HashSet<&str>` before the loop, dropping the runtime complexity inside the loop from $O(N)$ to $O(1)$ and completely eliminating the loop-internal heap allocations.

Benchmarks (`capability_validation_bench.rs`) show a ~50% reduction in execution time for the validation step.

Co-authored-by: Tcode-Motion <188012755+Tcode-Motion@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Replaced the deprecated `criterion::black_box` with `std::hint::black_box` in `tools/package-manager/benches/capability_validation_bench.rs` which was causing `cargo clippy` to fail on CI because of `-D warnings`.

Co-authored-by: Tcode-Motion <188012755+Tcode-Motion@users.noreply.github.com>
@Tcode-Motion
Tcode-Motion marked this pull request as ready for review September 15, 2026 08:48
@Tcode-Motion
Tcode-Motion merged commit 6a3c314 into main Sep 15, 2026
6 checks passed
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