nfx-stringbuilderpool 1.0.0
High-performance C++20 library for zero-allocation string building with thread-safe pooling
Loading...
Searching...
No Matches
StringBuilderPool.h
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 nfx
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
79#pragma once
80
81#include <cstdint>
82#include <memory>
83#include <string>
84#include <string_view>
85
86namespace nfx::string
87{
88 //=====================================================================
89 // DynamicStringBuffer class
90 //=====================================================================
91
104 {
105 friend class DynamicStringBufferPool;
106
107 private:
108 //----------------------------------------------
109 // Construction
110 //----------------------------------------------
111
114
120 explicit DynamicStringBuffer( size_t initialCapacity );
121
122 public:
128
134
135 //----------------------------------------------
136 // Destruction
137 //----------------------------------------------
138
141
142 //----------------------------------------------
143 // Assignment
144 //----------------------------------------------
145
152
159
160 //----------------------------------------------
161 // Capacity and size information
162 //----------------------------------------------
163
170 [[nodiscard]] size_t size() const noexcept;
171
178 [[nodiscard]] size_t capacity() const noexcept;
179
185 [[nodiscard]] bool isEmpty() const noexcept;
186
187 //----------------------------------------------
188 // Buffer management
189 //----------------------------------------------
190
195 void clear() noexcept;
196
203 void reserve( size_t newCapacity );
204
211 void resize( size_t newSize );
212
213 //----------------------------------------------
214 // Data access
215 //----------------------------------------------
216
223 [[nodiscard]] char* data() noexcept;
224
231 [[nodiscard]] const char* data() const noexcept;
232
239 char& operator[]( size_t index );
240
247 const char& operator[]( size_t index ) const;
248
249 //----------------------------------------------
250 // Content manipulation
251 //----------------------------------------------
252
259 void append( std::string_view str );
260
267 void append( const std::string& str );
268
275 void append( const char* str );
276
283 void push_back( char c );
284
285 //----------------------------------------------
286 // String conversion
287 //----------------------------------------------
288
294 [[nodiscard]] std::string toString() const;
295
302 [[nodiscard]] std::string_view toStringView() const noexcept;
303
304 //----------------------------------------------
305 // Iterator interface
306 //----------------------------------------------
307
309 using value_type = char;
310
312 using iterator = char*;
313
315 using const_iterator = const char*;
316
323 [[nodiscard]] iterator begin() noexcept;
324
331 [[nodiscard]] const_iterator begin() const noexcept;
332
339 [[nodiscard]] iterator end() noexcept;
340
347 [[nodiscard]] const_iterator end() const noexcept;
348
349 private:
350 //----------------------------------------------
351 // Small buffer optimization constants
352 //----------------------------------------------
353
355 static constexpr size_t STACK_BUFFER_SIZE = 256;
356
358 static constexpr auto GROWTH_FACTOR = 1.5;
359
360 //----------------------------------------------
361 // Private members
362 //----------------------------------------------
363
365 alignas( char ) char m_stackBuffer[STACK_BUFFER_SIZE];
366
368 std::unique_ptr<char[]> m_heapBuffer;
369
371 size_t m_size;
372
374 size_t m_capacity;
375
377 bool m_onHeap;
378
379 //----------------------------------------------
380 // Private methods
381 //----------------------------------------------
382
387 void ensureCapacity( size_t needed_capacity );
388
393 char* currentBuffer() noexcept;
394
399 const char* currentBuffer() const noexcept;
400 };
401
402 //=====================================================================
403 // StringBuilder class
404 //=====================================================================
405
422 class StringBuilder final
423 {
424 friend class StringBuilderLease;
425
426 //----------------------------------------------
427 // Construction
428 //----------------------------------------------
429 private:
431 inline explicit StringBuilder( DynamicStringBuffer& buffer );
432
433 public:
435 StringBuilder() = delete;
436
438 StringBuilder( const StringBuilder& ) = default;
439
441 StringBuilder( StringBuilder&& ) noexcept = delete;
442
443 //----------------------------------------------
444 // Destruction
445 //----------------------------------------------
446
448 ~StringBuilder() = default;
449
450 //----------------------------------------------
451 // Assignment
452 //----------------------------------------------
453
455 StringBuilder& operator=( const StringBuilder& ) = delete;
456
458 StringBuilder& operator=( StringBuilder&& ) noexcept = delete;
459
460 //----------------------------------------------
461 // Array access operators
462 //----------------------------------------------
463
469 inline char& operator[]( size_t index );
470
476 inline const char& operator[]( size_t index ) const;
477
478 //----------------------------------------------
479 // String append operations
480 //----------------------------------------------
481
486 inline void append( std::string_view str );
487
492 inline void append( const std::string& str );
493
498 inline void append( const char* str );
499
504 inline void push_back( char c );
505
506 //----------------------------------------------
507 // Stream operators
508 //----------------------------------------------
509
515 inline StringBuilder& operator<<( std::string_view str );
516
522 inline StringBuilder& operator<<( const std::string& str );
523
529 inline StringBuilder& operator<<( const char* str );
530
536 inline StringBuilder& operator<<( char c );
537
538 //----------------------------------------------
539 // Size and capacity management
540 //----------------------------------------------
541
546 inline size_t length() const noexcept;
547
552 inline void resize( size_t newSize );
553
554 //----------------------------------------------
555 // Iterator interface
556 //----------------------------------------------
557
559 using value_type = char;
560
562 using iterator = char*;
563
565 using const_iterator = const char*;
566
571 inline iterator begin();
572
577 inline const_iterator begin() const;
578
583 inline iterator end();
584
589 inline const_iterator end() const;
590
591 //----------------------------------------------
592 // StringBuilder::Enumerator class
593 //----------------------------------------------
594
611 {
612 public:
613 //----------------------------
614 // Construction
615 //----------------------------
616
622 inline Enumerator( const StringBuilder& builder );
623
624 //----------------------------
625 // Destruction
626 //----------------------------
627
629 ~Enumerator() = default;
630
631 //----------------------------
632 // Enumerator operations
633 //----------------------------
634
639 inline bool next();
640
645 inline char current() const;
646
648 inline void reset();
649
650 private:
651 //----------------------------
652 // Private member variables
653 //----------------------------
654
656 const char* m_data;
657
659 const char* m_end;
660
662 const char* m_current;
663 };
664
665 private:
666 //----------------------------------------------
667 // Private member variables
668 //----------------------------------------------
669
671 DynamicStringBuffer& m_buffer;
672 };
673
674 //=====================================================================
675 // StringBuilderLease class
676 //=====================================================================
677
696 {
697 friend class StringBuilderPool;
698
699 //----------------------------------------------
700 // Construction
701 //----------------------------------------------
702 private:
704 inline explicit StringBuilderLease( DynamicStringBuffer* buffer );
705
706 public:
709
712
717 inline StringBuilderLease( StringBuilderLease&& other ) noexcept;
718
719 //----------------------------------------------
720 // Destruction
721 //----------------------------------------------
722
725
726 //----------------------------------------------
727 // Assignment
728 //----------------------------------------------
729
732
739
740 //----------------------------------------------
741 // Public interface methods
742 //----------------------------------------------
743
749 [[nodiscard]] inline StringBuilder create();
750
756 [[nodiscard]] inline DynamicStringBuffer& buffer();
757
763 [[nodiscard]] inline std::string toString() const;
764
765 private:
766 //----------------------------------------------
767 // Private implementation methods
768 //----------------------------------------------
769
771 void dispose();
772
774 [[noreturn]] void throwInvalidOperation() const;
775
776 //----------------------------------------------
777 // Private member variables
778 //----------------------------------------------
779
781 DynamicStringBuffer* m_buffer;
782
784 bool m_valid;
785 };
786
787 //=====================================================================
788 // StringBuilderPool class
789 //=====================================================================
790
858 {
859 public:
860 //----------------------------------------------
861 // Pool statistics structure
862 //----------------------------------------------
863
866 {
869
872
875
878
880 double hitRate;
881 };
882
883 private:
884 //----------------------------------------------
885 // Construction
886 //----------------------------------------------
887
889 StringBuilderPool() = default;
890
891 public:
892 //----------------------------------------------
893 // Static factory methods
894 //----------------------------------------------
895
912 [[nodiscard]] static StringBuilderLease lease();
913
914 //----------------------------
915 // Statistics methods
916 //----------------------------
917
922 static PoolStatistics stats() noexcept;
923
925 static void resetStats() noexcept;
926
927 //----------------------------
928 // Lease management
929 //----------------------------
930
935 static size_t clear();
936
941 static size_t size() noexcept;
942 };
943} // namespace nfx::string
944
945#include "nfx/detail/string/StringBuilderPool.inl"
High-performance dynamic string buffer with efficient memory management.
iterator begin() noexcept
Get mutable iterator to beginning of buffer.
void append(std::string_view str)
Append string_view content to buffer.
bool isEmpty() const noexcept
Check if buffer is empty.
const char * const_iterator
Immutable iterator type for buffer traversal.
char * iterator
Mutable iterator type for buffer traversal.
DynamicStringBuffer(const DynamicStringBuffer &other)
Copy constructor.
char * data() noexcept
Get mutable pointer to buffer data.
void reserve(size_t newCapacity)
Reserve minimum capacity for buffer.
char value_type
Character type for iterator compatibility.
DynamicStringBuffer & operator=(DynamicStringBuffer &&other) noexcept
Move assignment operator.
void resize(size_t newSize)
Resize buffer to specified size.
DynamicStringBuffer & operator=(const DynamicStringBuffer &other)
Copy assignment operator.
~DynamicStringBuffer()=default
Destructor.
std::string toString() const
Convert buffer content to std::string.
void clear() noexcept
Clear buffer content without deallocating memory.
DynamicStringBuffer(DynamicStringBuffer &&other) noexcept
Move constructor.
void push_back(char c)
Append single character to buffer.
size_t size() const noexcept
Get current buffer size in bytes.
iterator end() noexcept
Get mutable iterator to end of buffer.
std::string_view toStringView() const noexcept
Get string_view of buffer content.
size_t capacity() const noexcept
Get current buffer capacity in bytes.
High-performance string builder with fluent interface and efficient memory management.
const char * const_iterator
Immutable iterator type for buffer traversal.
StringBuilder(StringBuilder &&) noexcept=delete
Move constructor.
StringBuilder(const StringBuilder &)=default
Copy constructor.
char * iterator
Mutable iterator type for buffer traversal.
char value_type
Character type for iterator compatibility.
StringBuilder()=delete
Default constructor.
Forward-only iterator for character-by-character enumeration of StringBuilder content.
Enumerator(const StringBuilder &builder)
Constructs an enumerator for iterating over the characters in the given StringBuilder buffer.
void reset()
Resets the enumerator to the initial position (before the first character).
bool next()
Advances to the next character in the buffer.
char current() const
Returns the current character in the buffer.
RAII lease wrapper for pooled StringBuilder buffers with automatic resource management.
std::string toString() const
Converts buffer contents to std::string.
StringBuilderLease & operator=(const StringBuilderLease &)=delete
Copy assignment operator.
StringBuilderLease(const StringBuilderLease &)=delete
Copy constructor.
StringBuilderLease()=delete
Default constructor.
StringBuilderLease & operator=(StringBuilderLease &&other) noexcept
Move assignment operator.
DynamicStringBuffer & buffer()
Provides direct access to underlying memory buffer.
StringBuilderLease(StringBuilderLease &&other) noexcept
Move constructor.
StringBuilder create()
Creates StringBuilder wrapper for buffer manipulation.
Thread-safe memory pool for high-performance StringBuilder instances with optimized allocation strate...
static PoolStatistics stats() noexcept
Gets current pool statistics.
static StringBuilderLease lease()
Creates a new StringBuilder lease with an optimally sourced memory buffer.
Pool performance statistics for external access.
uint64_t dynamicStringBufferPoolHits
Number of successful buffer retrievals from shared cross-thread pool.
uint64_t totalRequests
Total number of buffer requests made to the pool.
double hitRate
Cache hit rate as a percentage (0.0 to 1.0)
uint64_t newAllocations
Number of new buffer allocations when pools were empty.
uint64_t threadLocalHits
Number of successful buffer retrievals from thread-local cache.