74 points · by conorbros · 4y ago · 124 comments
(The thread points out that the Nim version uses a doubly-linked list from the standard library while you'd need unsafe in Rust, and the lru crate does indeed use unsafe. So take that into account.)
Unsafe is part of the rust language. Theres nothing wrong with using it if you know what you’re doing. The value of unsafe is that it boxes your unsafe code - so you can direct extra care to those functions when writing, reviewing and auditing. (As opposed to C where memory corruption could happen anywhere, so you need to be careful everywhere.)
I could imagine a separate language on the programming languages shootout which is “safe rust”. It’d be interesting how the best unsafe and the best safe programs compare in speed and size.
I would say that a proper way to rephrase it would be to use idiomatic code vs unidiomatic. If you start using any high-level language in a non intended fashion (writing non idiomatic code) probably most of them would be faster.
Meanwhile: it may suggest the comparison is a work in progress, not finished or definitive.
Can data be "silly" or is it what people say about the data that is "silly"?
I assume nim has gotten a lot less attention in these benchmarks and there's probably huge wins available to it.
In the benchmarks game —
https://benchmarksgame-team.pages.debian.net/benchmarksgame/...
This is not the benchmarks game, although "some of the benchmark problems and implementation are borrowed from it".
Edit: FWIW, building with "gcc foo.c -o foo -O2 -s -Wall --static" was slightly faster than without the --static. And "musl-gcc foo.c -o foo -O2 -s -Wall --static" was slightly better again. Maybe 0.5ms improvement in total. I'm on Ubuntu 20 x64, gcc 9, Intel Xeon W-2133.
$ git clone https://github.com/jart/cosmopolitan && cd cosmopolitan
$ make -j8 o//examples/rusage.com o//examples/hello.com
$ o//examples/rusage.com o//examples/hello.com
hello world 123
RL: took 90µs wall time
RL: ballooned to 108kb in size
RL: needed 106µs cpu (0% kernel)
RL: caused 13 page faults (100% memcpy)
This one has a tiny bit more overhead than the practical minimum you saw earlier in the benchmark. Note that APE binaries start off as shell scripts so the first run is going to be slower. It's something I'm working towards improving in a variety of ways. Here's the executive summary of the upcoming changes: int ws, pid;
CHECK_NE(-1, (pid = fork()));
if (!pid) {
execve("ape.com", (char *const[]){"ape.com", 0}, environ);
perror("execve");
_Exit(127);
}
CHECK_EQ(pid, wait(&ws));
CHECK_TRUE(WIFEXITED(ws));
CHECK_EQ(0, WEXITSTATUS(ws));
Here's the latency of the various execution strategies: FORK+EXEC+EXIT+WAIT │ APE │ APE-LOADER │ BINFMT_MISC
───────────────────────────────────────────────────────────────
fork() + execve() │ 55µs │ 66µs │ 56µs
vfork() + execve() │ 25µs │ 446µs │ 35µs
/bin/sh -c ./ape (1st) │ 485µs │ 457µs │ 170µs
/bin/sh -c ./ape (avg) │ 148µs │ 466µs │ 159µsEdit, and also no, cycles are not "more about instruction set". They're pulses of electricity that make their way through a chip, they are about as far from instruction-set-specific as it gets.
static inline unsigned long ClocksToNanos(unsigned long x, unsigned long y) {
// approximation of round(x*.323018) which is usually
// the ratio between inva rdtsc ticks and nanoseconds
unsigned long difference = y >= x ? y - x : ~x + y + 1;
return (difference * 338709) >> 20;
}
That's just one quick and dirty way your approximation could be approximated. Should work great for any benchmark games. Obviously don't use it for like, an X-ray or something. You can get the RDTSC value as follows: static inline unsigned long Rdtsc(void) {
unsigned long Rax, Rdx;
asm volatile("rdtsc" : "=a"(Rax), "=d"(Rdx) : /* no inputs */ : "memory");
return Rdx << 32 | Rax;
}
Intel also has recommendations for things like using CPUID and memory fences if you need to reign in speculative execution, but they can be costly. If you want to go even deeper then RDTSCP has a nice feature that lets you know if the operating system switched you to a different core during your measurement interval because it gives you TSC_AUX.Because instruction set is the interface and the architecture is the implementation? Or something else?
https://benchmarksgame-team.pages.debian.net/benchmarksgame/...
https://benchmarksgame-team.pages.debian.net/benchmarksgame/...
----
> I only wish the benchmarks had some metric for how idiomatic the implementation is compared to normal developers.
Maybe the benchmarks game website kind-of does — source code size.
Otherwise "I only wish…" so — wishful thinking.
Otherwise "how idiomatic" seems to mean — how I would implement the program today, if I didn't care [goldilocks] too much about performance?
(Incidentally "normal developers" doesn't seem to be stretching towards inclusive.)
However it is a mistake to then suppose that nothing can be known and just go ahead and use Python to make a AAA first person shooter.
It makes it clear that some people have most time to optimize for a benchmark than others.
This benchmark on state-of-the-art cryptography shows that GC-ed languages including Go and Nim can be 2x faster than Rust:
- https://hackmd.io/@gnark/eccbench#Pairing (old benchmarks, in particular Nim had a parameter passing issue/bad copy behavior on large types)
Furthermore, Nim GC/memory management is type-dependent, only seq/string (which use malloc/free) and `ref T` type (which use refcounting) involves some runtime. So Nim devs can choose to opt-in in GC or use manual memory management depending on their priorities.
This is wrong. At least, it is not true generally. I wrote a 1:1 port of my C++ chess engine, and after a bit of fiddling with compiler options the Nim version was faster. In fact, when using a delayed garbage collector, the program was even faster than when using reference counting.
No doubt I misunderstood what you meant, so
2.13s n-body C gcc #9 program
3.18s n-body C# .NET #9 program
Is it just that I'm reading your "2-3x" literally, and you also meant 1.5x? :-)I think you're saying that no GC language gets withing 2-3x of the fastest non-GC language. There are slow non-GC languages.
I'm not sure. The following claim is being made on Nim's website:
> Modern concepts like zero-overhead iterators and compile-time evaluation of user-defined functions, in combination with the preference of value-based datatypes allocated on the stack, lead to extremely performant code.
> Efficient, expressive, elegant
> Nim is a statically typed compiled systems programming language
A systems programming language need to have exceptionally good performance, so not sure it's to be expected that the performance is so different than Rust.
Apparently the Rust versions have received more optimization efforts compared to the Nim code as well, and it seems some people are working on optimizing the Nim versions so we'll see where it lands in the future I guess: https://news.ycombinator.com/item?id=30244188
Having programmed in both I think Rust often makes it easier to write fast code by making all the copying vs borrowing more explicit.
Nim is obviously still very fast and much easier to pick up, so there are trade-offs here.
But no, Fortran is not an alternative to C (und vice versa).
EDIT: Downvoters - is that not the case?
As an aside, I'm not expecting a lot of languages to live up to the "C like performance" claim when they make it, as C has been receiving optimizations for over 50 years by now, so it makes sense it's still the fastest around, at least until there is some major breakthrough in programming language design.
Languages like Nim/Rust/D/etc. should not have significantly different speeds for equivalent implementations for these kinds of benchmarks, as they ultimately can be written to compile to equivalent LLVM IR and thus the same machine code.
What you're usually seeing when you see such large differences is that there are differences in the implementation of the algorithm or of a standard library function.
There's nothing about these kinds of languages that should result in inherently significant performance differences on such synthetic microbenchmarks.
Allocation-heavy or write-barrier-heavy code might make a difference, but you literally have performance differences while iterating over arrays of scalars. And even then, last I benchmarked it, Nim actually compared favorably with e.g. jemalloc.
I guess it depends on what significant is but I certainly hope Rust can be faster than C in many scenarios due to optimizations llvm can make with the added type information such as inferring aliasing rules.
- multithreading runtime (i.e Rayon vs Weave https://github.com/mratsim/weave)
- Cryptography: https://hackmd.io/@gnark/eccbench#Pairing
- Scientific computing / matrix multiplication: https://github.com/bluss/matrixmultiply/issues/34#issuecomme...
There is no inherent reason why a Nim program would be slower than Rust.
https://programming-language-benchmarks.vercel.app/rust-vs-c
The issue with targeting C is you ended up losing a lot of information about intent. Compilers looking at C have to put a lot of trust that what the programmer wrote was intentional (and thus, unoptimizable).
Rust and C both end up faster than Nim because they are talking directly to the compiler.
IDK enough about Nim to say if it could hit Rust/C speeds if it targeted the LLVM instead, but it would be a lot faster than it currently is targeting C.
> The issue with targeting C is you ended up losing a lot of information about intent. Compilers looking at C have to put a lot of trust that what the programmer wrote was intentional (and thus, unoptimizable).
The same issue exists in C-sourced compiler backends, which is both GCC and LLVM. It’s not exactly a secret that anything which is not strongly exercised by standard C(++) codebases tends to be rather broken for any non-trivial use and takes years to fix (restrict/noalias being the poster child for this, at least in the context of Rust, I think it hasn’t gotten re-disabled yet so it might make a year).
C has nothing to do with performance, runtimes and libraries do. Nim has runtime overhead whereas C and Rust do not.
Not expecting it to match Rust or C in performance, just surprised at the size of the gap.
Would be interesting to see what the generated C is like from the Nim code.
If you don't use automatically managed types like seq/string and ref, there is no GC underneath. And even then, the GC is just reference counting so no difference from Rust Rc/Arc.
parallel:
for i in 0..<n:
spawn times_i(v, u, i, n, reverse)
(https://github.com/hanabi1224/Programming-Language-Benchmark...)It's almost four times as slow as the otherwise identical single-threaded version.
Meanwhile —
https://benchmarksgame-team.pages.debian.net/benchmarksgame/...