C++ Weekly - Ep 430 - How Short String Optimizations Work

  Рет қаралды 15,389

C++ Weekly With Jason Turner

C++ Weekly With Jason Turner

Күн бұрын

Пікірлер: 51
@cppweekly
@cppweekly 4 ай бұрын
There were a couple of bugs in the implementation that GCC let slide - mostly around the use of initializing / assigning C-style arrays. Thanks to a viewer for pointing that out. Here's an updated version that works on GCC/Clang/MSVC compiler-explorer.com/z/3ecYaKK8e
@nyyakko
@nyyakko 5 ай бұрын
I think this has to be one of the coolest examples on how and why to use constexpr.
@patrix9999
@patrix9999 5 ай бұрын
Yep, i've been writing my own custom string class implementation and i had to debug such cases at runtime which take me quite some time to make it right, whereas constexpr simply allow us to see the problem at compile time, which is very useful.
@mCoding
@mCoding 4 ай бұрын
Top notch! You did a great job showing off the core ideas of SSO/SOO that can be adapted to any dev's situation while not getting bogged down by hyper-optimizations.
@negidrums915
@negidrums915 5 ай бұрын
constexpr is essentially a compile-time UB sanitizer😮
@cppweekly
@cppweekly 4 ай бұрын
constexpr all the things!
@Xilefian
@Xilefian 5 ай бұрын
A good exercise is implementing small object optimised vector
@sirhenrystalwart8303
@sirhenrystalwart8303 4 ай бұрын
I'm way more sold on the benefit of constexpr/consteval after seeing this.
@cppweekly
@cppweekly 4 ай бұрын
Yeah, I'm still refining the way I present this stuff. This is a really good argument for it!
@Raspredval1337
@Raspredval1337 5 ай бұрын
I've just googled this topic today! PS: one could pack the small string even tighter, there's a way to use single byte for a size, but you'd store the max_small_string_capacity minus size instead. When the small string size equals the max capacity, the capacity minus size becomes zero and acts as a null terminator. 🤯
@higaski
@higaski 5 ай бұрын
That sounds like UB, given that the compiler is free to add padding to its liking.
@garyp.7501
@garyp.7501 5 ай бұрын
@@higaski This trick that @user-cy1m5vb71 uses, could be put into yet another union, and thus you'd know about the packing.
@Raspredval1337
@Raspredval1337 5 ай бұрын
struct string { struct s_large { char* data; // assuming it's a 64bit pointer uint64_t size; // maybe even some padding to add extra stack space // like char padding[8]; }; struct s_small { static constexpr max_capacity = sizeof(s_large); char data[sizeof(heap) - 1]; char max_capacity_minus_size; }; uint64_t capacity; union { s_large l; s_small s; } data; }; even if there's some alignment padding added to the members of the s_large, the s_small should still be fine
@UsernameUsername0000
@UsernameUsername0000 5 ай бұрын
@@Raspredval1337 I don’t get this approach. Won’t requesting c_str for a small string not be guaranteed-null-terminated?
@Raspredval1337
@Raspredval1337 5 ай бұрын
@@UsernameUsername0000if the whole small string storage is zero initialized, then it will be. Plus, when the small string size would be equal to it's capacity, the last byte (capacity_minus_size) would be equal to zero, thus acting as a null terminator
@Subdest
@Subdest 5 ай бұрын
Why Boolean needed to check if it is a small object? Size is present. Max size of small object also is known. I doubt that checking Boolean is much faster then checking if some value is less then a constant…
@hampus23
@hampus23 5 ай бұрын
Well, if you shrink the heap allocated string you don't reallocate so that won't work but there are better ways to do this.
@hampus23
@hampus23 5 ай бұрын
Raymond Chen has written a great post (Inside STL: The string) about this.
@garyp.7501
@garyp.7501 5 ай бұрын
If the string is shrunk, ie a short string is put into a what was a long string, you'd have to be sure to deallocate the extra space. That might be surprising to some users. Ie, long string, ... allocate short string ... deallocate long string ... allocate again. Vs long string .. allocate short string, .... no deallocation long but not too long string ... also no reallocation
@Raspredval1337
@Raspredval1337 5 ай бұрын
size can be zero even if it's a heap-allocated string. But capacity does reflect it's nature tho, you can use that, as long as heap allocated capacity is always larger than the max small string capacity
@cppweekly
@cppweekly 4 ай бұрын
Because it's possible I grew the string bigger than small object size, allocated, then shrunk the string later and don't want to copy data / deallocate storage.
@hwstar9416
@hwstar9416 5 ай бұрын
honestly I'd rather if small string was a different type, maybe something like: small_string str;
@ohwow2074
@ohwow2074 5 ай бұрын
I don't know why they didn't choose to do that in the first place. Just let the user specify the size of the small buffer based on their needs. Now we're limited to a 16 byte buffer.
@lkedvenc6898
@lkedvenc6898 5 ай бұрын
I have implemented my own string class and the idea was quite similar. The regular std::string ruins the heap if you use it very frequently. Of course my string was better. ;-)
@cyrilemeka6987
@cyrilemeka6987 3 ай бұрын
Did you implement a copy on write string?
@mjKlaim
@mjKlaim 5 ай бұрын
I would have made `is_small_storage()` a function which returns either `m_size < ` an arbitrary constant or the result of `m_size < sizeof(small_storage)` (so no additional storage needed for the bool). In several functions we would then need to re-evaluate this but I think it's worth it as it takes less space.
@antonpieper
@antonpieper 5 ай бұрын
Also, a "problem" with this and the video version is that the assignment operator would need to deallocate the memory if you assign a small string to a previously large string, because you would loose the pointer location
@mjKlaim
@mjKlaim 5 ай бұрын
@@antonpieper Yes the check has to be done for any of the modifying operations I guess.
@Mozartenhimer
@Mozartenhimer 5 ай бұрын
I think the real optimization would be just to make the most significant bit of the size a flag
@mjKlaim
@mjKlaim 5 ай бұрын
@@Mozartenhimer Doesnt that reduce the max size of the container? It's an undefined integer type so all the bits are used.
@Mozartenhimer
@Mozartenhimer 5 ай бұрын
@@mjKlaim I don't think you'll miss that 9.2 exabytes.
@FedericoPekin
@FedericoPekin 5 ай бұрын
awesome one!
@garyp.7501
@garyp.7501 5 ай бұрын
To pack this object into something even smaller in space you could use a bit field for the m_size, and while you can't allocate a string that is unsigned int64, you can allocate a unsigned int63. And use a bit at either end to indicate whether the space was allocated on the heap or using your m_small field of your other union. There of course is a minimal overhead of looking at those bit fields vs a bool or the int, but this is a thing that maps doing red/black do.
@cyrilemeka6987
@cyrilemeka6987 3 ай бұрын
The overhead of bit manipulation should be minimal or non-existent. Computers leave and breathe for those operations, I implemented a u8char class that unfortunately doesn't cache the calculated unicode code-point due to memory concerns, but retrieving the unicode code points from the utf-8 encoded text is a relatively fast operation due to switching over to bit manipulation instead of using std::string and std::bitset which were really costly operations.
@cyrilemeka6987
@cyrilemeka6987 3 ай бұрын
Oh and he won't be able to use the bits on either end, the most significant bit is more apt.
@PaulMetalhero
@PaulMetalhero 5 ай бұрын
Would love to see a std::variant version
@cyrilemeka6987
@cyrilemeka6987 3 ай бұрын
Maybe to avoid std::visit and lambda pain?
@anon_y_mousse
@anon_y_mousse 5 ай бұрын
The only thing I dislike here, as usual, is all of the boilerplate code. However, if they incorporate such a string type into the standard then that goes away because you won't have to implement it yourself, and in such a case I would have no complaints.
@UsernameUsername0000
@UsernameUsername0000 5 ай бұрын
Isn’t std::size_t in not ?
@ohwow2074
@ohwow2074 5 ай бұрын
Yeah
@cppweekly
@cppweekly 4 ай бұрын
I often get that one wrong - sorry!
@HinaraT
@HinaraT 5 ай бұрын
I think I would have use the capacity has the common field instead of the size because if for example you modify the length of your string a lot between let say 12 and 20 characters your solution force you to allocate deallocate each time you cross the 16 byte boundary. Whereas the capacity can stay at ~20 while your size can move around as it want. To use capacity instead we can say that size() = capacity when capacity < 16 else size and capacity() = max(16,capacity)
@Raspredval1337
@Raspredval1337 5 ай бұрын
btw, offtopic question: since you really shouldn't store a function address in a void pointer, it's recommended to use a dummy function pointer type instead. The question is: can you reliably store a method pointer into this dummy function pointer type? Or what can you use instead of a void pointer to store a method pointer? 🤔
@cppweekly
@cppweekly 4 ай бұрын
I've never heard of this recommendation to use a dummy function pointer type instead. I just double checked and cast to and from void * is allowed as of C++11. The problem is that it's less portable - on something like harvard architecture you might actually have different sized pointers between function pointer and memory pointer types... I would personally still use void * when I need to do that. So, I have no specifically clear guidance here.
@pierrecolin6376
@pierrecolin6376 5 ай бұрын
Is std::launder necessary for unions? I know the answer is no when the data members are active only once in the lifetime of the union, but this is not the case here.
@cppweekly
@cppweekly 4 ай бұрын
I believe launder is only necessary in the case of something like placement new(), en.cppreference.com/w/cpp/utility/launder there's no mention of unions there.
@EgorChebotarev
@EgorChebotarev 3 ай бұрын
cool
C++ Weekly - Ep 431 - CTAD for NTTP
7:34
C++ Weekly With Jason Turner
Рет қаралды 8 М.
C++ Weekly - Ep 410 -  What Are Padding and Alignment? (And Why You Might Care)
9:48
C++ Weekly With Jason Turner
Рет қаралды 15 М.
How Strong is Tin Foil? 💪
00:25
Brianna
Рет қаралды 58 МЛН
Это было очень близко...
00:10
Аришнев
Рет қаралды 7 МЛН
ПРЯМОЙ ЭФИР. Золотой мяч France Football 2024
4:41:06
Sigma baby, you've conquered soap! 😲😮‍💨 LeoNata family #shorts
00:37
C++ Super Optimization: 1000X Faster
15:33
Dave's Garage
Рет қаралды 324 М.
C++ Weekly - Ep 440 - Revisiting Visitors for std::visit
9:07
C++ Weekly With Jason Turner
Рет қаралды 12 М.
30 Programming Truths I know at 30 that I Wish I Knew at 20
17:41
Why Didn't He Get the Job? Let's Find Out! // Code Review
27:25
The Cherno
Рет қаралды 135 М.
C++ Weekly - Ep 404 - How (and Why) To Write Code That Avoids std::move
8:50
C++ Weekly With Jason Turner
Рет қаралды 31 М.
C++ Weekly - Ep 434 - GCC's Amazing NEW (2024) -Wnrvo
10:19
C++ Weekly With Jason Turner
Рет қаралды 16 М.
Microservices are Technical Debt
31:59
NeetCodeIO
Рет қаралды 594 М.
WHY did this C++ code FAIL?
38:10
The Cherno
Рет қаралды 278 М.
C++ Weekly - Ep 313 - The `constexpr` Problem That Took Me 5 Years To Fix!
26:19
C++ Weekly With Jason Turner
Рет қаралды 23 М.
The cloud is over-engineered and overpriced (no music)
14:39
Tom Delalande
Рет қаралды 677 М.
How Strong is Tin Foil? 💪
00:25
Brianna
Рет қаралды 58 МЛН