Skip to content

Commit b3660ae

Browse files
authored
Merge pull request #118 from SymbolicML/copyable-eval-options
Copyable buffer and eval options
2 parents 1584918 + 0ba297a commit b3660ae

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DynamicExpressions"
22
uuid = "a40a106e-89c9-4ca8-8020-a735e8728b6b"
33
authors = ["MilesCranmer <miles.cranmer@gmail.com>"]
4-
version = "1.9.1"
4+
version = "1.9.2"
55

66
[deps]
77
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"

src/Evaluate.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ struct ArrayBuffer{A<:AbstractMatrix,R<:Base.RefValue{<:Integer}}
3636
index::R
3737
end
3838

39+
function Base.copy(buffer::ArrayBuffer)
40+
return ArrayBuffer(copy(buffer.array), Ref(buffer.index[]))
41+
end
42+
3943
reset_index!(buffer::ArrayBuffer) = buffer.index[] = 0
4044
reset_index!(::Nothing) = nothing
4145

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

124+
_copy(x) = copy(x)
125+
_copy(::Nothing) = nothing
126+
function Base.copy(eval_options::EvalOptions)
127+
return EvalOptions(;
128+
turbo=eval_options.turbo,
129+
bumper=eval_options.bumper,
130+
early_exit=eval_options.early_exit,
131+
buffer=_copy(eval_options.buffer),
132+
)
133+
end
134+
120135
@unstable function _process_deprecated_kws(eval_options, deprecated_kws)
121136
turbo = get(deprecated_kws, :turbo, nothing)
122137
bumper = get(deprecated_kws, :bumper, nothing)

test/test_evaluation.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,36 @@ end
288288
@test_throws ArgumentError ex(randn(1, 5), OperatorEnum(); bad_arg=1)
289289
end
290290

291+
@testitem "Test EvalOptions copy" begin
292+
using DynamicExpressions
293+
using DynamicExpressions: ArrayBuffer
294+
295+
# Test copying with various configurations
296+
buffer = zeros(5, 10)
297+
buffer_ref = Ref(0)
298+
original = EvalOptions(;
299+
turbo=true, bumper=false, early_exit=true, buffer=ArrayBuffer(buffer, buffer_ref)
300+
)
301+
copied = copy(original)
302+
303+
# Test that all fields are equal
304+
@test copied.turbo == original.turbo
305+
@test copied.bumper == original.bumper
306+
@test copied.early_exit == original.early_exit
307+
@test copied.buffer.array == original.buffer.array
308+
@test copied.buffer.index[] == original.buffer.index[]
309+
310+
# Test that buffer is copied, not referenced
311+
@test copied.buffer !== original.buffer
312+
@test copied.buffer.array !== original.buffer.array
313+
@test copied.buffer.index !== original.buffer.index
314+
315+
# Test without buffer
316+
original_no_buffer = EvalOptions(; turbo=true, bumper=false, early_exit=true)
317+
copied_no_buffer = copy(original_no_buffer)
318+
@test copied_no_buffer.buffer === nothing
319+
end
320+
291321
@testitem "Test Inf val" begin
292322
using DynamicExpressions
293323

0 commit comments

Comments
 (0)