All the details, benchmark code, generated assembly, and explanation are in the README:

https://github.com/amidukr/rust-devirtualization-test

UPD

Made the dynamic-dispatch part more accurate.

The measured difference increased from ~3.5× to ~4.9×.

Changed:

// was x = op.apply(std::hint::black_box(x)); 

to:

// now x = std::hint::black_box(op).apply(std::hint::black_box(x)); 

Which changed the generated assembly from:

; was call r15 

to:

; now call qword ptr [rax + 24] 

The previous version allowed LLVM to hoist the vtable method pointer out of the loop. The updated version performs the vtable method lookup on each iteration.

UPD

Benchmark for de-virtualization case added.

30 posts - 6 participants

Read full topic