Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

small string optimization for vector?

I know several (all?) STL implementations implement a "small string" optimization where instead of storing the usual 3 pointers for begin, end and capacity a string will store the actual character data in the memory used for the pointers if sizeof(characters) <= sizeof(pointers). I am in a situation where I have lots of small vectors with an element size <= sizeof(pointer). I cannot use fixed size arrays, since the vectors need to be able to resize dynamically and may potentially grow quite large. However, the median (not mean) size of the vectors will only be 4-12 bytes. So a "small string" optimization adapted to vectors would be quite useful to me. Does such a thing exist?

I'm thinking about rolling my own by simply brute force converting a vector to a string, i.e. providing a vector interface to a string. Good idea?

like image 604
BuschnicK Avatar asked Feb 01 '10 16:02

BuschnicK


People also ask

What is short string optimization?

In several implementations, including the Visual C++'s one, the STL string classes are empowered by an interesting optimization: The Small String Optimization (SSO). What does that mean? Well, it basically means that small strings get a special treatment.

Can we store string in vector?

Solution. Use a vector for array-like storage of your strings. Example 4-6 offers a simple example. vector s follow array semantics for random access (they also do a lot more), so they are easy and familiar to use.

How do you find a vector string?

Find String Matches in a Vector or Matrix in R Programming – str_detect() Function. str_detect() Function in R Language is used to check if the specified match of the substring exists in the original string.


1 Answers

Boost 1.58 was just released and it's Container library has a small_vector class based on the LLVM SmallVector.

There is also a static_vector which cannot grow beyond the initially given size. Both containers are header-only.

facebook's folly library also has some awesome containers.

It has a small_vector which can be configured with a template parameter to act like boost's static or small vectors. It can also be configured to use small integer types for it's internal size bookkeeping which given that they are facebook is no surprise :)

There is work in progress to make the library cross platform so Windows/MSVC support should land some day...

like image 111
onqtam Avatar answered Sep 28 '22 06:09

onqtam