-
Notifications
You must be signed in to change notification settings - Fork 484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use dyn Trait with rayon without performance loss? #1038
Comments
97ns is suspiciously short and I suspect that the optimizer is computing the sum at The slow down is due to consuming the values in the parallel computations whereas the sequential ones just iterate over references. This has a large effect because the If I just replace
which seems reasonable expect for the 80ns case which is probably not doing any computation at runtime at all. EDIT: It really is just the parallel freeing and glibc's malloc implementation, not the dynamic dispatch itself. Remove either one from the picture and the pathological slowdown disappears. |
(As to why the parallel versions suffers so much from freeing the heap allocations, note that the heap is a shared resource and even the best heap allocators will need some level of synchronization which will most likely experience significant contention when all hardware threads are basically freeing allocations from a single global heap.) |
((For example, using glibc's default allocator I get 15s for your original "Parallel,Bodies" example whereas using a more specialized allocator which is well-suited to such a scenario like snmalloc-rs brings this down to 175ms.)) |
Thanks for the response. By fixing the I expected some slowdown but was unsure how much. I wanted to make sure I wasn't making an obvious mistake. |
Not even that - it's completely throwing away the unused computation. Try printing it: let sum = /*...*/.sum::<f64>();
println!("elapsed time:{:?}, sum:{sum}", timer.elapsed()); That slows down the serial versions, while the parallel versions are about the same. In general, you could also use |
I noticed a massive performance loss when looping over a
Vec<Box<dyn Trait>>
compared to looping over aVec<struct>
usinginto_par_iter
. I'm not sure what is causing and if I'm doing something wrong.Below I have a snippet that reproduces the behavior.
When running with the release build, this produces the output:
The text was updated successfully, but these errors were encountered: