Reviewing LevelDB, Part IV: On std::string, buffers and memory management in C++
Join the DZone community and get the full member experience.Join For Free
this is a bit of a side track. one of the things that is quite clear to me when i am reading the leveldb code is that i was never really any good at c++. i was a c/c++ developer. and that is a pretty derogatory term. c & c++ share a lot of the same syntax and underlying assumption, but the moment you want to start writing non trivial stuff, they are quite different. and no, i am not talking about oo or templates.
i am talking about things that came out of that. in particular, throughout the leveldb codebase, they are very rarely, if at all, allocate memory directly. pretty much the whole codebase rely on std::string to handle buffer allocations and management. this make sense, since raii is still the watch ward for good c++ code. being able to utilize std::string for memory management also means that the memory will be properly released without having to deal with it explicitly.
more interestingly, the leveldb codebase is also using std::string as a general buffer. i wonder why it is std::string vs. std::vector<char>, which would bet more reasonable, but i guess that this is because most of the time, users will want to pass strings as keys, and likely this is easier to manage, given the type of operations available on std::string (such as append).
it is actually quite fun to go over the codebase and discover those sort of things. especially if i can figure them out on my own .
this is quite interesting because from my point of view, buffers are a whole different set of problems. we don’t have to worry about the memory just going away in .net (although we do have to worry about someone changing the buffer behind our backs), but we have to worry a lot about buffer size . this is because at some point (80kb), buffers graduate to the large object heap, and stay there. which means, in turn, that every time that you want to deal with buffers you have to take that into account, usually with a buffer pool.
another aspect that is interesting with regards to memory usage is the explicit handling of copying. there are various places in the code where the copy constructor was made private, to avoid this. or a comment is left about making a type copy-able intentionally. i get the reason why, because it is a common failing point in c++, but i forgot (although i am pretty sure that i used to know) the actual semantics of when/ how you want to do that in all cases.
Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Incident Response Guide
Tactics and Strategies on Software Development: How To Reach Successful Software [Video]
Designing a New Framework for Ephemeral Resources
Demystifying SPF Record Limitations