Skip to content

Optimize RzVector and RzPVector implementations and algorithms#3

Open
notxvilka wants to merge 2 commits into
devfrom
optimize-vector-introsort-2816901706661995987
Open

Optimize RzVector and RzPVector implementations and algorithms#3
notxvilka wants to merge 2 commits into
devfrom
optimize-vector-introsort-2816901706661995987

Conversation

@notxvilka
Copy link
Copy Markdown
Owner

@notxvilka notxvilka commented May 3, 2026

Changes

Performance optimizations for RzVector and RzPVector as requested in issue rizinorg#6096.

Key changes:

  1. Introsort: Replaced the naive randomized quicksort with Introsort, ensuring $O(n \log n)$ performance even in worst-case scenarios.
  2. Memory Management: Sorting now avoids repeated heap allocations during recursion. It uses a 512-byte stack buffer for elements up to 256 bytes, falling back to a single heap allocation otherwise.
  3. Algorithmic Optimizations:
    • rz_pvector_uniq now uses a sort-then-deduplicate approach, improving complexity from $O(n^2)$ to $O(n \log n)$.
    • Added rz_vector_remove_if for $O(n)$ bulk removal.
    • Added cache prefetching in rz_pvector_find and rz_pvector_find_index.
  4. Micro-optimizations:
    • Optimized rz_vector_push fast path.
    • Adjusted INITIAL_VECTOR_LEN to 8 and simplified growth logic.
  5. Benchmarks: Added test/bench/bench_vector.c to measure and verify these improvements. Sorting performance nearly doubled in tests.
  6. Documentation: Followed the project convention of keeping Doxygen for API functions in the implementation files.

Before

―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― ✀  ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
Benchmark              Iterations Total time [ms] Avg iter time [us/iteration] Throughput [iterations/sec]
-----------------------------------------------------------------------------------------------------------
[vector push] 100k            100       58.286000                   582.860000                 1715.677864
[vector push] 1m               10       56.575000                  5657.500000                  176.756518
[pvector push] 100k           100       39.777000                   397.770000                 2514.015637
[pvector push] 1m              10       57.391000                  5739.100000                  174.243348
[vector sort] 10k             100      167.933000                  1679.330000                  595.475577
[pvector sort] 10k            100       63.534000                   635.340000                 1573.960399
[vector contains] 10k         100        7.065000                    70.650000                14154.281670
[pvector contains] 10k        100        6.346000                    63.460000                15757.957769

――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
1/1 bench - rizin:vector OK               1.32s

After

―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― ✀  ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
Benchmark              Iterations Total time [ms] Avg iter time [us/iteration] Throughput [iterations/sec]
-----------------------------------------------------------------------------------------------------------
[vector push] 100k            100       48.720000                   487.200000                 2052.545156
[vector push] 1m               10       55.643000                  5564.300000                  179.717125
[pvector push] 100k           100       41.264000                   412.640000                 2423.419930
[pvector push] 1m              10       49.652000                  4965.200000                  201.401756
[vector sort] 10k             100      106.239000                  1062.390000                  941.273920
[pvector sort] 10k            100      102.484000                  1024.840000                  975.762070
[vector contains] 10k         100        7.029000                    70.290000                14226.774790
[pvector contains] 10k        100        6.482000                    64.820000                15427.337242

――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
1/1 bench - rizin:vector OK               1.83s

PR created automatically by Jules for task 2816901706661995987 started by @notxvilka

@google-labs-jules
Copy link
Copy Markdown

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

@notxvilka notxvilka force-pushed the optimize-vector-introsort-2816901706661995987 branch from bf12567 to ade67e1 Compare May 3, 2026 12:51
@notxvilka notxvilka force-pushed the optimize-vector-introsort-2816901706661995987 branch from ade67e1 to f2e9df5 Compare May 3, 2026 13:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants