Skip to content

⚡ Bolt: MeasureVLQ Method Optimization#23

Open
tedd wants to merge 1 commit into
mainfrom
bolt-measurevlq-optimization-16881916124573945133
Open

⚡ Bolt: MeasureVLQ Method Optimization#23
tedd wants to merge 1 commit into
mainfrom
bolt-measurevlq-optimization-16881916124573945133

Conversation

@tedd

@tedd tedd commented Jul 7, 2026

Copy link
Copy Markdown
Owner

💡 Hypothesis: The while loops implemented within MeasureVLQ(Int64) and MeasureVLQ(UInt64) traverse variable bit widths in O(log N) iterations. This execution mechanism incurs superfluous computational cycles that could be consolidated by direct manipulation of the most significant set bit.

🎯 Execution: Replaced the sequential logical shift loops with a constant time mathematical resolution. The algorithm calculates 63 - LeadingZeroCount() to obtain the magnitude of the number and mathematically determines the VLQ encoding boundaries utilizing a log2 / 7 translation strategy.

📊 Empirical Impact:

Method Mean Error StdDev Ratio RatioSD Allocated Alloc Ratio
Legacy_MeasureVLQ_UInt64 8.718 us 0.0478 us 0.0423 us 1.00 0.00 - NA
Optimized_MeasureVLQ_UInt64 2.384 us 0.0283 us 0.0221 us 0.27 0.00 - NA
Legacy_MeasureVLQ_Int64 11.132 us 0.1534 us 0.1360 us 1.28 0.02 - NA
Optimized_MeasureVLQ_Int64 2.652 us 0.0272 us 0.0212 us 0.30 0.00 - NA

🔬 Verification Protocol: Execute dotnet run -c Release within the src/Tedd.SpanUtils.Benchmarks project to replicate these findings symmetrically across the integrated Archive and main components. Run dotnet test on the unit test project to verify data equivalence boundaries.


PR created automatically by Jules for task 16881916124573945133 started by @tedd

Refactors the O(log N) bitwise shifting calculations within `MeasureVLQ` and its related overrides into an O(1) mathematical formulation utilizing CPU-accelerated LeadingZeroCount. Integrates BenchmarkDotNet infrastructure (`Archive` and `Benchmarks` projects) to structurally demonstrate latency improvements while verifying identical deterministic outcomes.

Co-authored-by: tedd <493224+tedd@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 7, 2026 00:48
@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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 optimizes SpanUtils.MeasureVLQ(Int64/UInt64) by replacing iterative shifting loops with a leading-zero-count based computation to determine VLQ encoded length in constant time, and adds benchmark scaffolding to compare the optimized implementation against a legacy baseline.

Changes:

  • Replaced loop-based MeasureVLQ(Int64) / MeasureVLQ(UInt64) sizing logic with an O(1) calculation using BitUtils.LeadingZeroCount.
  • Added a new Tedd.SpanUtils.Benchmarks project plus an Archive project to preserve the legacy implementation for benchmarking comparisons.
  • Updated the solution to include the new projects and additional build configurations.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/Tedd.SpanUtils/Utils.cs Implements the optimized constant-time MeasureVLQ sizing logic.
src/Tedd.SpanUtils.sln Adds new Archive/Benchmarks projects and expands solution configurations.
src/Tedd.SpanUtils.Benchmarks/Tedd.SpanUtils.Benchmarks.csproj Introduces a BenchmarkDotNet console project targeting net8.0.
src/Tedd.SpanUtils.Benchmarks/Program.cs Adds benchmarks comparing legacy vs optimized MeasureVLQ for ulong and long.
src/Tedd.SpanUtils.Archive/Utils.Archive.cs Adds legacy MeasureVLQ implementations used as a benchmark baseline.
src/Tedd.SpanUtils.Archive/Tedd.SpanUtils.Archive.csproj Introduces the Archive project targeting net8.0.
.jules/bolt.md Adds a Bolt note describing the optimization rationale and benchmark intent.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread .jules/bolt.md
Comment on lines +2 to +3
## 2024-07-07 - MeasureVLQ Loops O(N) to O(1) Bitwise Evaluation
**Observation:** `MeasureVLQ` and `MeasureWriteSize` logic relied on serial O(N) checking and O(log N) scaling via byte masking. Using intrinsic instruction scaling to `Lzcnt.IsSupported` enables zero allocation calculation of VLQ width limits.
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.

2 participants