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

Add a copy(::LazyBranch) method to prevent branch materialization on … #286

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 25 additions & 16 deletions src/iteration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,29 +112,38 @@ mutable struct LazyBranch{T,J,B} <: AbstractVector{T}
buffer::Vector{B}
thread_locks::Vector{ReentrantLock}
buffer_range::Vector{UnitRange{Int64}}
end

function LazyBranch(f::ROOTFile, b::Union{TBranch,TBranchElement})
T, J = auto_T_JaggT(f, b; customstructs=f.customstructs)
T = (T === Vector{Bool} ? BitVector : T)
_buffer = T[]
if J != Nojagg
# if branch is jagged, fix the buffer and eltype according to what
# VectorOfVectors would return in `getindex`
_buffer = VectorOfVectors(T(), Int32[1])
T = SubArray{eltype(T), 1, T, Tuple{UnitRange{Int64}}, true}
end
Nthreads = _maxthreadid()
return new{T,J,typeof(_buffer)}(f, b, length(b),
b.fBasketEntry,
[_buffer for _ in 1:Nthreads],
[ReentrantLock() for _ in 1:Nthreads],
[0:-1 for _ in 1:Nthreads])
function LazyBranch(f::ROOTFile, b::Union{TBranch,TBranchElement})
T, J = auto_T_JaggT(f, b; customstructs=f.customstructs)
T = (T === Vector{Bool} ? BitVector : T)
_buffer = T[]
if J != Nojagg
# if branch is jagged, fix the buffer and eltype according to what
# VectorOfVectors would return in `getindex`
_buffer = VectorOfVectors(T(), Int32[1])
T = SubArray{eltype(T), 1, T, Tuple{UnitRange{Int64}}, true}
end
Nthreads = _maxthreadid()
return LazyBranch{T,J,typeof(_buffer)}(f, b, length(b),
b.fBasketEntry,
[_buffer for _ in 1:Nthreads],
[ReentrantLock() for _ in 1:Nthreads],
[0:-1 for _ in 1:Nthreads])
end

LazyBranch(f::ROOTFile, s::AbstractString) = LazyBranch(f, f[s])
basketarray(lb::LazyBranch, ithbasket) = basketarray(lb.f, lb.b, ithbasket)
basketarray_iter(lb::LazyBranch) = basketarray_iter(lb.f, lb.b)

"""
copy(x::LazyBranch)

Makes a shallow copy of x.
"""
Base.copy(x::LazyBranch{T,J,B}) where {T,J,B} = LazyBranch{T,J,B}(x.f, x.b, x.L, x.fEntry, copy.(x.buffer),
x.thread_locks, copy(x.buffer_range))

function Base.hash(lb::LazyBranch, h::UInt)
h = hash(lb.f, h)
h = hash(lb.b.fClassName, h)
Expand Down
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,11 @@ end
@test [row.int32_array for row in table[20:30]] == BA[20:30]
@test sum(table.int32_array) == sum(row.int32_array for row in table)
@test [row.int32_array for row in table] == BA
BA_copy = copy(BA)
#Check the copy is a LazyBranch:
@test typeof(BA_copy) == typeof(BA)
#Check the content:
@test all(BA .== BA_copy)

# do some hardcoded value checks
bunches = Float32[]
Expand Down
Loading