r/programming • • Jul 13 '19

Outperforming Rust With Functional Programming

http://blog.vmchale.com/article/fast-functional
0 Upvotes

22 comments sorted by

View all comments

40

u/R_Sholes Jul 13 '19 edited Jul 13 '19

Oh, that blog's """""benchmarking"""" is consistently hilariously bad.

I remember it from a (now deleted) article showing how Haskell was orders of magnitude faster on simple benchmarks like factorial and Fibonacci, where the author explained it with some bullshit like this article's "Here, we do something that is not possible to do in C - we safely stack-allocate a function argument"WAT, while the actual reason was Haskell's version defined as lazy lists which were populated and memoized in benchmark's warmup run, and the actual benchmark only timing the list indexing.

/u/matthieum posted the comment asking if this is related to 32 vs 64 bit, but the actual difference is signed vs unsigned.

"{n : nat} (n : int(n)) : int" in ATS version constrains the argument to C ints >= 0, with proof of >=0 done at call site.

C and Rust version use signed ints, making "i/2" into

    shr     edx, 31 ; or 63 for 64 bit
    add     edi, edx
    sar     edi

instead of a single sar instruction you get in ATS output. (ETA: this is needed to accommodate the -1 / 2 == 0 case - simple shift would make -1 / 2 == -1)

Just changing int/i64 to unsigned int/u64 simplifies it the same way, and even more - simply using "n > 1" instead of "n != 1" allows the same optimization to kick in, at least in Rust version. The fact that ATS version is the only one using "n > 1" looks suspiciously like the author not liking the results and nudging them.

PS: ATS assembly for comparison:

collatz:
.L2:
	movl	$1, %eax
	cmpl	$1, %edi
	jle	.L1
.L3:
	addl	$1, %eax
	testb	$1, %dil
	jne	.L5
	sarl	%edi
	cmpl	$1, %edi
	jne	.L3
	ret
.L5:
	leal	1(%rdi,%rdi,2), %edi
	jmp	.L3
.L1:
	ret

Rust's assembly after switching != to >:

playground::collatz_loop_signed2:
	mov	eax, 1
	cmp	rdi, 2
	jl	.LBB2_3
	mov	eax, 1

.LBB2_2:
	mov	rcx, rdi
	shr	rcx
	test	dil, 1
	lea	rdi, [rdi + 2*rdi + 1]
	cmove	rdi, rcx
	add	rax, 1
	cmp	rdi, 1
	jg	.LBB2_2

.LBB2_3:
	ret

ETA: Oh, and replacing != to > in matthieum's godbolt.org link gives gcc version same as ATS's (probably because ATS uses gcc as back-end) and Clang version same as Rust's (probably since they both use LLVM)

5

u/matthieum Jul 14 '19

Sigh, I was betting it would be a fluke :/

Nice work identifying it!