Commit 11de0762 authored by Clemens Backes's avatar Clemens Backes Committed by V8 LUCI CQ

[utils] Improve BitVector performance

Avoid most of the {is_inline()} checks by having a {data_begin_} pointer
which either points at the inline storage or at the zone-allocated
memory.
This replaces a dynamic branch by a memory indirection, which is
beneficial for big (non-inline) BitVectors. For small BitVectors we will
have to see what the bots say; the hypothesis is that a memory load is
still faster than a dynamic branch.

Apart from better performance, this change allows for simpler code in
many places, including the iterator implementation.

R=jkummerow@chromium.org

Bug: v8:13063
Change-Id: I1e28279d1a438598e0b8403a6a4078c2cd2a4c48
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3776685Reviewed-by: 's avatarJakob Kummerow <jkummerow@chromium.org>
Commit-Queue: Clemens Backes <clemensb@chromium.org>
Cr-Commit-Position: refs/heads/main@{#81902}
parent 0998bbe6
......@@ -4,6 +4,8 @@
#include "src/utils/bit-vector.h"
#include <numeric>
#include "src/base/bits.h"
#include "src/utils/utils.h"
......@@ -26,13 +28,10 @@ void BitVector::Print() const {
#endif
int BitVector::Count() const {
if (is_inline()) return base::bits::CountPopulation(data_.inline_);
int count = 0;
for (int i = 0; i < data_length_; i++) {
count += base::bits::CountPopulation(data_.ptr_[i]);
}
return count;
auto accumulate_popcnt = [](int cnt, uintptr_t word) -> int {
return cnt + base::bits::CountPopulation(word);
};
return std::accumulate(data_begin_, data_end_, 0, accumulate_popcnt);
}
} // namespace internal
......
This diff is collapsed.
......@@ -126,5 +126,40 @@ TEST_F(BitVectorTest, Resize) {
EXPECT_TRUE(!v.Contains(243));
}
TEST_F(BitVectorTest, BigBitVectorIterator) {
// Big BitVector with big and small entries.
BitVector v(500, zone());
v.Add(27);
v.Add(300);
v.Add(499);
auto iter = v.begin();
auto end = v.end();
EXPECT_NE(iter, end);
EXPECT_EQ(27, *iter);
++iter;
EXPECT_NE(iter, end);
EXPECT_EQ(300, *iter);
++iter;
EXPECT_NE(iter, end);
EXPECT_EQ(499, *iter);
++iter;
EXPECT_EQ(iter, end);
// Remove small entries, add another big one.
v.Resize(1000, zone());
v.Remove(27);
v.Remove(300);
v.Add(500);
iter = v.begin();
end = v.end();
EXPECT_NE(iter, end);
EXPECT_EQ(499, *iter);
++iter;
EXPECT_NE(iter, end);
EXPECT_EQ(500, *iter);
++iter;
EXPECT_EQ(iter, end);
}
} // namespace internal
} // namespace v8
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment