[all-commits] [llvm/llvm-project] ca451d: ADT: Add SmallVectorImpl::truncate() to replace us...

Duncan P. N. Exon Smith via All-commits all-commits at lists.llvm.org
Wed Dec 8 13:58:06 PST 2021


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: ca451d3fa40ebbd35c702ba698e9e5f29e8ed69b
      https://github.com/llvm/llvm-project/commit/ca451d3fa40ebbd35c702ba698e9e5f29e8ed69b
  Author: Duncan P. N. Exon Smith <dexonsmith at apple.com>
  Date:   2021-12-08 (Wed, 08 Dec 2021)

  Changed paths:
    M llvm/include/llvm/ADT/SmallVector.h
    M llvm/unittests/ADT/SmallVectorTest.cpp

  Log Message:
  -----------
  ADT: Add SmallVectorImpl::truncate() to replace uses of set_size()

Add `SmallVectorImpl::truncate()`, a variant of `resize()` that cannot
increase the size.

- Compared to `resize()`, this has no code path for growing the
  allocation and can be better optimized.
- Compared to `set_size()`, this formally calls destructors, and does
  not skip any constructors.
- Compared to `pop_back_n()`, this takes the new desired size, which in
  many contexts is more intuitive than the number of elements to remove.

The immediate motivation is to pair this with `resize_for_overwrite()`
to remove uses of `set_size()`, which can then be made private.

Differential Revision: https://reviews.llvm.org/D115383




More information about the All-commits mailing list