Skip to content

Copyable buffer and eval options #118

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

Merged
merged 4 commits into from
Jan 1, 2025
Merged
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "DynamicExpressions"
uuid = "a40a106e-89c9-4ca8-8020-a735e8728b6b"
authors = ["MilesCranmer <miles.cranmer@gmail.com>"]
version = "1.9.1"
version = "1.9.2"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
15 changes: 15 additions & 0 deletions src/Evaluate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ struct ArrayBuffer{A<:AbstractMatrix,R<:Base.RefValue{<:Integer}}
index::R
end

function Base.copy(buffer::ArrayBuffer)
return ArrayBuffer(copy(buffer.array), Ref(buffer.index[]))
end

reset_index!(buffer::ArrayBuffer) = buffer.index[] = 0
reset_index!(::Nothing) = nothing

Expand Down Expand Up @@ -117,6 +121,17 @@ end
@unstable @inline _to_bool_val(x::Bool) = x ? Val(true) : Val(false)
@inline _to_bool_val(::Val{T}) where {T} = Val(T::Bool)

_copy(x) = copy(x)
_copy(::Nothing) = nothing
function Base.copy(eval_options::EvalOptions)
return EvalOptions(;
turbo=eval_options.turbo,
bumper=eval_options.bumper,
early_exit=eval_options.early_exit,
buffer=_copy(eval_options.buffer),
)
end

@unstable function _process_deprecated_kws(eval_options, deprecated_kws)
turbo = get(deprecated_kws, :turbo, nothing)
bumper = get(deprecated_kws, :bumper, nothing)
Expand Down
30 changes: 30 additions & 0 deletions test/test_evaluation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,36 @@ end
@test_throws ArgumentError ex(randn(1, 5), OperatorEnum(); bad_arg=1)
end

@testitem "Test EvalOptions copy" begin
using DynamicExpressions
using DynamicExpressions: ArrayBuffer

# Test copying with various configurations
buffer = zeros(5, 10)
buffer_ref = Ref(0)
original = EvalOptions(;
turbo=true, bumper=false, early_exit=true, buffer=ArrayBuffer(buffer, buffer_ref)
)
copied = copy(original)

# Test that all fields are equal
@test copied.turbo == original.turbo
@test copied.bumper == original.bumper
@test copied.early_exit == original.early_exit
@test copied.buffer.array == original.buffer.array
@test copied.buffer.index[] == original.buffer.index[]

# Test that buffer is copied, not referenced
@test copied.buffer !== original.buffer
@test copied.buffer.array !== original.buffer.array
@test copied.buffer.index !== original.buffer.index

# Test without buffer
original_no_buffer = EvalOptions(; turbo=true, bumper=false, early_exit=true)
copied_no_buffer = copy(original_no_buffer)
@test copied_no_buffer.buffer === nothing
end

@testitem "Test Inf val" begin
using DynamicExpressions

Expand Down
Loading