Small String Optimization in C++

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys my name is Archana and welcome back to my say boss boss series so today we're going to be talking about strings and specifically something called small string optimization or SSR for short what does it mean why do we care and how does it improve our string experience now strings are a very interesting topic I think in Sabo's lost or injured just in any language in general because they have all of this kind of negative stigma I guess associated with them they have this reputation of being slower and don't even start talking about performance to a C++ programmer because man if something's slower in C++ it's really easy to look at someone's code see them using strings in a way that you just frown upon and then immediately think that they're either not a good programmer or they're doing things that stupid slower way there's just so much negativity associated with strings believe me but I'm here to tell you that it's not all bad and in fact sometimes when you do see someone using a string maybe it's not as bad as you think now strings in general I think I something they're just gonna continue talking about every now and then on this channel it's impossible to program without strings but on the other hand reducing your usage of strings in your code can significantly speed it up depending on how in fact you do use them so expect this topic to stay around but today specifically we're going to be looking at how small strings are actually optimized by the sibyl's law standard library so that they end up being not as bad as you think but first I want to thank hosting out for sponsoring this video if you guys have not heard of hosting out hosting it is a fast and secure web hosting platform now I think that nowadays in 2020 pretty much everyone should have a website if you don't have a website and you're trying to become a programmer well you need some kind of website so that you can easily show people what it is you're working on and what you've achieved especially if you're looking for a job don't even get me started on that and what people don't realize is that the added benefit to all this is that you will know how to set up a website the fact that you've got a website on the Internet to me immediately means that well you know how to set up a website you know how web single accent that's a perp in and of itself now there's a lot to love about hosting us specifically apart from being really really fast really intuitive the fact that they have built-in Diagnostics to help you along the way of something like your DNS isn't set up correctly apart from all of that they're also really really affordable and I absolutely love that because web hosting should be accessible to as many people as possible even if you don't have a lot of money to spend on this and hosting our I really think they hit the ball on that because they are super amazing and affordable a lot of these kind of cheaper web hosts just don't seem to do that great of a job but hosting our is not some cheap web host hosting it is it's just a really good web hosting platform regardless of the price so if you don't have a website yet and you want to get online or you're with some other web host and you're not enjoying that experience and you want to come over to a good one then definitely check out hosting and there'll be a link in the description you can save up to 91% off their hosting plans if you use this code code chenna so definitely check out hosting out if you guys need any kind of web hosting link in the description below alright strings string string strings strings strings where would we be without strings probably not where we are today whether that's a good thing or a bad thing don't know one of the reasons that we don't like strings is because they tend to allocate memory and C++ heap allocations bad those things are just grouped together so much even though I mean arguably speaking memory allocation maybe isn't that bad anyway I better not say anything cuz so because we know that creating a standard string results in a memory allocation a lot of people try and kind of avoid that they'll try and reduce their kind of string usage or they'll try and come up with creative ideas of how to use strings or at worst case scenario they'll just berate you for using strings because why you allocating so much memory all the time don't you know that that is bad but as I mentioned I'm here to tell you that north it's not necessarily bad because the C++ center library has maybe somewhat surprisingly thought of this and they've put a stop to it by saying that small strings that is strings that are not exceedingly long they don't need to be heap-allocated I can just allocate a little stack based buffer that is not heap-allocated and that is gonna be my static string storage for Strings that are below a certain length now what that actual length is to define a small string can absolutely vary based on the surplus plus standard library that you're using but here in Visual Studio 2019 that length seems to be 15 characters if you have a string that is 15 characters or less it will not allocate memory on the heap it will just use that stack buffer as I mentioned however if you do have a string that is 16 characters or above that's gonna hit malloc and you know heap Alec and whatever on earth function is used to allocate memory on whatever platform you're on so in other words if you have a string that's quite small you don't have to worry about using a conch-shell pointer or trying to micromanage or optimize that kind of bit of your code because likely that's not going to result in a heap allocation anyway so without further ado let's dive into Visual Studio and actually take a look at how this works behind the scenes and see it in action I'm just gonna make a string I'm gonna call it churner now this is a really small string it's only six characters which would throw a lot of people off because if you assign this to a standard string this is kind of implication of a heap allocation a lot of people might choose to actually do something like this like a Const our name because this is something that is clearly a static string meaning we're not gonna be appending anything to it we're not taking it in from a file or from like the console or anything like that it's just a simple static string so why then would I use a standard string if that comes with that whole heap allocation overhead well here's a thing it doesn't come with a heap allocation overhead because this fits the criteria of a small string in C++ which means it will in fact just be stored in a statically allocated buffer that does not push the heap at all so let's take a look at this in action what I'm actually gonna do is something a little bit different instead of just straightaway jumping to a practical example and then maybe taking a look at you know overriding operator knew which I think we will do eventually in this video as well I'm just gonna go to the definition of string now let's take a look at this I'll hide this solution Explorer so we string as a basic string now there's a few things that we need to know it here mainly the fact that the element type here is a char so if we go to this class here and we take a look at it we have this element type here which we know is char if we go down to the constructor that actually takes in some kind of buffer of characters which looks like what this thing is here so we have this kind of Const LM pointer and LM of course is a Const our pointer in this case now some of this stuff applies to earn the debug mode and other stuff replies to release mode we'll take a look at that a bit later but the main function here is a sign which takes in the actual pointer being that const char pointer which is in fact the word Cherno here and then also account which in this case would be 6 because Cherno is 6 characters if we go into this assign function it might be a little bit difficult to find the right one but in this case it would be the one that takes in that element that of course redirects to something else and does some some extra conversion stuff but if we drill down even more and we find the right one here which is actually just a little bit above here you can see this is the one that takes in that pointer and then the count of how many characters it is there's this little if statement here and this basically says that if the count meaning the size of the string is below a certain value which is this kind of reserved size which we'll check out in a minute then actually what happens is we simply get what looks like to be an already existing buffer of memory and simply move our characters over into that buffer of memory and that's it and then we just return it so there's no allocation at all whereas here in this case if it doesn't pass this if statement test we actually call this reallocate for function which as you can see eventually over here actually calls an allocate function and that allocate function will in fact hit the new operator and will cause a heap allocation let's go back a little bit here what is this my res variable it's the current storage reserved for string so in other words it's just that reserve size so anything below this or equal to this will constitute a small string and if we take a look at where this is actually set inside here you can see that it gets set to buffer size minus 1 and buffer size for us is this scary Val buff's I which is in fact a constant expression and in this case it's actually going to be 16 and we eventually subtract one so we have 15 meaning 15 is that maximum amount of characters that constitutes a small string so that's kind of all the code in just a little bit kind of looking at how everything works here and if I go back to that assign function whichever one it is then you can see that the presence of this if statement does in fact result in nor heap allocations for a string that is 15 characters or smaller and we can of course test this out so what I like doing is writing this little operator new here we'll just take in the size here this will just simply return a malloc with the right size here the benefit here is that we can actually put a breakpoint and I'll also add some custom code here to actually print this so we'll just say allocating and then size and then bytes yeah okay so let's not worry about this let's put a C in get so that if we don't click close our console and then let's launch this alright so check this out we actually allocate 8 bytes even though our string clearly is below 15 characters that's another little quirk with visual geo string class it's basically something that only happens in debug mode but if we switch to release mode and we launch this then you'll see we have Noor allocations at all but if I expand this and make this a little bit more than these 6 characters so 7 8 9 10 11 12 13 14 15 so that's exactly 15 characters let's take a look at that still nothing no allocations but the moment I go into 16 characters you can see we allocate 32 bytes so it immediately snaps up to this predefined value here with just one character less we have absolutely no heap allocations but once we tip over to 16 characters or more we allocate memory on the heap and that's basically all there is to say for this video if if you're in care about any more details you can pretty much stop watching now just know that in C++ and specifically in this implementation of the C++ standard library which I'm using Visual Studio 2019 here any string that is 15 characters or less does not cause a heap allocation and in that sense it's an optimization it's more efficient it's faster now let's quickly take a look at what on earth happens in debug mode that caused an allocation we'll put a breakpoint here and I'll also take this back to Cherno and we'll check it out all right so this allocation comes from external code let's just show that external code and let's go up to here which is probably going to show us what we want to see I'll just make some more room here there it is contain a proxy pointer so this is what causes the allocation now this container proxy pointer you can see is equal to this rebind Alec T situation which is actually a template argument for this class or I don't even know what this is to be honest it's so complicated but basically the point of this whole thing is that you can see that this is only the case if the iterator debug level is not zero because in that sense this part of the code gets compiled however if the debug level is zero which is what it would be in release mode you can see that instead this get proxy allocator is a fake allocator and this fake allocator does absolutely nothing so this container proxy 12 which eventually actually causes that new allocation to take place so it turns out that it's a little bit of a red herring in the sense that it's a debug only allocation and it's not in fact small string optimization not working it's just the case for all debug strings okay so that's a small string optimization strings that are sufficiently small will not cause any heap allocations and thus will result in your program running faster it's a cute little trick they worked into the C++ standard library to make it that little bit faster how thoughtful anyway hope you guys enjoyed this video if you did please hit that like button drop a comment below with what you would like to see next I'm about to come out with a whole bunch of like data structures and patterns and all of that kind of stuff videos that's what I'm kind of more or less gravitating towards with the C++ series don't forget to check out hosting up up to 91 percent off web hosting and I will see you guys next time goodbye [Music]
Info
Channel: The Cherno
Views: 44,727
Rating: 4.9615936 out of 5
Keywords: thecherno, thechernoproject, cherno, c++, programming, gamedev, game development, learn c++, c++ tutorial, game engine, how to make a game engine, game engine series, sso, small string optimization
Id: S7oVXMzTo4w
Channel Id: undefined
Length: 13min 13sec (793 seconds)
Published: Sat Feb 15 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.