我正在使用协议缓冲区和OpensSSL 生成 HMAC,然后 CBC 加密这两个字段以混淆会话 cookie - 类似于 Kerberos 令牌。
Protocol Buffers 的 API 与 std::strings 通信并具有缓冲区缓存机制;我通过将缓存机制放置在线程本地内存中来利用缓存机制,以便在同一线程中进行连续调用;此外,OpenSSL HMAC 和 EVP CTX 也放置在同一线程本地内存结构中(请参阅 这个问题了解我为什么使用线程本地内存的一些细节,以及它即使使用单个线程也能实现大量加速)。
这些 cookie 字符串的生成和反序列化(“我的算法”)使用中间 void *
和 std::string
,并且由于 Protocol Buffers 具有内部内存保留机制,因此我想要“我的算法”具有这些特征。
那么如何实现通用暂存存储器呢?我对 std::string 对象的 rdbuf(streambuf - strinbuf ??) 了解不多。我可能需要将其增长到执行“我的算法”期间遇到的最小常见大小。想法?
我想我的问题是:“是可重复使用的字符串的内部缓冲区,如果是这样,如何?”
编辑(新问题):
似乎经过反思在 Vlad 发表文章后,我确实需要一个 std::string 以及一个 void * c 风格的暂存缓冲区。我的问题是:流行的 stl 字符串实现在不需要内存时会保留内存吗? (我的需求可能会保持在 128 字节到 10 KB 之间)。
I am using Protocol Buffers and OpensSSL to generate, HMACs and then CBC encrypt the two fields to obfuscate the session cookies -- similar Kerberos tokens.
Protocol Buffers' API communicates with std::strings and has a buffer caching mechanism; I exploit the caching mechanism, for successive calls in the the same thread, by placing it in thread local memory; additionally the OpenSSL HMAC and EVP CTX's are also placed in the same thread local memory structure ( see this question for some detail on why I use thread local memory and the massive amount of speedup it enables even with a single thread).
The generation and deserialization, "my algorithms", of these cookie strings uses intermediary void *
s and std::string
s and since Protocol Buffers has an internal memory retention mechanism I want these characteristics for "my algorithms".
So how do I implement a common scratch memory ? I don't know much about the rdbuf(streambuf - strinbuf ??) of the std::string object. I would presumeably need to grow it to the lowest common size ever encountered during the execution of "my algorithms". Thoughts ?
My question I guess would be: " is the internal buffer of a string re-usable, and if so, how ?"
Edit (new question):
It seems uppon reflection after Vlad's post that I do need a std::string as well a void * c-style scratch buffer. My question would then be: do popular stl's string implementations retain memory when they dont need it ? (my needs will probably stay between 128-bytes to 10-KB).
发布评论
评论(1)
您不应期望
std::string
的全部内容都驻留在 TLS 中,因为std::string
会自行对数据进行分配和重新分配。一个简单的想法是在堆上分配一个结构并将指向它的指针存储在 TLS 中。编辑:
AFAIK
rdbuf
是流的功能,而不是string
的功能(请参阅此处和此处 )。编辑:
我建议使用
std::vector
而不是字符串,它 应该是连续的。同样,最好只将指向向量的指针放入 TLS 中。同一篇文章的评论说,该标准甚至要求string
是连续的,从&(str[0])
char 开始。You shouldn't expect the whole content of your
std::string
to reside in TLS, sincestd::string
makes allocations and reallocations for data on its own. A simple idea would be to allocate a structure on heap and store a pointer to it in the TLS.Edit:
AFAIK
rdbuf
is a feature of streams, not ofstring
(see here and here).Edit:
I would suggest using
std::vector
instead of string, it should be contiguous. Again, it's perhaps better to put just a pointer to thevector
into TLS. The comments to the same article say that the standard requires evenstring
to be contiguous, starting from&(str[0])
char.