admin管理员组文章数量:1279008
I have a dictionary with many entries 65k: key => [value, value2,..]
The problem with json that it takes time to deserialize and find a key every time some input comes.
The dictionary is static and won't change in the future.
Is there any way to embed this deserialized dictionary inside an executable in the type of a memory map?
I tried embedding it directly in a module inside a HashMap, but since there are so many keys it takes an infinity to compile and it's not desirable for a library crate.
Could possibly a static variable solve the issue. If yes, how would I check if the static variable is already declared?
I have a dictionary with many entries 65k: key => [value, value2,..]
The problem with json that it takes time to deserialize and find a key every time some input comes.
The dictionary is static and won't change in the future.
Is there any way to embed this deserialized dictionary inside an executable in the type of a memory map?
I tried embedding it directly in a module inside a HashMap, but since there are so many keys it takes an infinity to compile and it's not desirable for a library crate.
Could possibly a static variable solve the issue. If yes, how would I check if the static variable is already declared?
Share Improve this question edited Feb 24 at 9:46 vlad asked Feb 24 at 9:33 vladvlad 635 bronze badges 5 |1 Answer
Reset to default 4One option would be using LazyLock
:
use std::sync::LazyLock;
use std::collections::HashMap;
static DICT: LazyLock<HashMap<String, String>> = LazyLock::new(|| {
let data = include_str!("filename.json");
let dict = todo!("Parse dictionary here");
dict
});
This will run the code inside the initialiser on the first access, meaning only the first access will be bottlenecked.
You could include the data using include_str
or include_bytes
.
Another option is to have a build.rs
file or some other script that processes the data and outputs a Rust file containing the processed HashMap.
本文标签: rustBest way to embed a big static dictionary in memory mapStack Overflow
版权声明:本文标题:rust - Best way to embed a big static dictionary in memory map? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741281848a2370052.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
LazyStatic
or similar. – Richard Neumann Commented Feb 24 at 9:44