This is an archive of the discontinued LLVM Phabricator instance.

[libc++] Optimize ranges::find for vector<bool>
ClosedPublic

Authored by philnik on Jul 22 2023, 2:40 PM.

Details

Reviewers
Mordante
Group Reviewers
Restricted Project
Commits
rG8670b53e11bb: [libc++] Optimize ranges::find for vector<bool>
Summary

Benchmark results:

----------------------------------------------------------------
Benchmark                                    old             new
----------------------------------------------------------------
bm_vector_bool_ranges_find/1             5.64 ns         6.08 ns
bm_vector_bool_ranges_find/2             16.5 ns         6.03 ns
bm_vector_bool_ranges_find/3             20.3 ns         6.07 ns
bm_vector_bool_ranges_find/4             22.2 ns         6.08 ns
bm_vector_bool_ranges_find/5             23.5 ns         6.05 ns
bm_vector_bool_ranges_find/6             24.4 ns         6.10 ns
bm_vector_bool_ranges_find/7             26.7 ns         6.10 ns
bm_vector_bool_ranges_find/8             25.0 ns         6.08 ns
bm_vector_bool_ranges_find/16            27.9 ns         6.07 ns
bm_vector_bool_ranges_find/64            44.5 ns         5.35 ns
bm_vector_bool_ranges_find/512            243 ns         25.7 ns
bm_vector_bool_ranges_find/4096          1858 ns         35.6 ns
bm_vector_bool_ranges_find/32768        15461 ns         93.5 ns
bm_vector_bool_ranges_find/262144      126462 ns          571 ns
bm_vector_bool_ranges_find/1048576     497736 ns         2272 ns

Diff Detail

Event Timeline

philnik created this revision.Jul 22 2023, 2:40 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 22 2023, 2:40 PM
philnik requested review of this revision.Jul 22 2023, 2:40 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 22 2023, 2:40 PM
Herald added a reviewer: Restricted Project. · View Herald Transcript

Benchmarks:

----------------------------------------------------------------
Benchmark                                    old             new
----------------------------------------------------------------
bm_vector_bool_ranges_find/1             5.64 ns         6.08 ns
bm_vector_bool_ranges_find/2             16.5 ns         6.03 ns
bm_vector_bool_ranges_find/3             20.3 ns         6.07 ns
bm_vector_bool_ranges_find/4             22.2 ns         6.08 ns
bm_vector_bool_ranges_find/5             23.5 ns         6.05 ns
bm_vector_bool_ranges_find/6             24.4 ns         6.10 ns
bm_vector_bool_ranges_find/7             26.7 ns         6.10 ns
bm_vector_bool_ranges_find/8             25.0 ns         6.08 ns
bm_vector_bool_ranges_find/16            27.9 ns         6.07 ns
bm_vector_bool_ranges_find/64            44.5 ns         5.35 ns
bm_vector_bool_ranges_find/512            243 ns         25.7 ns
bm_vector_bool_ranges_find/4096          1858 ns         35.6 ns
bm_vector_bool_ranges_find/32768        15461 ns         93.5 ns
bm_vector_bool_ranges_find/262144      126462 ns          571 ns
bm_vector_bool_ranges_find/1048576     497736 ns         2272 ns

Does this fix the bug you posted? https://github.com/llvm/llvm-project/issues/64038

The benchmarks look great!

I did not look very close at the patch, I really like to see what the CI thinks first.

Does this fix the bug you posted? https://github.com/llvm/llvm-project/issues/64038

Partially. There are still lots of optimizations missed. I'll upload more patches to fix other algorithms later.

Great benchmarks!

Nitpick: can you also mention std::find in the commit message? When I first read the title, I thought we had some missed optimization opportunity specifically in the Ranges code.

Mordante requested changes to this revision.Jul 26 2023, 9:22 AM

I like to look at it when the CI passes or at least can apply the patch. The underlying stack is accepted, please rebase when the underlying patches have landed.

This revision now requires changes to proceed.Jul 26 2023, 9:22 AM
Mordante accepted this revision.Jul 31 2023, 9:01 AM

LGTM with the comments addressed.

libcxx/benchmarks/algorithms/find.bench.cpp
61

please add the benchmarks to the commit message.

libcxx/include/__bit/invert_if.h
24–28
This revision is now accepted and ready to land.Jul 31 2023, 9:01 AM
philnik edited the summary of this revision. (Show Details)Jul 31 2023, 6:03 PM
philnik updated this revision to Diff 545890.Jul 31 2023, 6:16 PM
philnik marked 2 inline comments as done.

Try to fix CI

philnik updated this revision to Diff 545899.Jul 31 2023, 7:12 PM

Try to fix CI

This revision was automatically updated to reflect the committed changes.