Skip to content

Commit

Permalink
avoid allocation if no transformation are necessary in setindex!
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Barth committed Jan 13, 2022
1 parent e3ea82e commit 8d54130
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/cfvariable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,12 @@ end
return out
end

@inline function CFtransformdata(
data::AbstractArray{T,N},fv::Nothing,scale_factor::Nothing,
add_offset::Nothing,time_origin::Nothing,time_factor::Nothing,::Type{T}) where {T,N}
# no transformation necessary (avoid allocation)
return data
end

@inline _inv(x::Nothing) = nothing
@inline _inv(x) = 1/x
Expand Down Expand Up @@ -568,6 +574,13 @@ end
return out
end

@inline function CFinvtransformdata(
data::AbstractArray{T,N},fv::Nothing,scale_factor::Nothing,
add_offset::Nothing,time_origin::Nothing,time_factor::Nothing,::Type{T}) where {T,N}
# no transformation necessary (avoid allocation)
return data
end

# for scalar
@inline function CFinvtransformdata(data,fv,scale_factor,add_offset,time_origin,time_factor,DT)
inv_scale_factor = _inv(scale_factor)
Expand Down
3 changes: 3 additions & 0 deletions test/test_scaling.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using NCDatasets
using Test

filename = tempname()
# The mode "c" stands for creating a new file (clobber)
ds = NCDatasets.NCDataset(filename,"c")
Expand Down

0 comments on commit 8d54130

Please sign in to comment.