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

specialize isassigned for performance #15

Merged
merged 1 commit into from
Apr 21, 2024
Merged

specialize isassigned for performance #15

merged 1 commit into from
Apr 21, 2024

Conversation

nsajko
Copy link
Collaborator

@nsajko nsajko commented Apr 21, 2024

Fixes #14

The below benchmark shows many orders of magnitude of performance improvement, more so for arrays with many undef elements:

using BenchmarkTools, FixedSizeArrays

function new_arr(len, prob)
  ret = FixedSizeVector{FixedSizeVector{Nothing}}(undef, len)
  for i  eachindex(ret)
    if rand() < prob
      ret[i] = eltype(ret)(undef, 0)
    end
  end
  ret
end

function assigned_count(arr)
  cnt = 0
  for i  eachindex(arr)
    cnt += isassigned(arr, i)
  end
  cnt
end

len = 10000

arr = new_arr(len, 0.1);
@btime assigned_count(arr)
# prior to this change: 244.170 ms
# with this change:     6.879 μs

arr = new_arr(len, 0.5);
@btime assigned_count(arr)
# prior to this change: 134.400 ms
# with this change:     6.871 μs

arr = new_arr(len, 0.9);
@btime assigned_count(arr)
# prior to this change: 27.091 ms
# with this change:     6.879 μs

versioninfo()
# Julia Version 1.12.0-DEV.379
# Commit aad72458539 (2024-04-20 23:40 UTC)
# Build Info:
#   Official https://julialang.org/ release
# Platform Info:
#   OS: Linux (x86_64-linux-gnu)
#   CPU: 8 × AMD Ryzen 3 5300U with Radeon Graphics
#   WORD_SIZE: 64
#   LLVM: libLLVM-16.0.6 (ORCJIT, znver2)
# Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)

Copy link

codecov bot commented Apr 21, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (4a1613e) to head (469101b).

Additional details and impacted files
@@            Coverage Diff            @@
##              main       #15   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines           32        33    +1     
=========================================
+ Hits            32        33    +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@nsajko nsajko merged commit 5353022 into JuliaArrays:main Apr 21, 2024
8 checks passed
@nsajko nsajko deleted the isassigned branch April 21, 2024 21:03
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

Successfully merging this pull request may close these issues.

specialize isassigned
2 participants