|
|
|
|
|
by dan-robertson
2876 days ago
|
|
Rust get’s a speed advantage over those languages from three things: 1. Being safer for writing concurrent programs and therefore having programs which are concurrent 2. (mostly) Type safe memory management which means most things are allocated on the stack and those things that are allocated in the heap don’t need to be gc’d (but note that gc doesn’t mean slow memory management but it is harder to have a shared heap in concurrent programs as some synchronisation is needed), and rust programs are typically written in a way that doesn’t allocate much on the heap. 3. Specialisation of generic code to specific data types 1 and 2 are hard for Java and c# as programs are not normally written in ways which would satisfy rust’s checks and so a compiler would just compile these to runtime checks like the c# or Java compilers would use. For number 3. C# already does this. Note that actually if C# or Java program were translated into a rust program which does the same work then the Rust program would probably be slower because the Java and C# compilers are much better at optimising programs written in the Java/C# style. A fourth advantage rust gets (part of 3 and part of the type system) is a lack of subclassing and vtables which are typically fast at runtime but hard to inline. One way to make a Java or C# program faster is to be conscious of the hidden work being done and trying to write programs in a way that safety checks can be eliminated and allocation can be made automatic. |
|