Skip to content

Commit

Permalink
Use Refs
Browse files Browse the repository at this point in the history
  • Loading branch information
willtebbutt committed Oct 21, 2024
1 parent 5b2906d commit 9368639
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "MistyClosures"
uuid = "dbe65cb8-6be2-42dd-bbc5-4196aaced4f4"
authors = ["Will Tebbutt", "Frames White", "Hong Ge"]
version = "1.0.3"
version = "2.0.0"

[compat]
julia = "1.10"
Expand Down
9 changes: 7 additions & 2 deletions src/MistyClosures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ using Core.Compiler: IRCode

import Base: show

# As of 2.0, MistyClosure contains a reference to its IR. This is because `IRCode` is
# a surprisingly bulky type (136B in 1.11.1 -- the same as 17 Float64s). Since especially
# fast access to `ir` is never needed, it is fine to store it out-of-place, thus reducing
# the size of a `MistyClosure` from 176B to 48B. This can have a substantial impact on the
# performance of calling many `OpaqueClosure`s of the same type.
struct MistyClosure{Toc<:OpaqueClosure}
oc::Toc
ir::IRCode
ir::Base.RefValue{IRCode}
end

function MistyClosure(ir::IRCode, env...; kwargs...)
return MistyClosure(OpaqueClosure(ir, env...; kwargs...), ir)
return MistyClosure(OpaqueClosure(ir, env...; kwargs...), Ref(ir))
end

(mc::MistyClosure)(x::Vararg{Any, N}) where {N} = mc.oc(x...)
Expand Down
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ end
@test @inferred(mc(5.0)) == sin(5.0)

# Default constructor.
mc_default = MistyClosure(OpaqueClosure(ir; do_compile=true), ir)
mc_default = MistyClosure(OpaqueClosure(ir; do_compile=true), Ref(ir))
@test @inferred(mc_default(5.0) == sin(5.0))

# Recommended constructor with env.
Expand All @@ -25,7 +25,7 @@ end
@test @inferred(mc_with_env(4.0)) == Foo(5.0)(4.0)

# Default constructor with env.
mc_env_default = MistyClosure(OpaqueClosure(ir_foo, 4.0; do_compile=true), ir_foo)
mc_env_default = MistyClosure(OpaqueClosure(ir_foo, 4.0; do_compile=true), Ref(ir_foo))
@test @inferred(mc_env_default(5.0) == Foo(5.0)(4.0))

# deepcopy
Expand Down

0 comments on commit 9368639

Please sign in to comment.