How to make your STRINGS FASTER in C++!

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 พ.ย. 2024

ความคิดเห็น • 379

  • @abhikjain
    @abhikjain 4 ปีที่แล้ว +178

    When using clang with c++17, the string was stored on stack, not heap. When the size of string exceeded beyond 32 bytes that it was stored on the heap. So those of you getting 0 allocations, just increase the size of your string.

    • @minhnguyennhat5112
      @minhnguyennhat5112 2 ปีที่แล้ว +10

      Gosh this was soo useful, I spent hours trying to figure out why I cannot override the new operator

    • @michaplucinski142
      @michaplucinski142 ปีที่แล้ว +3

      it's actually when it is longer than 15 characters. Which is not the same as 32 bytes =)

    • @man-z
      @man-z ปีที่แล้ว +4

      Its look like to small string optimization

    • @ultimatesoup
      @ultimatesoup ปีที่แล้ว +1

      That's not true. You're probably thinking about small string optimization, but that's not applicable in any compiler when strings get to a certain size

    • @abhinavrathod8876
      @abhinavrathod8876 6 หลายเดือนก่อน

      @@michaplucinski142 actually longer than 16 characters. Well at least on GNU gcc on Ubuntu

  • @knoriy
    @knoriy 5 ปีที่แล้ว +116

    I'm glad this series got a sponser. The stuff I've learnt have been very helpful.

  • @tekno679
    @tekno679 5 ปีที่แล้ว +172

    Breaking News: C++17 reinvents struct {char*, size_t}

    • @jamespong6588
      @jamespong6588 5 ปีที่แล้ว +15

      Jakub Rozek better buy these new books

    • @VarunSingh000
      @VarunSingh000 4 ปีที่แล้ว

      ​@Peterolen thx! i see both sides of the picture now ^_^

    • @godDIEmanLIVE
      @godDIEmanLIVE 4 ปีที่แล้ว +6

      No, it just offers a convenient standard function for people to use. struct{ char*, size_t} was always possible.

    • @simonfarre4907
      @simonfarre4907 3 ปีที่แล้ว +3

      Yeah this was never the case though, that they reinvented something, or did something "basic" - it was a design goal laid out over a decade ago. string_view was meant as a pre-cursor to the ideas of a views (same notion as ranges and their "views", non-owning, handles of immutable data) - something which is generally wide adopted concept in any new decent programming languages where lifetimes (something which c++ always focused on) & ownership is more explicit in order to squeeze out performance in a much more ergonomic way. And as above commenter mentioned it also provides an interface, so that any string type can be passed to a function taking a string_view, along with utility functions provided. So a std::string implicitly converts to string_view, a {char, len}, converts, but also, unicode strings convert etc etc. Again, the idea is about thinking about ownership (something which was explicitly a design goal in Rust from the start for instance), not just some rando data type.

    • @mateusz-czajkowski
      @mateusz-czajkowski 3 ปีที่แล้ว

      I don't get it. People say that manipulating char pointer = undefined behaviour. So what is the purpose of storing string literal in char pointer?

  • @antiHUMANDesigns
    @antiHUMANDesigns 2 ปีที่แล้ว +9

    Some years ago, I wrote my own string class that is a perfect clone of the functions of std::string, except for one major difference: The string is of static size, instantiated through a template argument. This makes it be allocated without an internal pointer to the heap, so it's good for structs and such that don't need to be serialized before saving to file, and it of course stays on the stack if that's where you allocated it.
    Because it can't change size, it automatically truncates stuff that goes out of bounds, though. Really good for short strings, basically, such as ID tags.
    It's tradeoff because you're wasting whatever bytes you're not using of a fixed-size string, though.

  • @ultimatesoup
    @ultimatesoup ปีที่แล้ว +1

    Important to note, that while string view doesn't allocate, if you have a lot of string literals and rvalues, using string view will cause binary bloat because they have to be explicitly stored in the .data portion of the binary. Just a tradeoff to be aware of

  • @haywardhaunter2620
    @haywardhaunter2620 4 หลายเดือนก่อน +2

    The description of the problem is correct at a high level, and the advice to use string_view is terrific. But the demonstration of the problem is flawed. The first clue is that the allocation size was only 8 bytes, but you'd need at least 14 bytes to store the string. The allocations detected by overriding operator new are not the string buffers themselves. The compiler was set for 32-bit x86 code, so 8 bytes is likely two pointers or a pointer and a size_t. Furthermore, if you try the same experiment in a Release build, there are zero allocations. I believe the 8-byte allocations seen are actually to store extra data to assist with debugging, and are a feature of Microsoft's implementation of the standard library. The library uses the small-string optimization, and apparently the strings in this example are all small, since the buffers are inside the string objects (on the stack) instead in a buffer on the heap. In real code, when you're dealing with strings that a little longer, there will be heap allocations to store the character data, so the advice is still good.

  • @StefansKanal12
    @StefansKanal12 5 ปีที่แล้ว +8

    You can also do this:
    using namespace std::literals;
    auto s = “Hello“sv;
    s will be a string_view

  • @GGodis
    @GGodis 5 ปีที่แล้ว +189

    It's not entirely true that std::string is always using the heap allocations. Why no mention about the "Small String Optimization" (SSO) in the std::string?

    • @viniciusmagalhaeshorta7154
      @viniciusmagalhaeshorta7154 5 ปีที่แล้ว +22

      Maybe if changing from Debug to Release mode?

    • @Lisekplhehe
      @Lisekplhehe 5 ปีที่แล้ว +1

      Thank you, this is a very good point.

    • @seditt5146
      @seditt5146 5 ปีที่แล้ว +4

      GGodis. What he was using was a small string was it not? Even with the SSO it was not allocating those 8 bytes for the characters themselves.

    • @0000xFFFF
      @0000xFFFF 4 ปีที่แล้ว +12

      I guess he listened... he just made the video.

    • @aldeywahyuputra5719
      @aldeywahyuputra5719 4 ปีที่แล้ว +1

      0xC0LD yep, he knows that the optimizations usually opts for stack alloc but he purposely used debug mode for heap allocation demonstration.

  • @jroberts3425
    @jroberts3425 4 ปีที่แล้ว +9

    Liking the focus on optimisation. Keep it coming. Not sure if you cover it elsewhere but it would be great if you could cover optimisation of matrix operations. E.g. rotations of a 3 vect , with same focus on making it faster (but readable).

  • @shadowmil
    @shadowmil 5 ปีที่แล้ว +49

    Should mention that the danger of using std::string_view. You should never take a copy of std::string_view. Using it for a function parameter or a local variable is normally fine. But use extreme caution if you're using it as a data member or returning it from a function. Doing so means there is a good chance the memory std::string_view points to could become invalidated.

    • @SteinCodes
      @SteinCodes 5 ปีที่แล้ว +3

      This video largely reminded me of other problems with string_view.
      A common problem with it is, if you need std::string from it, or if there is a chain of strings it becomes a hassle for the compiler to properly optimize it.
      Such as SSO, string as hex etc...
      TBH having a simple CoW implementation for const refs is the only use case. But it's definitely a good use case. (Yes I usually use my own CoW implementations..) :P

    • @alexloktionoff6833
      @alexloktionoff6833 2 ปีที่แล้ว

      @@SteinCodes don’t CoW implementations have issues with multi threading scalability ?

  • @DarshanSenTheComposer
    @DarshanSenTheComposer 5 ปีที่แล้ว +23

    Absolutely love these performance optimization videos! Keep up this great work. :)

  • @intom1639
    @intom1639 3 ปีที่แล้ว +2

    Working properly with memory is very important for performance. Please share more tips on that. Thank you so much!

  • @michaplucinski142
    @michaplucinski142 2 ปีที่แล้ว +1

    5:35
    I think it will allocate memory
    because we sent a const char*
    so it has to make implicit convertion
    and it will actually allocate space for that string
    I'm just learning, don't scream at me if I am wrong =(

  • @MO-fg2cm
    @MO-fg2cm หลายเดือนก่อน

    What I learnt :
    1. Way to track amount of memory allocation by overloading new operator
    2. c_str vs strings and SSO

  • @chrisstone-streetlightinte5629
    @chrisstone-streetlightinte5629 5 ปีที่แล้ว +4

    Since I am working on my own little 2D engine, optimization is the area that interests me the most so I'm on board with more of these types of videos.

  • @amallkrishna
    @amallkrishna 5 ปีที่แล้ว +20

    Please do some videos on STL and their components - iterators, containers, functions and algorithms!
    And maybe a whole series on algorithms and data structures! That'd be fantastic to learn from you!

  • @sergeykolesnik1171
    @sergeykolesnik1171 5 ปีที่แล้ว +1

    You can also write a custom allocator that will allocate memory on the stack for your length-limited string. That would be `using StackString = std::basic_string;` And you also should mention, that you CAN use string_view or const char, but you definetely would be fine with std::string.

  • @matanp4713
    @matanp4713 4 ปีที่แล้ว +2

    instead of:
    const char* name = "Yan Chernikov";
    you can do:
    std::string_view name("Yan Chernikov");
    This way you will reduce your allocations back to zero AND you will save on the C++ convention and not use C convention. For firstName and lastName use name.data().

  • @VoidloniXaarii
    @VoidloniXaarii ปีที่แล้ว

    Fascinating digging behind the curtain and passion for efficiency gfx programmer core, thank you very much

  • @clodgozon3968
    @clodgozon3968 5 ปีที่แล้ว +1

    I love talking about memories and performance with a friend

  • @dameck9570
    @dameck9570 4 ปีที่แล้ว +7

    Wow, and I wondered why my obj file loader was so inefficient with larger models and about 6 strings per line😇 Good Video👍

  • @עומרפריאל
    @עומרפריאל 4 ปีที่แล้ว

    I think the videos dealing with optimization really help,
    and there is not much information on this topic on the internet.

  • @adsCrazyDiamond
    @adsCrazyDiamond 2 ปีที่แล้ว +4

    I think strings have been greatly optimized with c++... no matter how large I make this string I get 0 allocations... it does tally allocations and print alloc sizes but only if i use the new command to allocate memory myself. And then only if I print the allocations before the new object goes out of scope... std::string simply isn't allocating any memory on the heap. Maybe it would in larger programs but this example presented here really doesn't work anymore.

  • @alexanderhugestrand
    @alexanderhugestrand 5 ปีที่แล้ว

    Memory alignment is a pretty technical area which could be explotied for faster string operations. It's much faster to read a 32 bit integer and mask out each byte than to read one byte at a time. If you are someone who likes coding down to the metal, you could use inline assembler and use the SIMD/SSE instruction set to read 128 bits at a time. With some clever optimizations (instruction pairing) you can do operations on 256 bits per CPU clock cycle. I've never tried that on strings myself, but I sure will some day soon.

    • @jamesmnguyen
      @jamesmnguyen 5 ปีที่แล้ว

      Any resources on instruction pairing? Or just Google it?

    • @alexanderhugestrand
      @alexanderhugestrand 5 ปีที่แล้ว

      @@jamesmnguyen I think Google is the best way to go. It was a long time ago I wrote any assembler, except some recent SIMD experiments. If the same rules as during the 90's still hold true, it's pretty simple. Just make sure that one instruction doesn't depend on the result of the previous one. The CPU will fetch two such independent instructions at once and execute.

  • @tetraquark2402
    @tetraquark2402 5 ปีที่แล้ว +48

    It does look like you are just going back to C.

  • @anik301
    @anik301 2 ปีที่แล้ว

    Amazing work, you're just THE mentor of mine. Thaink you for your work and be aware that yor videos made me pass my cs classes during my studies. Thank you so much.

  • @pvorisek2553
    @pvorisek2553 5 ปีที่แล้ว +1

    I like your presentation of these kind of problems and making C++ programs run faster. Keep up like this. You are really helping me.

  • @platinoob__2495
    @platinoob__2495 4 ปีที่แล้ว

    the beat at the intro is just great

  • @alexloktionoff6833
    @alexloktionoff6833 2 ปีที่แล้ว

    FORTH language had ‘string views’ from the very beginning in the previous century. Actually all ‘strings’ were tuples of pointer+len. I was wondering why, but now 50 years later I see it was very clever!

  • @QuadHealer
    @QuadHealer 4 ปีที่แล้ว +2

    Cool! I have been writing C/C++ professionally since 1995, and I have never heard of std::string_view. Thank you for that C++17 hint! I must however warn against using the misleading use of spaces as you do:
    const char* name = "Yan Chernikov";
    The "correct" way of writing this is:
    const char *name = "Yan Chernikov";
    because beginners would otherwise get confused if you e.g. declare the following variables:
    const char* name, lastname;
    'name' is a pointer to a const char (as expected), but ‘lastname’ would just be a ‘const char’, which beginners would miss if they think the '*' belongs to the type, and the same goes for references. Of course you know this, but it is just good practice to not confuse beginners with spacing. Anyway, I just wanted to thank you for this hint.

    • @Zantom07
      @Zantom07 4 ปีที่แล้ว +2

      Nice comment! It's a bit of a holy war which side the * should go, but it's generally agreed upon to simply not use the comma notation for declaring variables, and the confusion you mention is one of the reasons why.
      Personally I think it can be decently argued that
      const char *name = "Yan Chernikov";
      is good C style, but *not* good C++ style, where you might prefer
      const char* name = "Yan Chernikov";
      instead.
      The reason for this is the difference in how C and C++ handles types. C is weakly typed, and pointers are not their own types. In C, 'name' would be a pointer variable to a value/array of type 'const char'. Thus it makes sense that the * belongs to the variable, and not the type. In C++, which is strongly typed, 'name' would be a variable of type 'const char*'. Thus it makes sense that the * belongs to the type, not the variable. It's a subtle distinction, but it's particularly important for when you interact with C++-only features such as references and templates.
      All that said, it probably doesn't matter that much :) Some people even prefer putting spaces both sides of the *!

  • @maniaharshil
    @maniaharshil 11 หลายเดือนก่อน

    Nice job cherno !! we need daily something out of box like this!! performance is very very useful in time critical apps, please do more videos on these topic to improve performance. Thanks in advance and keep it up!! 😍

  • @AL-kn4yx
    @AL-kn4yx ปีที่แล้ว

    Thank you for making these videos.

  • @zyghom
    @zyghom 2 ปีที่แล้ว

    I always knew using strings is bad, now I know exactly why - perfect!

    • @zyghom
      @zyghom 2 ปีที่แล้ว

      @Artem Katerynych it is not about bad or good - strings are in another part of the memory right? unlike other variables strings cannot release this memory for other variables or functions. Or I am wrong (that can happen as well)

    • @zyghom
      @zyghom 2 ปีที่แล้ว

      @Artem Katerynych yes, I use C/CPP only for ESP32 or other small MCU where every byte counts ;-)

  • @derschwarzejulian7201
    @derschwarzejulian7201 5 ปีที่แล้ว +9

    I definetly started with the wrong language. C/C++ is so interesting it completely different than Java or etc. If I master it one day I will sure be happy about it. Thanks for the videos!

    • @superscatboy
      @superscatboy 3 ปีที่แล้ว +3

      "C/C++" isn't a language, it's two languages, with a forward slash sandwiched between them.

  • @bi0cide697
    @bi0cide697 5 ปีที่แล้ว +33

    Lmao watching this like "Just use C brah"

    • @VictorRodriguez-zp2do
      @VictorRodriguez-zp2do 4 ปีที่แล้ว +3

      The very moment I saw the title I knew this would involve C.

  • @СтаніславДеркач-щ6и
    @СтаніславДеркач-щ6и 3 ปีที่แล้ว

    I'm going to improve my pluses and English with you
    Thanks from Ukraine

  • @WildGrowthJ
    @WildGrowthJ 2 หลายเดือนก่อน

    this is like catnip for my sense of perfectionism

  • @zemlidrakona2915
    @zemlidrakona2915 4 ปีที่แล้ว

    std::string is not always the best choice. Often COW strings (Copy On Write) are better and don't require extra classes and complexity to avoid the allocations if implemented right. There are a lot of things in the standard library that aren't really ideal for the general case. They try to cover too much ground and you pay a price for that. Another example is std::shared_ptr which is twice the size of a normal pointer. The simple way is just to a base class with a reference count which is smaller and generally faster.

  • @tristanbellingham6759
    @tristanbellingham6759 5 ปีที่แล้ว +152

    So to make C++ faster we should just use C? hmmm

    • @johnmcguiness3519
      @johnmcguiness3519 4 ปีที่แล้ว +14

      @Marc Dirven You can modify the string if it's not const. :) So, yes.

    • @johnmcguiness3519
      @johnmcguiness3519 4 ปีที่แล้ว +15

      @Marc Dirven No, I'd just use a char *, which is a C "string" (or to be more precise, a pointer to a char array).

    • @johnmcguiness3519
      @johnmcguiness3519 4 ปีที่แล้ว +11

      @Marc Dirven I guess it depends what level of abstraction you are comfortable with. Using pointers and functions like strlen, strncpy, snprintf, strcat and so on to manipulate strings are just as "friendly", if not more to me. You also shouldn't get bugs if you know what you're doing.

    • @johnmcguiness3519
      @johnmcguiness3519 4 ปีที่แล้ว +12

      @Marc Dirven That's the point, and I do use plain C a lot for greater efficiency. I use C++ where OOP makes more sense and I need to make classes. Many great pieces of software are written using this hybrid approach.

    • @deedoubs
      @deedoubs 4 ปีที่แล้ว +13

      My favorite is when Java/Java Script/C# devs fight against the garbage collector.

  • @FreestateofOkondor
    @FreestateofOkondor 5 ปีที่แล้ว +2

    I love these optimization videos.

  • @CaleMcCollough
    @CaleMcCollough 5 ปีที่แล้ว

    I've got everyone tapped out when it comes to string processing. Script2 uses ASCII Data that grows from stack to heap. I wrote the world's fastest number printing algorithm, which is mainly for integers but it also optimizes pretty much every other FtoS like Grisu and Ryu. Script2 features the Uniprinter (Universal Printer), which can print to any stream source. Unlike the C++ std library, the Uniprinter handles char32_t or char16_t as a sain person would.

    • @AlFredo-sx2yy
      @AlFredo-sx2yy 2 ปีที่แล้ว

      im surprised a comment with a claim such as that has gone unoticed for over 2 years damn. So uhm, i did a bit of research regarding what you said about you inventing the world's fastest number printing algorithm, i did find it a bit odd that there's barely anything about you out there. I mean, i've see some stuff like the puff algorithm and the Kabuki Starship thing but when i search for those things barely any results come up, and when i asked some peers they've never heard about anything about this. Im just wondering if you could please go a little bit more in depth about the algorithm you mentioned and explain some specifics.

  • @thestarinthesky_
    @thestarinthesky_ 4 ปีที่แล้ว

    Thank you so much. That's exactly what i wanted to know. I appreciate that!

  • @puppergump4117
    @puppergump4117 2 ปีที่แล้ว

    I guess if it's the app is loaded with string operations you'd want less allocations, but then again the std::string is optimized and compilers can always do this cleaning on their own.

  • @AlexisHernandez-fv5vg
    @AlexisHernandez-fv5vg 5 ปีที่แล้ว +1

    Video for performance while comparing strings 😊

  • @captainfordo1
    @captainfordo1 ปีที่แล้ว +1

    Moral of the story: never use std::string for performance-critical tasks like games

  • @teiroberts5330
    @teiroberts5330 5 ปีที่แล้ว +1

    Yes! Another C++ video. It really sets characters

  • @gouthamkancherla5571
    @gouthamkancherla5571 ปีที่แล้ว

    void print(const std::string& s) {
    std::cout

  • @bludgeon1081
    @bludgeon1081 3 ปีที่แล้ว +1

    It might not allocate memory because the string is less than 15 characters. But if it was longer, I think it would allocate.
    3 allocations.

  • @mamadshonazaramonov3010
    @mamadshonazaramonov3010 ปีที่แล้ว

    [Answer] I think it would allocate 5:40

  • @funhuninjastudio
    @funhuninjastudio 5 ปีที่แล้ว +3

    Hey Cherno,can you make a video for *sockets* ?They are a little bit complicated for me.Thank you. :)

  • @codychan4992
    @codychan4992 4 ปีที่แล้ว +1

    Different result, Linux here, the operator new is not called unless firstName and lastName are long enough(>15), C++17 + Linux here.

  • @poorman-trending
    @poorman-trending 3 ปีที่แล้ว +1

    Is there a name for that thing you do with your arms at the begging of each video?

  • @kapilverma2668
    @kapilverma2668 5 ปีที่แล้ว +3

    Memory Mapped files would be good.

  • @feschber
    @feschber 5 ปีที่แล้ว +97

    So the TL;DR of this video is just: Use const char instead of std::string just like good old c

    • @nexusclarum8000
      @nexusclarum8000 5 ปีที่แล้ว +21

      ... tbh I think a lot of people are starting to wake up and realize it's not as simple as "more abstraction is better". sometimes it is. sometimes it isn't. oop in general has been a terrible idea that just won't die. I mean, I get why they went that route. In C for example in order to write clean code what inevitably happens is people start putting function pointers in structs with their data (methods). They realize still ppl are mucking about with data members of that struct that are really only intended for "internal use" so they want data encapsulation. They use translation units, static functions etc to emulate "private members". But, it's a bit clunky... OOP was made up to basically streamline this.... but honestly.... they took it too far... inheritence and all that garbage just tightly couples everything together too much and really hurts the promises of "re usability" etc...

    • @feschber
      @feschber 5 ปีที่แล้ว +19

      Nexus Clarum Well it’s always a matter of knowing what to do and what not to do. You can write a lot messier code in non oop than in oop if you don’t know what you are doing

    • @creature_of_fur
      @creature_of_fur 5 ปีที่แล้ว +19

      @@nexusclarum8000 IMO abstraction is great. It allows you to not think about the lower levels, so you don't get distracted.
      Another thing is that abstraction has to be done right - methods that make sense and are a bottleneck only when you expect them to be, encourage the right style of code.
      Of course, when the project is old, you inevitably need to change something about those lower levels, so you either have to give up these rules, or change the interface, which will probably lead to a horrible mess and tons of bugs...
      Maybe I just like OOP because my first language was Java, idk

    • @markotikvic
      @markotikvic 4 ปีที่แล้ว

      @@creature_of_fur Abstraction is context dependent. Meaning, you can't abstract code in every project, in every library, in every file. A lot of applications require low level, not abstracted code specifically for clarity sake.
      "Abstraction is great" means nothing in real world, where not everything is always trivial and most answers start with "Well, it depends..."

    • @wile9763
      @wile9763 4 ปีที่แล้ว

      Nope. You have to pay for const char*s somewhere. First, initialized const char*s are just immutable string literals. The string literal has to be stored somewhere. Most likely, it will stored in the data block of your binary. Also, memory allocation can be abstracted further by using allocator classes as in std::basic_string. It's worth noting that malloc implementations are really smart today and likely are using arena style allocation to avoid frequently extending the necessary amount of memory needed. If it feels like this is getting complicated, it's because it is complicated. C++ gives the user capability to care as little or as much about performance as they'd like.

  • @cankarkadev9281
    @cankarkadev9281 5 ปีที่แล้ว

    Thanks so much! Today im gonna benchmark my engine :)

  • @Spartan322
    @Spartan322 5 ปีที่แล้ว

    tbh, the C++ standard library does tend to overthink things and then completely miss the mark on trying to be bare metal optimized, I love the STL in a way but only because it has functionality, not because its actually that efficient.

  • @alimaster89
    @alimaster89 5 ปีที่แล้ว

    Hey, very cool video. I wonder about what is the best way to convert strings into other types (float, int, double). I know there are different ways to approach that but its hard to tell which is most efficent. Would love If you can speak about this topic in one video.

    • @alimaster89
      @alimaster89 5 ปีที่แล้ว

      @Peterolen If you know the answer then answer. If you dont know then dont answer.

  • @ThatJay283
    @ThatJay283 7 หลายเดือนก่อน

    not strings, but i doubled the performance of my games renderer just by switching from a forward renderer to a deferred renderer. like it's now efficient to the point where i can run it on my laptop, which has an IGPU, and it'll actually run well while still looking really good.

  • @ron5075-n6n
    @ron5075-n6n ปีที่แล้ว

    I built a code editor and for some reason I uses std::string for the lines of texts. As the text on my code editor grows, the slower it gets. Thanks for this vid.

  • @FernandoJose-kt6gt
    @FernandoJose-kt6gt 11 หลายเดือนก่อน

    Thanks for the nice video explaining. I was just refactoring some basic, simple code from std::string to std::string_view and was disappointed to find converting to an int with e.g. std::stoi is not supported and there's apparently no good alternative (std::from_chars would need a custom made wrapper).

  • @brunomarques5258
    @brunomarques5258 3 ปีที่แล้ว

    With your help a got a string serializer running 10x faster

  • @eventhisidistaken
    @eventhisidistaken 4 ปีที่แล้ว

    On the one end of the spectrum, we have people promoting going back to functional programming for maximum efficiency, and on the other end of the spectrum, we have gigabyte XML transfers for machine to machine communication where it's idiotic to use ASCII. I'm glad my degrees are EE and not schizophrenic CS.

  • @cmdlp4178
    @cmdlp4178 5 ปีที่แล้ว +1

    When you overload operator new to call malloc, you should also overload operator delete to call free! Otherwise you write invalid code with undefined behavior!

    • @_slier
      @_slier 4 ปีที่แล้ว

      maybe this is the reason why my allocation count is always at 0

  • @Yupppi
    @Yupppi ปีที่แล้ว

    I came here from the video "small string" and thinking "wait none of these should make the heap allocation, what's going on?"
    Question: let's assume you have multiple names and don't know them. Would this be possible to do using the space between the names as the divider or is that operation one of those costly things you want to avoid and there's no getting around that problem?

  • @srivallabhavaidya2991
    @srivallabhavaidya2991 ปีที่แล้ว

    So, you mean. Instead of string should we use constant char* ?

  • @kairos1334
    @kairos1334 5 ปีที่แล้ว +3

    What about SBO cherno. I guess small strings are typically allocated on the stack itself rather than heap so as to save the cost of memory allocation and in your case (9 bytes ) SBO should have got in automatically. Why isn't that happening. Plz correct me if I am wrong somewhere.

    • @oracleoftroy
      @oracleoftroy 5 ปีที่แล้ว +1

      A Release build would have allowed SSO to kick in and his example program would report 0 allocations.

  • @handlethissonny
    @handlethissonny 15 วันที่ผ่านมา +1

    "yes or no, write a comment below NOW!" xD roflmao

  • @ThatJay283
    @ThatJay283 7 หลายเดือนก่อน

    7:10 my first method to get to zero allocations would be using C strings :)

  • @Xxp0r
    @Xxp0r 5 ปีที่แล้ว +16

    Can you please cover Exceptions and why you don't use them?

    • @agfd5659
      @agfd5659 5 ปีที่แล้ว +4

      They are useless. Search for jon blow's rant on TH-cam to see the reasons why.

    • @kristupasantanavicius9093
      @kristupasantanavicius9093 5 ปีที่แล้ว +1

      @dvorak il The most useful feature in C++ are templates and non nullable references.

    • @notnullnotvoid
      @notnullnotvoid 5 ปีที่แล้ว

      @@kristupasantanavicius9093 There is no such thing as a "non-nullable reference" in C++. References can be null just as well as pointers can, because they can be created from pointers and are in fact just pointers with different syntax.

    • @oracleoftroy
      @oracleoftroy 5 ปีที่แล้ว +1

      @@notnullnotvoid No, the moment you deference a null pointer, your program is undefined and not considered C++ by the standard.

    • @kristupasantanavicius9093
      @kristupasantanavicius9093 5 ปีที่แล้ว

      @@oracleoftroy The caller is doing something undefined, since he is performing a dereference. The function being called, which contains reference parameters, is completely valid code. I haven't encountered many cases, if any, where a reference points to a nullptr. Its probably because you have to derefence a pointer when converting it to a reference, which stops you for a moment to think about the possible state of the pointer.

  • @JazzBlues97
    @JazzBlues97 ปีที่แล้ว

    What about optimizing the concatenations of strings? Each concatenation done allocates on the heap 2 times. How to do an easy (and safe) concatenation of two char*?

  • @עומרפריאל
    @עומרפריאל 4 ปีที่แล้ว

    With that possible, can you create a video about working with files?

  • @Kludgeware
    @Kludgeware 5 ปีที่แล้ว

    Love the cpp content!

  • @antiHUMANDesigns
    @antiHUMANDesigns 2 ปีที่แล้ว

    2:22 is a hack that gets you past the intro stuff.

  • @bies_moron4404
    @bies_moron4404 5 ปีที่แล้ว

    Like !
    Video about "String interning" would be helpful !

  • @vtgordo
    @vtgordo 9 หลายเดือนก่อน

    Help! My code won't build/run in 'Release' vs. 'Debug' mode. In 'Release' it says there is no string_view in std. I am using VS2019 and setting C++ to 17. The answer I saw on MS developer's page didn't seem to help. It was about string_view in general - not specific to 'Release' vs. 'Debug'. Even Chat GPT didn't provide an answer! :)

  • @pmvanker
    @pmvanker 4 ปีที่แล้ว

    stack is also in memory, heap is also in memory. in both case cpu need to read memory. so what is the differ ? why stack is faster ?

  • @atrumluminarium
    @atrumluminarium 5 ปีที่แล้ว +3

    GPU compute when tho?

  • @hiren07ec
    @hiren07ec 3 ปีที่แล้ว

    i need video of shorcuts you use in visual studio. i know its silly request but dont want to learn from other 😷

  • @joychen1970
    @joychen1970 ปีที่แล้ว

    Hey, Cheno, which font type you are using in VS, it pretty nice~

  • @real_lrj
    @real_lrj 2 ปีที่แล้ว

    more videos about STL pls.

  • @Puddlestomps
    @Puddlestomps 5 ปีที่แล้ว +12

    Why was it only allocating 8 bytes for the string? I would expect something like one byte per character, but you had 13 characters being allocated in 8 bytes.

    • @TwaritWaikar
      @TwaritWaikar 5 ปีที่แล้ว

      Great question. I think his string is still 14+ bytes in size (including the null char) and with some additional stuff present in std::string. A good way to test all this is to put a breakpoint inside the new operator overload and verify every allocation manually

    • @peribooty
      @peribooty 5 ปีที่แล้ว +9

      I might be wrong about this, but I think it was only 8 bytes because the actual string "Yan Chernikov" is static, so there was no need to allocate it, the actual 8 bytes that *are* being allocated are for the actual string class and it's members/methods

    • @TwaritWaikar
      @TwaritWaikar 5 ปีที่แล้ว +1

      @@peribooty That is more plausible

    • @AdamBucior
      @AdamBucior 5 ปีที่แล้ว +1

      The real answer is that it was't allocating space for the string, but for container proxy (Visual Studio uses it in debug mode for debugging purposes). The storage for the string was't allocated because of SSO (small string optimization). Basically if string is small (in Visual Studio small string is up to 16 bytes), it is stored in a char array which is just a member variable of std::string. This makes constructing and copying small strings faster but increases memory usage of std::string (If string is larger than 16 bytes, this char array remains unused (It's not really wasted entirely, but if you want to go into details just google it)).

    • @rameynoodles152
      @rameynoodles152 5 ปีที่แล้ว +1

      @@AdamBucior I tried this program on GCC and Clang on Linux, and I get 17 allocations, each about 40 bytes. Here is the output:
      Allocating 40 bytes
      Allocating 4 bytes
      Allocating 40 bytes
      Allocating 4 bytes
      Allocating 4 bytes
      Allocating 40 bytes
      Allocating 40 bytes
      Allocating 60 bytes
      Allocating 28 bytes
      Allocating 40 bytes
      Allocating 40 bytes
      Allocating 40 bytes
      Allocating 40 bytes
      Allocating 40 bytes
      Allocating 40 bytes
      Allocating 40 bytes
      Allocating 40 bytes
      Hello
      Allocations: 17
      That's insanely different from Visual Studio.

  • @TusharHaral
    @TusharHaral 5 ปีที่แล้ว

    *Appreciate you sharing these small bits of code which do enhance the code little by little which is generally neglected or we don't think of. Thanks mate.* ✌️

  • @airyfoo3019
    @airyfoo3019 4 ปีที่แล้ว

    which is your font,so beautiful! like it

  • @mucomplex9115
    @mucomplex9115 3 ปีที่แล้ว

    There is no reason for me to not like this video. thumb up!

  • @hiren07ec
    @hiren07ec 5 ปีที่แล้ว +1

    Hey how come you writing code so fast? can you make some video or suggest keys or shortcuts & how it's works?

  • @Just1dead
    @Just1dead 4 ปีที่แล้ว

    Yes

  • @turxandunya4010
    @turxandunya4010 ปีที่แล้ว +1

    Guess l have a string like "hello_1_world". And each iteration l want to increase "1" part here. 2, 3 etc... So l can not use const char* pointer or string_view. l have to allocate new memory in every iteration by using std::string. What do you think about it?

    • @Get_Boros
      @Get_Boros ปีที่แล้ว +1

      with out const, and not with allocation
      char hello[] = "Hello_1_World";
      hello[6]++;
      for (int i = 0; i < sizeof(hello) - 1; i++)
      cout

  • @ahmadmansoori1588
    @ahmadmansoori1588 5 ปีที่แล้ว

    Thank you

  • @DavidRomigJr
    @DavidRomigJr 6 หลายเดือนก่อน

    It caught my attention that the allocation for the 14 char null-terminated C-string of your name was 8 which I normally presume to be bytes, but uh, that shouldn’t fit?

  • @xiangli9588
    @xiangli9588 3 ปีที่แล้ว

    yes

  • @raiantasfin3866
    @raiantasfin3866 4 ปีที่แล้ว +1

    I don't understand, I copied the first code and compiled it with both -std=c++14 flag and later -std=c++17 flag. None of them printed any byte allocation or any number of allocations.

    • @NomoregoodnamesD8
      @NomoregoodnamesD8 4 ปีที่แล้ว

      It's likely your compiler was able to perform small-string optimization on the example and avoid memory allocations

  • @MarkMcDaniel
    @MarkMcDaniel หลายเดือนก่อน

    Main lesson, if you just need a fixed length string, use a c-string.

  • @totallynuts7595
    @totallynuts7595 5 ปีที่แล้ว +1

    All this performance talk will surely link back to the Renderer in Hazel...

  • @shadesofanmol
    @shadesofanmol 5 ปีที่แล้ว +3

    in macOS this code is not showing any allocation, basically our new overload is not being called

    • @luwanV
      @luwanV 5 ปีที่แล้ว +1

      yea same thing for me with g++-9 on OS X

    • @shadesofanmol
      @shadesofanmol 5 ปีที่แล้ว

      @@luwanV yeah

    • @oracleoftroy
      @oracleoftroy 5 ปีที่แล้ว

      Same in MSVC if you compile a release build. Yay for small string optimization!

  • @connorking9217
    @connorking9217 5 ปีที่แล้ว

    Really enjoy this type of content for sure! Looking forward to more of it!

  • @Xxp0r
    @Xxp0r 5 ปีที่แล้ว +9

    Gotta love writing C code to optimise C++ :D!

    • @Xxp0r
      @Xxp0r 5 ปีที่แล้ว +1

      @dvorak il I write both

    • @naxaes7889
      @naxaes7889 5 ปีที่แล้ว +1

      @dvorak il Although it's convenient with overloaded functions, stronger type safety, and occasionally overloaded operators and template programming. References can be nice as well. But other than that, C is the way to go!

  • @efusion1995
    @efusion1995 5 ปีที่แล้ว +2

    yes pls more of this :D

  • @idioticcatfish6927
    @idioticcatfish6927 4 ปีที่แล้ว

    Yes.

  • @Henry14arsenal2007
    @Henry14arsenal2007 2 ปีที่แล้ว

    Why not const std::string_view if we're just printing?

  • @saikrisshnaanichenametla2437
    @saikrisshnaanichenametla2437 5 ปีที่แล้ว

    I love this video