[PATCH] D93532: [ADT] Add resize_for_overwrite method to SmallVector.

Nathan James via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 18 04:28:34 PST 2020


njames93 created this revision.
njames93 added reviewers: dblaikie, mehdi_amini, silvas, dexonsmith.
njames93 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Analagous to the std::make_(unqiue|shared)_for_overwrite added in c++20.
If T is POD, and the container gets larger, any new values added wont be initialized.
This is useful when using SmallVector as a buffer where its planned to overwrite any potential new values added.
If T is not POD, `new (Storage) T` functions identically to `new (Storage) T()` so this will function identically to `resize(size_type)`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D93532

Files:
  llvm/include/llvm/ADT/SmallVector.h


Index: llvm/include/llvm/ADT/SmallVector.h
===================================================================
--- llvm/include/llvm/ADT/SmallVector.h
+++ llvm/include/llvm/ADT/SmallVector.h
@@ -460,19 +460,29 @@
     this->Size = 0;
   }
 
-  void resize(size_type N) {
+private:
+  void resizeImpl(size_type N, bool forOverwrite) {
     if (N < this->size()) {
-      this->destroy_range(this->begin()+N, this->end());
+      this->destroy_range(this->begin() + N, this->end());
       this->set_size(N);
     } else if (N > this->size()) {
       if (this->capacity() < N)
         this->grow(N);
       for (auto I = this->end(), E = this->begin() + N; I != E; ++I)
-        new (&*I) T();
+        if (forOverwrite)
+          new (&*I) T;
+        else
+          new (&*I) T();
       this->set_size(N);
     }
   }
 
+public:
+  void resize(size_type N) { resizeImpl(N, false); }
+
+  /// Like resize, but \ref T is POD, the new values won't be initialized.
+  void resize_for_overwrite(size_type N) { resizeImpl(N, true); }
+
   void resize(size_type N, const T &NV) {
     if (N == this->size())
       return;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D93532.312753.patch
Type: text/x-patch
Size: 1121 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201218/765ab92d/attachment.bin>


More information about the llvm-commits mailing list