Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Micro optimization for offset array #222

Open
Moelf opened this issue Feb 26, 2023 · 1 comment
Open

Micro optimization for offset array #222

Moelf opened this issue Feb 26, 2023 · 1 comment

Comments

@Moelf
Copy link
Member

Moelf commented Feb 26, 2023

often when we go from 0-index to 1-index and use ArrayOfArrays, we have some patterns like this:

function read_field(io, field::VectorField{O, T}, page_list) where {O, T}
offset = read_field(io, field.offset_col, page_list)
content = read_field(io, field.content_col, page_list)
o = one(eltype(offset))
jloffset = pushfirst!(offset .+ o, o) #change to 1-indexed, and add a 1 at the beginning
res = VectorOfVectors(content, jloffset, ArraysOfArrays.no_consistency_checks)
return res::_field_output_type(field)
end

I can think of 3 ways of doing this assuming we will make a copy (another option is to plug offset::Base.ReinterpretArray into VectorOfVectors but idk if that's a good idea):

julia> function f(ary, o)
           return pushfirst!(ary .+ o, o)
       end

julia> function g(ary, o)
           return append!([o], ary .+= o)
       end

julia> function h(ary, o)
           res = append!([o], ary)
           @views res[2:end] .+= o
           return res
       end

julia> @benchmark f(ary,o) setup = begin ary = reinterpret(Int32, rand(UInt8, 10^5*4)); o = Int32(1) end
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min  max):  37.401 μs  672.151 μs  ┊ GC (min  max): 0.00%  88.17%
 Time  (median):     49.064 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   53.250 μs ±  43.186 μs  ┊ GC (mean ± σ):  6.11% ±  6.97%

    ▃▅▅▆█▇▅▃▃▂▂▂▁                                              ▂
  ▃▄██████████████▇▇▆▅▅▄▃▃▃▄▃▁▁▄▁▁▁▃▁▃▁▁▁▁▁▁▁▃▁▁▁▁▁▁▁▁▃▅▄▃▃▄▆▆ █
  37.4 μs       Histogram: log(frequency) by time       141 μs <

 Memory estimate: 695.55 KiB, allocs estimate: 3.

julia> @benchmark g(ary,o) setup = begin ary = reinterpret(Int32, rand(UInt8, 10^5*4)); o = Int32(1) end
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min  max):  55.837 μs  692.319 μs  ┊ GC (min  max): 0.00%  75.43%
 Time  (median):     62.729 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   65.075 μs ±  28.550 μs  ┊ GC (mean ± σ):  2.06% ±  4.30%

          ▁▃▄▅▆▇███▇▆▄▂▂▂▂▂▁▂▁▁▁ ▁▁▁▂▁                         ▃
  ▃▁▁▃▁▃▅▇███████████████████████████████▇▇▇▆▇▆▆▅▇▆▇▆▇▆▅▅▆▅▄▅▆ █
  55.8 μs       Histogram: log(frequency) by time      81.9 μs <

 Memory estimate: 390.89 KiB, allocs estimate: 6.

julia> @benchmark h(ary,o) setup = begin ary = reinterpret(Int32, rand(UInt8, 10^5*4)); o = Int32(1) end
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min  max):  27.733 μs  140.297 μs  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):     34.085 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   35.711 μs ±   6.306 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%

             ▃▇█▅▁
  ▂▂▁▂▂▂▃▃▄▅██████▆▅▅▄▄▄▄▄▄▃▃▃▃▃▃▃▃▃▃▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂ ▃
  27.7 μs         Histogram: frequency by time         54.6 μs <

 Memory estimate: 390.81 KiB, allocs estimate: 2.
@Moelf
Copy link
Member Author

Moelf commented Feb 26, 2023

tbh, I'm not sure why h is better than g

edit, probably:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant