线程本地内存,使用 std::string 的内部缓冲区作为 c 样式临时内存

发布于 2024-08-29 09:06:17 字数 1040 浏览 3 评论 0 原文

我正在使用协议缓冲区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::strings 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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁吢 2024-09-05 09:06:18

您不应期望 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, since std::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 of string (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 the vector into TLS. The comments to the same article say that the standard requires even string to be contiguous, starting from &(str[0]) char.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文