FastLED 3.9.15
Loading...
Searching...
No Matches

◆ intern() [4/4]

fl::string fl::StringInterner::intern ( const string_view & sv)

Definition at line 29 of file string_interner.cpp.hpp.

29 {
30 if (sv.empty()) return fl::string();
31
32 // SSO optimization: strings that fit in the inline buffer don't benefit from interning
33 // because fl::string will store them inline (no heap allocation anyway).
34 // Interning small strings would just add hash overhead with no memory savings.
35 if (sv.size() <= FASTLED_STR_INLINED_SIZE) {
36 return fl::string(sv);
37 }
38
39 // String is large enough to require heap allocation - check if already interned
40 // Try to find existing entry - O(1) average via hash map
41 auto it = mEntries.find(sv);
42 if (it != mEntries.end()) {
43 // Found existing - return fl::string sharing the StringHolder
44 return fl::string(it->second);
45 }
46
47 // Not found - create new StringHolder (heap-allocated)
48 auto holder = fl::make_shared<StringHolder>(sv.data(), sv.size());
49
50 // Create string_view key that points into holder's data
51 // This is safe because StringHolder data is heap-allocated and never moves
52 string_view key(holder->data(), holder->length());
53
54 // Insert into map - key points into value's data (self-referential)
55 mEntries[key] = holder;
56
57 // Return fl::string sharing the StringHolder
58 return fl::string(holder);
59}
fl::unordered_map< string_view, StringHolderPtr > mEntries
shared_ptr< T > make_shared(Args &&... args) FL_NOEXCEPT
Definition shared_ptr.h:414
#define FASTLED_STR_INLINED_SIZE
std::string compatible string class.
Definition string.h:32

References fl::string_view::data(), fl::string_view::empty(), FASTLED_STR_INLINED_SIZE, fl::make_shared(), mEntries, and fl::string_view::size().

Referenced by fl::intern(), fl::intern(), fl::intern(), fl::intern(), fl::string::intern(), intern(), intern(), intern(), fl::string::interned(), fl::string::interned(), and fl::string::interned().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: