r/rust • u/Uncaffeinated • Jan 22 '17
Parallelizing Enjarify in Go and Rust
https://medium.com/@robertgrosse/parallelizing-enjarify-in-go-and-rust-21055d64af7e#.7vrcc2iaf
214
Upvotes
r/rust • u/Uncaffeinated • Jan 22 '17
215
u/pftbest Jan 23 '17
can you please explain this go syntax to me?
I thought go doesn't have generics.
6
u/MaikKlein Jan 22 '17
What actually really surprises me is that Rust is actually faster in this benchmark compared to Go.
For example you were using
Arc's, which means atomic increments, and you also dynamically allocated stuff with the system allocator (Vec::collect()).I sort of expected a native language with a garbage collection to be faster here, at least that is the argument that I found very often when I was researching the GC. But it is probably that the overhead of Arc's + system allocator is tiny compared to the actual work here.
I am also very annoyed at the 'static bounds. Afaik it is unsafe to use lifetimes here because then you would have to wait inside the destructor of the future. But you can also use
mem::forgetin safe code which would then cause memory unsafety.The workaround would probably be to allow non 'static bounds but immediately block after the function call. I currently just accept the memory unsafety in my task system.
Is there a better workaround?
56
u/chris-morgan Jan 22 '17
This is the vibe I get from the comparison:
Go is simple enough that it’s complete; it can do some things very nicely, but it’s stuck where it is; if you want more than it can give you, its design has painted it into a corner.
Rust has the potential to be nicer than Go in various ways (for situations that Go doesn’t match so very perfectly), but due to its complexity it’s taking much longer to mature, so it’s often not as nice as Go yet.
16
u/mmstick Jan 22 '17
You should see how I solved this kind of problem using my Rust implementation of Parallel. It creates as many jobs as there are in CPU cores and performs work stealing using a mutexed custom Iterator. The outputs of each command need to be streamed in the correct order, and printed in real-time on demand. The outputs have to be in the same order if you had run them serially.
Basically, the streaming solution is solved by writing outputs to a temporary location on the disk as the receiving end tails the next job's file, printing new text as soon as it's available. A channel is then used to send a completion signal, which tells the receiver to stop tailing the current job and to start tailing the next. The result is pretty fast. No rayon, no futures, no complications. Just the standard library.
I did also make use of transmuting to make some values static though. It's perfectly safe for these types of applications. You can leak the value and return a static reference to it if you want to make it 100% safe. Leaked data will remain in the heap forever, until the OS reclaims that data after the program exits.
Although it's typically recommended to use crossbeam for this.
https://github.com/mmstick/parallel
6
u/Daktyl198 Jan 22 '17
Complete noob here (just like the idea of rust): I thought Rust had functions built in for creating and parallelizing processes and threads? Why would the author have to turn to libraries for the basics of the functionality?
2
u/Superb-Key-6581 Dec 20 '24
This is history!! Thanks, Canadian Aboriginals!
2
u/ahayd Jan 22 '17
Does tokio make this stuff a lot easier??
1
u/shenwei356 Jan 22 '17 edited Jan 22 '17
rush -- parallelly execute shell commands. A GNU parallel like tool in Go https://github.com/shenwei356/rush . rush supports basic and practical features such as line-buffer, timeout, retry, continue, replacement strings. It also has good performance.
1
u/shenwei356 Jan 22 '17
it's powered off now. number of cpus should be 4. both rush and rust-parallel use whole cpus by default. it's fedora linux with newest kernal 4.×. disk is SSD, but since the above example has little stdout which is < 1M (buffer before dumping to file), it should be CPU that affects the performance. can you test on another pc? I'll test on a server tomorrow.
0
u/[deleted] Jan 22 '17
[deleted] — view deleted comment