Is increasing array index size in the future?

I think this exact same thing a lot, and it’s surprising how often the reason for it is that circumstances exist that I’m unaware of that make it more complicated.

You and I have both mentioned in this threa that 64-bit CPUs are optimized to deal with 64-bit values, however that doesn’t mean all 64-bit operations are faster every time. I just wrote up some benchmarks and, on my machine, the BigArray class I wrote that can index larger than 32-bit offsets is 20% slower to allocate and 81% slower to iterate through 1k of the exact same elements. I assumed allocations would be slower, but iteration being slower surprised me considering that both of them are doing the exact same pointer arithmetic on 32- vs 64-bit offsets.

Benchmark code

I’m on an ARM64 CPU, so maybe it’ll be different on a different CPU architecture (or even other ARM64 CPUs that are not made by Apple), but this is why I said in my previous post that benchmarks would be important here. Performance is never as straightforward as people think it is. You have to see it running.

require "benchmark"
require "../src/big_array"

puts "Allocation"
values = [Array(String).new, BigArray(String).new]
Benchmark.ips do |x|
  x.report "Array" { values.unsafe_put 0, Array(String).new }
  x.report "BigArray" { values.unsafe_put 1, BigArray(String).new }
end
# Need a side effect to avoid LLVM optimizations, but we don't actually want it to print
pp values unless values

puts
puts "Indexing"
value = ""
Benchmark.ips do |x|
  array = Array(String).new
  big_array = BigArray(String).new
  1_000.times do |i|
    array << i.to_s
    big_array << i.to_s
  end

  x.report "Array" { array.each { |v| value = v } }
  x.report "BigArray" { big_array.each { |v| value = v } }
end
pp value unless value

I’m on an ARM64 CPU, so maybe a different CPU architecture (or even an ARM64 CPU not made by Apple) might get different results, but this is why I said in my previous post that benchmarks would be important here. Performance is never straightforward.

If you don’t want to use a shard for it, that’s an understandable opinion to have, but sensationalizing your position by saying you’re being “forced” to use another language after I just gave you the blueprint to do what you want in Crystal is not as strong an argument as you might think it is.

4 Likes