SmallBuffer.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. #pragma once
  3. #include <array>
  4. #include <cstddef>
  5. #include <cstdint>
  6. #include <type_traits>
  7. /** Helper class for allocating temporary fixed size arrays with SBO.
  8. *
  9. * This is intentionally much simpler than SmallVector, to improve performance
  10. * at the expense of many features:
  11. * - No zero-initialization for numeric types
  12. * - No resizing after construction
  13. * - No copy/move
  14. * - No non-trivial types
  15. */
  16. namespace c10 {
  17. template <typename T, size_t N>
  18. class SmallBuffer {
  19. static_assert(std::is_trivial_v<T>, "SmallBuffer is intended for POD types");
  20. std::array<T, N> storage_;
  21. size_t size_{};
  22. T* data_{};
  23. public:
  24. SmallBuffer(size_t size) : size_(size) {
  25. if (size > N) {
  26. data_ = new T[size];
  27. } else {
  28. data_ = &storage_[0];
  29. }
  30. }
  31. SmallBuffer(const SmallBuffer&) = delete;
  32. SmallBuffer& operator=(const SmallBuffer&) = delete;
  33. // move constructor is needed in function return
  34. SmallBuffer(SmallBuffer&& rhs) noexcept : size_{rhs.size_} {
  35. rhs.size_ = 0;
  36. if (size_ > N) {
  37. data_ = rhs.data_;
  38. rhs.data_ = nullptr;
  39. } else {
  40. storage_ = std::move(rhs.storage_);
  41. data_ = &storage_[0];
  42. }
  43. }
  44. SmallBuffer& operator=(SmallBuffer&&) = delete;
  45. ~SmallBuffer() {
  46. if (size_ > N) {
  47. delete[] data_;
  48. }
  49. }
  50. T& operator[](size_t idx) {
  51. return data()[idx];
  52. }
  53. const T& operator[](size_t idx) const {
  54. return data()[idx];
  55. }
  56. T* data() {
  57. return data_;
  58. }
  59. const T* data() const {
  60. return data_;
  61. }
  62. size_t size() const {
  63. return size_;
  64. }
  65. T* begin() {
  66. return data_;
  67. }
  68. const T* begin() const {
  69. return data_;
  70. }
  71. T* end() {
  72. return data_ + size_;
  73. }
  74. const T* end() const {
  75. return data_ + size_;
  76. }
  77. };
  78. } // namespace c10
  79. #else
  80. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  81. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)