简体   繁体   English

C++ 中的静态映射

[英]Static map in C++

I am working on optimizing the code that creates tens of thousands of maps of type我正在优化创建数以万计类型地图的代码

std::unordered_map<std::wstring, std::wstring>

The number of possible keys is about 20 and they all are known at compile time.可能的键数大约为 20 个,它们在编译时都是已知的。

My idea is to probably have some specialized container with the minimal memory footprint, but ideally with C++ standard-like interface.我的想法是可能有一些内存占用最少的专用容器,但最好使用类似 C++ 标准的接口。

The other idea is to have something like std::tuple but with the index of type char * .另一个想法是拥有类似 std::tuple 的东西,但具有char *类型的索引。 Probably it is a strange idea and I did not think yet on its implementation and I am not sure it can be implemented, but theoretically it can be可能这是一个奇怪的想法,我还没有考虑它的实现,我不确定它是否可以实现,但理论上它可以

std::static_map<"key1", "key2", ... > m;
m<"key1"> = "value1";
std::string val = m<"key1">;

theoretically there can be an implementation that uses consexpr hash function.理论上可以有一个使用 consexpr 哈希函数的实现。

EDIT1: It is a data processing software that collects logs (or messages) over UDP protocol. EDIT1:它是一种通过UDP协议收集日志(或消息)的数据处理软件。 Once a message is received an MSG structure containing this map along with other data is created.一旦收到消息,就会创建一个包含此映射和其他数据的 MSG 结构。 Then MSG is added to the processing queue and after further processing is added to some other queue and so on.... If the processing is slow there can be tens of thousands of MSG's in the queues.然后将 MSG 添加到处理队列中,并在进一步处理后将其添加到某个其他队列,依此类推.... 如果处理速度缓慢,队列中可能会有数以万计的 MSG。

Your code creates and populates lots of maps and lots of wstrings, which means lots of allocations.您的代码创建并填充了大量映射和大量 wstring,这意味着大量分配。

A first reduction of the allocation effort could be to create the unordered_map with already a fixed number of buckets (20 ?).分配工作的第一个减少可能是创建具有固定数量的桶(20?)的unordered_map

But given the small number of keys known at compile time, I'd suggest to have a mapping between the key string to an integer between 0 and the known maximum number of keys.但是鉴于在编译时已知的键数量很少,我建议在键字符串与 0 和已知最大键数之间的整数之间进行映射。 You may then replace the unordered_map with a vector (also created with a fixed size), and avoid hashing for direct access to any given field.然后,您可以将unordered_map替换为vector (也以固定大小创建),并避免散列以直接访问任何给定字段。

Depending on the length of the key strings, you could perhaps use a trie to parse the key strings in the input message.根据关键字符串的长度,您或许可以使用 trie 来解析输入消息中的关键字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM