How to develop zero-cost abstractions

Hi everyone

I've been programming in Rust for over a year now and I love the zero-cost abstractions it provides. Now I would like to get my hands dirty writing my own. While I can come up with and implement the traits, adapters, and wrappers, I can only verify that they do the correct thing. However, I would like to see if they really are zero-cost abstractions. How would I do that? Is looking at the assembly output with --emit asm the only option?


PS: More specifically, what I've created is more or less a bunch of iterators and adapters that transform and aggregate a sequence of values. Now I added an aggregator adapter that collects the output values of its predecessor in a buffer until full, and then produces that buffer as its output. I want to see whether my supposedly zero-cost abstraction chain boils down to a for loop populating that buffer.

1 Like

The first step in these types of exercises would be to write (micro)benchmarks for the high level and low level approaches. If their performance is very similar (within noise), then you can claim that the high level code is zero cost to the low level one.

Looking at assembly output is usually done to investigate why a supposedly zero cost abstraction isn't zero. You have to be somewhat comfortable understanding assembly to spot the crucial differences because otherwise there will be what looks like noise between different code versions. Specifically, you'd want to see if vector operations are present in both (if one of them has it), if loops are unrolled, if function calls are inlined, etc.

I'd start with benchmarks though.


This is good advice. So basically:

  1. Write high-level code (supposedly of zero cost)
  2. Write low-level code
  3. Benchmark the two and compare
  4. Inspect assembly output if performances differ significantly

I will do so, and probably also throw in an implementation in plain C to compare against.

1 Like

Yup, sounds like a solid plan. Please remember to file github issues (or at least post here) if you find cases where you cannot achieve zero cost where you think you ought to.