Commit 66d50da2 authored by Zino Benaissa's avatar Zino Benaissa Committed by Facebook GitHub Bot

Make folly::small_vector friendly to compiler optimizations

Summary:
This includes several enhancements to folly::small_vector:

Eliminate overhead accessing vector elements by making heap_ points to element 0 of the vector.  To get start address only dispatch is required as shown below:

     value_type* data () {
       // Dispatch between heap_ or internalStorage,
       return isExtern() ? heap_ : &internalStorage_;
     }

Reorganize the implementation by recording if capacity is stored internally or on the heap in "size". As a result the maximum vector size is half smaller. This is the only visible behavior change of this PR. Assuming size is 64 bits then
  - bit63 determines if vector elements are stored in internalStorage_ or externally in the heap
  - bit62 determines if vector capacity is stored internally or on the heap
  - bit61..bit0 contain the size of the vector.

Reduce the overhead of updating size buy adding incrementSize functions.

Tune emplace_back function and vector initialization.

Reviewed By: yfeldblum

Differential Revision: D24413105

fbshipit-source-id: b447704832af9c734be51aa6d23b8cb4be69a8bf
parent f1c0c3c1
This diff is collapsed.
...@@ -618,7 +618,7 @@ TEST(small_vector, NoHeap) { ...@@ -618,7 +618,7 @@ TEST(small_vector, NoHeap) {
// Check max_size works right with various policy combinations. // Check max_size works right with various policy combinations.
folly::small_vector<std::string, 32, uint32_t> v4; folly::small_vector<std::string, 32, uint32_t> v4;
EXPECT_EQ(v4.max_size(), (1ul << 31) - 1); EXPECT_EQ(v4.max_size(), (1ul << 30) - 1);
/* /*
* Test that even when we ask for a small number inlined it'll still * Test that even when we ask for a small number inlined it'll still
...@@ -641,9 +641,9 @@ TEST(small_vector, NoHeap) { ...@@ -641,9 +641,9 @@ TEST(small_vector, NoHeap) {
TEST(small_vector, MaxSize) { TEST(small_vector, MaxSize) {
folly::small_vector<int, 2, uint8_t> vec; folly::small_vector<int, 2, uint8_t> vec;
EXPECT_EQ(vec.max_size(), 127); EXPECT_EQ(vec.max_size(), 63);
folly::small_vector<int, 2, uint16_t> vec2; folly::small_vector<int, 2, uint16_t> vec2;
EXPECT_EQ(vec2.max_size(), (1 << 15) - 1); EXPECT_EQ(vec2.max_size(), (1 << 14) - 1);
} }
TEST(small_vector, AllHeap) { TEST(small_vector, AllHeap) {
......
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