Skip to content
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

Use Ref to IRCode #9

Merged
merged 1 commit into from
Oct 21, 2024
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 = "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))
willtebbutt marked this conversation as resolved.
Show resolved Hide resolved
@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