String template library for efficient concatenation

Is there a blitz ++ library equivalent for strings (i.e. a library that improves the performance of string construction / manipulation by delaying the construction of the string until the entire expression has been read)?

Blitz ++ improves the speed of matrix / vector operations by metaprogramming patterns by creating a "syntax tree" at compile time from expressions like A + B + C , and then evaluating the syntax tree. This method could, for example, improve string concatenation performance, because after looking at an expression like s1 + s2 + s3 size of the result will be known, so that memory allocation and copying can then be performed in one step, rather than first allocating memory for s1 + s2 . copying, allocating memory for (s1 + s2) + s3 , and then copying again.

+4
source share
1 answer

I know that QString uses expression patterns to determine the final string size and efficiently distributes this in advance. I think the base code is not so difficult and can be used to work with most existing string classes. From the Qt 4.8 manual:

QStringBuilder uses expression patterns and overrides the '%' operator, so when you use '%' to concatenate strings instead of '+' , several concatenations of substrings will be delayed until the final result is assigned to QString . At the moment, the amount of memory required for the final result is known. Then the memory allocator is called once to get the required space, and the substrings are copied into it one by one.

Take a look at the wiki for a technical example.

Note that this interferes with things like decltype(a+b) or auto c = a+b , where the operator+ expression pattern is used, since the return type is a proxy type, not the original type a or b .

+1
source

Source: https://habr.com/ru/post/1445367/


All Articles