tempbuf.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #include "tempbuf.h"
  2. #include "addstorage.h"
  3. #include <util/system/yassert.h>
  4. #include <util/system/defaults.h>
  5. #include <util/generic/intrlist.h>
  6. #include <util/generic/singleton.h>
  7. #include <util/generic/yexception.h>
  8. #include <utility>
  9. #include <util/thread/singleton.h>
  10. #ifndef TMP_BUF_LEN
  11. #define TMP_BUF_LEN (64 * 1024)
  12. #endif
  13. class TTempBuf::TImpl: public TRefCounted<TImpl, TSimpleCounter, TImpl> {
  14. public:
  15. inline TImpl(void* data, size_t size) noexcept
  16. : Data_(data)
  17. , Size_(size)
  18. , Offset_(0)
  19. {
  20. }
  21. /*
  22. * We do not really need 'virtual' here
  23. * but for compiler happiness...
  24. */
  25. virtual ~TImpl() = default;
  26. inline void* Data() noexcept {
  27. return Data_;
  28. }
  29. const void* Data() const noexcept {
  30. return Data_;
  31. }
  32. inline size_t Size() const noexcept {
  33. return Size_;
  34. }
  35. inline size_t Filled() const noexcept {
  36. return Offset_;
  37. }
  38. inline void Reset() noexcept {
  39. Offset_ = 0;
  40. }
  41. inline size_t Left() const noexcept {
  42. return Size() - Filled();
  43. }
  44. void SetPos(size_t off) {
  45. Y_ASSERT(off <= Size());
  46. Offset_ = off;
  47. }
  48. inline void Proceed(size_t off) {
  49. Y_ASSERT(off <= Left());
  50. Offset_ += off;
  51. }
  52. static inline void Destroy(TImpl* This) noexcept {
  53. This->Dispose();
  54. }
  55. protected:
  56. virtual void Dispose() noexcept = 0;
  57. private:
  58. void* Data_;
  59. size_t Size_;
  60. size_t Offset_;
  61. };
  62. namespace {
  63. class TTempBufManager;
  64. class TAllocedBuf: public TTempBuf::TImpl, public TAdditionalStorage<TAllocedBuf> {
  65. public:
  66. inline TAllocedBuf()
  67. : TImpl(AdditionalData(), AdditionalDataLength())
  68. {
  69. }
  70. inline ~TAllocedBuf() override = default;
  71. private:
  72. void Dispose() noexcept override {
  73. delete this;
  74. }
  75. };
  76. class TPerThreadedBuf: public TTempBuf::TImpl, public TIntrusiveSListItem<TPerThreadedBuf> {
  77. friend class TTempBufManager;
  78. public:
  79. inline TPerThreadedBuf(TTempBufManager* manager) noexcept
  80. : TImpl(Data_, sizeof(Data_))
  81. , Manager_(manager)
  82. {
  83. }
  84. inline ~TPerThreadedBuf() override = default;
  85. private:
  86. void Dispose() noexcept override;
  87. private:
  88. char Data_[TMP_BUF_LEN];
  89. TTempBufManager* Manager_;
  90. };
  91. class TTempBufManager {
  92. struct TDelete {
  93. inline void operator()(TPerThreadedBuf* p) noexcept {
  94. delete p;
  95. }
  96. };
  97. public:
  98. inline TTempBufManager() noexcept {
  99. }
  100. inline ~TTempBufManager() {
  101. TDelete deleter;
  102. Unused_.ForEach(deleter);
  103. }
  104. inline TPerThreadedBuf* Acquire() {
  105. if (!Unused_.Empty()) {
  106. return Unused_.PopFront();
  107. }
  108. return new TPerThreadedBuf(this);
  109. }
  110. inline void Return(TPerThreadedBuf* buf) noexcept {
  111. buf->Reset();
  112. Unused_.PushFront(buf);
  113. }
  114. private:
  115. TIntrusiveSList<TPerThreadedBuf> Unused_;
  116. };
  117. }
  118. static inline TTempBufManager* TempBufManager() {
  119. return FastTlsSingletonWithPriority<TTempBufManager, 2>();
  120. }
  121. static inline TTempBuf::TImpl* AcquireSmallBuffer(size_t size) {
  122. #if defined(_asan_enabled_)
  123. return new (size) TAllocedBuf();
  124. #else
  125. Y_UNUSED(size);
  126. return TempBufManager()->Acquire();
  127. #endif
  128. }
  129. void TPerThreadedBuf::Dispose() noexcept {
  130. if (Manager_ == TempBufManager()) {
  131. Manager_->Return(this);
  132. } else {
  133. delete this;
  134. }
  135. }
  136. TTempBuf::TTempBuf()
  137. : Impl_(AcquireSmallBuffer(TMP_BUF_LEN))
  138. {
  139. }
  140. /*
  141. * all magick is here:
  142. * if len <= TMP_BUF_LEN. then we get prealloced per threaded buffer
  143. * else allocate one in heap
  144. */
  145. static inline TTempBuf::TImpl* ConstructImpl(size_t len) {
  146. if (len <= TMP_BUF_LEN) {
  147. return AcquireSmallBuffer(len);
  148. }
  149. return new (len) TAllocedBuf();
  150. }
  151. TTempBuf::TTempBuf(size_t len)
  152. : Impl_(ConstructImpl(len))
  153. {
  154. }
  155. TTempBuf::TTempBuf(const TTempBuf&) noexcept = default;
  156. TTempBuf::TTempBuf(TTempBuf&& b) noexcept
  157. : Impl_(std::move(b.Impl_))
  158. {
  159. }
  160. TTempBuf::~TTempBuf() = default;
  161. TTempBuf& TTempBuf::operator=(const TTempBuf& b) noexcept {
  162. if (this != &b) {
  163. Impl_ = b.Impl_;
  164. }
  165. return *this;
  166. }
  167. TTempBuf& TTempBuf::operator=(TTempBuf&& b) noexcept {
  168. if (this != &b) {
  169. Impl_ = std::move(b.Impl_);
  170. }
  171. return *this;
  172. }
  173. char* TTempBuf::Data() noexcept {
  174. return (char*)Impl_->Data();
  175. }
  176. const char* TTempBuf::Data() const noexcept {
  177. return static_cast<const char*>(Impl_->Data());
  178. }
  179. size_t TTempBuf::Size() const noexcept {
  180. return Impl_->Size();
  181. }
  182. char* TTempBuf::Current() noexcept {
  183. return Data() + Filled();
  184. }
  185. const char* TTempBuf::Current() const noexcept {
  186. return Data() + Filled();
  187. }
  188. size_t TTempBuf::Filled() const noexcept {
  189. return Impl_->Filled();
  190. }
  191. size_t TTempBuf::Left() const noexcept {
  192. return Impl_->Left();
  193. }
  194. void TTempBuf::Reset() noexcept {
  195. Impl_->Reset();
  196. }
  197. void TTempBuf::SetPos(size_t off) {
  198. Impl_->SetPos(off);
  199. }
  200. char* TTempBuf::Proceed(size_t off) {
  201. char* ptr = Current();
  202. Impl_->Proceed(off);
  203. return ptr;
  204. }
  205. void TTempBuf::Append(const void* data, size_t len) {
  206. if (len > Left()) {
  207. ythrow yexception() << "temp buf exhausted(" << Left() << ", " << len << ")";
  208. }
  209. memcpy(Current(), data, len);
  210. Proceed(len);
  211. }
  212. bool TTempBuf::IsNull() const noexcept {
  213. return !Impl_;
  214. }
  215. #if 0
  216. #include <util/datetime/cputimer.h>
  217. #define LEN (1024)
  218. void* allocaFunc() {
  219. return alloca(LEN);
  220. }
  221. int main() {
  222. const size_t num = 10000000;
  223. size_t tmp = 0;
  224. {
  225. CTimer t("alloca");
  226. for (size_t i = 0; i < num; ++i) {
  227. tmp += (size_t)allocaFunc();
  228. }
  229. }
  230. {
  231. CTimer t("log buffer");
  232. for (size_t i = 0; i < num; ++i) {
  233. TTempBuf buf(LEN);
  234. tmp += (size_t)buf.Data();
  235. }
  236. }
  237. {
  238. CTimer t("malloc");
  239. for (size_t i = 0; i < num; ++i) {
  240. void* ptr = malloc(LEN);
  241. tmp += (size_t)ptr;
  242. free(ptr);
  243. }
  244. }
  245. return 0;
  246. }
  247. #endif