Skip to content

Commit

Permalink
Use Refs (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
willtebbutt authored Oct 21, 2024
1 parent 5b2906d commit 2c7f9be
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

2 comments on commit 2c7f9be

@willtebbutt
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/117729

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 v2.0.0 -m "<description of version>" 2c7f9bec2f1ea1b5b71adcfa685cbb441699ed8f
git push origin v2.0.0

Please sign in to comment.