我需要一些高度并行化的集合,比如java并发skiplist。一般任务:我在一台服务器上工作,用来计算服务器收到的所有消息中的唯一单词。我不关心什么,只是字数而已。偶尔,我会收到一条get_count消息,我会重置计数器并重新开始。
但是我总是在post_words函数上遇到瓶颈。在java中,同样的事情在rust 80中运行5s。我试过横梁上的实验跳绳套装。我得到了同样的结果。另一个问题是字符串分配。有什么想法吗?
//Dashset from https://docs.rs/dashmap/4.0.2/dashmap/struct.DashSet.html
type Words = DashSet<String>;
let set: Arc<Words> = Arc::new(DashSet::with_capacity(100000));
// for each new socket I create
let set = set.clone();
//Word processing
fn post_words(client: i32, data: Vec<u8>, db: &Words) -> Response {
let mut decoder = GzDecoder::new(data.as_slice());
let mut input = String::new();
decoder.read_to_string(&mut input).unwrap();
//The bottleneck
for word in input.split_whitespace() {
db.insert(String::from(word));
}
let mut response = Response::new();
response.status = Response_Status::OK;
return response
}发布于 2021-04-27 19:54:50
因为我不需要知道单词a,所以只能保存一个散列。现在我不必为每个单词分配字符串。这将我的解决方案从80s提高到1.7s
type Words = DashSet<u64>;
async fn post_words(client: i32, data: Vec<u8>, db: &Words) -> Response {
let mut decoder = GzDecoder::new(data.as_slice());
let mut input = String::new();
decoder.read_to_string(&mut input).unwrap();
for word in input.split_whitespace() {
let mut s = DefaultHasher::new();
word.hash(&mut s);
db.insert( s.finish());
}
let mut response = Response::new();
response.status = Response_Status::OK;
return response
}https://stackoverflow.com/questions/67280449
复制相似问题