Implement InlinedVector independently of absl.#13676
Conversation
|
|
|
|
|
1 similar comment
|
|
|
|
|
|
|
|
|
|
| - grpc | ||
| - gpr_test_util | ||
| - gpr | ||
| - name: vector_test |
There was a problem hiding this comment.
you should also update the corresponding BUILD file
There was a problem hiding this comment.
It's already there. See test/core/support/BUILD.
| template <typename... Args> | ||
| void emplace_back(Args&&... args) { | ||
| if (size_ < N) { | ||
| new (&inline_[size_]) T(std::forward<Args>(args)...); |
There was a problem hiding this comment.
We can't use new. Prefer this. Idem for line 85 and 92 below.
There was a problem hiding this comment.
This is placement-new, not allocate-new. It's the same syntax used in the New() function you pointed at.
| InlinedVector& operator=(const InlinedVector&) = delete; | ||
|
|
||
| T& operator[](size_t offset) { | ||
| assert(offset < size_); |
There was a problem hiding this comment.
That's what I had originally, but @ctiller said to switch to this.
Craig, was this because this is more idiomatic in C++?
| } | ||
| } | ||
|
|
||
| TEST(InlinedVectorTest, ValuesAreInlined) { |
There was a problem hiding this comment.
would it be possible to test whether the values really are inlined, as opposed to placed in heap memory?
There was a problem hiding this comment.
I'm not sure I see an easy way to do that. Any suggestions?
There was a problem hiding this comment.
There are ways of detecting whether a variable lives on the stack vs the heap, but unfortunately they aren't portable. And for a tiny test like this it's not worth doing more sophisticated stuff such as using custom allocators. Nevermind.
| TEST(InlinedVectorTest, PushBackWithMove) { | ||
| InlinedVector<UniquePtr<int>, 1> v; | ||
| UniquePtr<int> i = MakeUnique<int>(3); | ||
| v.push_back(std::move(i)); |
There was a problem hiding this comment.
we could check that i has been null'ed after the move.
|
|
|
|
|
|
| } | ||
| } | ||
|
|
||
| TEST(InlinedVectorTest, ValuesAreInlined) { |
There was a problem hiding this comment.
There are ways of detecting whether a variable lives on the stack vs the heap, but unfortunately they aren't portable. And for a tiny test like this it's not worth doing more sophisticated stuff such as using custom allocators. Nevermind.
d601da1 to
c6406f3
Compare
|
|
|
|
Known issue: #13953 |
Splitting this off from the retry PR, since I'm going to want to use it in other code as well.