向量的小字符串优化

向量的小字符串优化

本文介绍了向量的小字符串优化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有几个(全部?)STL 实现实现了小字符串"优化,而不是存储通常的 3 个用于开始、结束和容量的指针,字符串将在用于指针的内存中存储实际字符数据,如果 sizeof(字符)<= sizeof(指针).我处于一种情况,我有很多元素大小 <= sizeof(pointer) 的小向量.我不能使用固定大小的数组,因为向量需要能够动态调整大小并且可能会变得非常大.但是,向量的中位数(不是平均值)大小仅为 4-12 字节.因此,适用于向量的小字符串"优化对我来说非常有用.这样的事情存在吗?

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?

推荐答案

你可以借用 SmallVector 来自 LLVM 的实现.(仅标头,位于 LLVMincludellvmADT)

You can borrow the SmallVector implementation from LLVM. (header only, located in LLVMincludellvmADT)

这篇关于向量的小字符串优化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 14:11