There are many different bottle necks in C++ networking applications, many of them related to the architecture, the way system calls are used, the way application generates the data.
However at certain point applications reach the point when memory allocation becomes the bottle neck, this is especially relevant for the cases where text strings are widely used. And this is usually the case for all networking and web applications that finally need to deal with strings.
The old and good
std::string has one limitation - it requires memory allocation
for the chunk it represents. This is actually correct for almost any kind of string around whether they reference counted, immutable - all they allocate chunks
Consider situation, where I need to search against path
some hierarchical structure like
std::map that uses
std::string as the key.
So I need to find a path using after fetching two keys
So basically for the function:
void find_path(std::string const &str);
Would need to:
- Create a string
- Create a sub-string
- Create a sub-string
So I've got three memory allocations and this is not matter whether these strings are mutable or immutable, reference counted or not.
CppCMS should handle such code in multiple places so how it solves this problem?
Create a special object
cppcms::string_keyit holds inside a full
std::stringbut also can be created explicitly from a pair of two
char const *pointers that define a text in range
What is important that in in the second case object does not copy the data but only references it and I'm as a user responsible to ensure that the text range remains valid as long as I use this object.
Now we add an overload for the function above:
void find_path(char const *str); void find_path(std::string const &str);
And when we split the text into parts we use only "unowned-strings" such that creation of a strings
barwould not require memory allocation at all.
It is a general line, but there are much more interesting tricks to deal with 0 or few allocation strings and streams, like creation of a small memory pools that release all strings in once, like using on stack storage for small text chunks and much more.
This technique is widely deployed in CppCMS 0.99.9 code and had allowed to improve performance of page generation by up to twice in some cases.
Now these tricks should be done careful as they rely on manual memory
management so, unless you do something many-many times or you detect
some bottle-neck in the application still stick with
it is usually good enough. Memory allocation today is very fast, just
don't abuse it.