Fastest way to zero an array

I have an integer array I need to zero (0) each element. (Done in inner loop of algorithm, needs to be fast as possible).

Currently I do this:

ary.iter_mut().for_each(|m| *m = 0)

I'm translating the program to Crystal. Crystal|Ruby have the fill method, so I can do:

ary.fill(0)

Crystal just optimized it to fill integer arrays with zeros (per my request).

class Array(T)
  def fill(value : T)
    {% if Int::Primitive.union_types.includes?(T) %}
      if value == 0
        to_unsafe.clear(size)
      else
        fill { value }
      end
    {% else %}
      fill { value }
    {% end %}
  end
end

Is there some comparable way to do this in Rust for integer arrays?

That turns directly into a memset call: Compiler Explorer

8 Likes

There’s a proposal for a fill method but it has some open questions.

Update: The fill method is now available (starting with Rust 1.50).

3 Likes

To be more specific, my array is of u64 elements, but the method should work on any defined integer type since they'd all be of fixed size.

That also turns directly into a call to memset: Compiler Explorer

2 Likes

@jzakiya In case it isn't clear: the compiler emitting a memset is generally what you want, since that'll be the most optimized way to set memory on your platform.

3 Likes

So does this mean my original code snippet is the fastest way to do this?
If not can you provide an actual code snippet that is faster (I don't really understand the memset stuff).

memset is generally fastest, your original code already compiled into memset, so it would be the fastest code without further context.

But why we need context here? Optimization and performance are subtle topic and humans are generally bad at measure it from code. So you must actually benchmark your real code before any hand-optimization, otherwise your "optimization" may slow down your code.

1 Like

So it seems the original code snippet may be the fastest.
(FYI: I always test my full code base when I make changes to it, not just code snippets.)

It would still be a nice feature (for users/programmers) to have an optimized fill method in the stdlib, to standardize these operations, and for synactic sugar to make source code shorter/clearer.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.