Skip to content

Commit

Permalink
Merge pull request #118 from SymbolicML/copyable-eval-options
Browse files Browse the repository at this point in the history
Copyable buffer and eval options
  • Loading branch information
MilesCranmer authored Jan 1, 2025
2 parents 1584918 + 0ba297a commit b3660ae
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
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 <[email protected]>"]
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

2 comments on commit b3660ae

@MilesCranmer
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/122235

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.9.2 -m "<description of version>" b3660ae13a1ec435656f14dfeb2d24c73f8c3958
git push origin v1.9.2

Please sign in to comment.