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

Simplify RuntimeGeneratedFunction type printing, but don't lie about the type :-) #16

Merged
merged 3 commits into from
Oct 27, 2020
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 = "RuntimeGeneratedFunctions"
uuid = "7e49a35a-f44a-4d26-94aa-eba1b4ca6b47"
authors = ["Chris Rackauckas <accounts@chrisrackauckas.com> and contributors"]
version = "0.4.1"
version = "0.4.0"

[deps]
ExprTools = "e2ba6199-217a-4e67-a87a-7c52f15ade04"
Expand Down
29 changes: 10 additions & 19 deletions src/RuntimeGeneratedFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,17 @@ export @RuntimeGeneratedFunction

This type should be constructed via the macro @RuntimeGeneratedFunction.
"""
struct RuntimeGeneratedFunction{moduletag,id,argnames}
struct RuntimeGeneratedFunction{argnames,moduletag,id} <: Function
Copy link
Member

Choose a reason for hiding this comment

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

haha yes, good idea.

body::Expr
function RuntimeGeneratedFunction(moduletag, ex)
def = splitdef(ex)
args, body = normalize_args(def[:args]), def[:body]
id = expr2bytes(body)
id = expr_to_id(body)
cached_body = _cache_body(moduletag, id, body)
new{moduletag,id,Tuple(args)}(cached_body)
new{Tuple(args),moduletag,id}(cached_body)
end
end

function Base.show(io::IO, ::Type{<:RuntimeGeneratedFunction{mod,id,arg}}) where {mod,id,arg}
print(io, "RuntimeGeneratedFunction{$arg}")
end

# don't override typeof
function Base.show(io::IO, ::MIME"text/plain", ::Type{<:RuntimeGeneratedFunction{mod,id,arg}}) where {mod,id,arg}
print(io, "RuntimeGeneratedFunction{$mod, $id, $arg}")
end

"""
@RuntimeGeneratedFunction(function_expression)

Expand Down Expand Up @@ -68,7 +59,7 @@ macro RuntimeGeneratedFunction(ex)
end
end

function Base.show(io::IO, f::RuntimeGeneratedFunction{moduletag, id, argnames}) where {moduletag,id,argnames}
function Base.show(io::IO, f::RuntimeGeneratedFunction{argnames, moduletag, id}) where {argnames,moduletag,id}
mod = parentmodule(moduletag)
func_expr = Expr(:->, Expr(:tuple, argnames...), f.body)
print(io, "RuntimeGeneratedFunction(#=in $mod=#, ", repr(func_expr), ")")
Expand All @@ -80,7 +71,7 @@ end
# @RuntimeGeneratedFunction
function generated_callfunc end

function generated_callfunc_body(moduletag, id, argnames, __args)
function generated_callfunc_body(argnames, moduletag, id, __args)
setup = (:($(argnames[i]) = @inbounds __args[$i]) for i in 1:length(argnames))
body = _lookup_body(moduletag, id)
@assert body !== nothing
Expand Down Expand Up @@ -110,7 +101,7 @@ end
# @generated function.
_cache_lock = Threads.SpinLock()
_cachename = Symbol("#_RuntimeGeneratedFunctions_cache")
_tagname = Symbol("#_RuntimeGeneratedFunctions_ModTag")
_tagname = Symbol("#_RGF_ModTag")

function _cache_body(moduletag, id, body)
lock(_cache_lock) do
Expand Down Expand Up @@ -168,8 +159,8 @@ function init(mod)
# or so. See:
# https://github.com/JuliaLang/julia/pull/32902
# https://github.com/NHDaly/StagedFunctions.jl/blob/master/src/StagedFunctions.jl#L30
@inline @generated function $RuntimeGeneratedFunctions.generated_callfunc(f::$RuntimeGeneratedFunctions.RuntimeGeneratedFunction{$_tagname, id, argnames}, __args...) where {id,argnames}
$RuntimeGeneratedFunctions.generated_callfunc_body($_tagname, id, argnames, __args)
@inline @generated function $RuntimeGeneratedFunctions.generated_callfunc(f::$RuntimeGeneratedFunctions.RuntimeGeneratedFunction{argnames, $_tagname, id}, __args...) where {argnames,id}
$RuntimeGeneratedFunctions.generated_callfunc_body(argnames, $_tagname, id, __args)
end
end)
end
Expand All @@ -186,10 +177,10 @@ function normalize_args(arg::Expr)
arg.args[1]
end

function expr2bytes(ex)
function expr_to_id(ex)
io = IOBuffer()
Serialization.serialize(io, ex)
return Tuple(sha512(take!(io)))
return Tuple(reinterpret(UInt32, sha1(take!(io))))
end

end
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ f1 = @RuntimeGeneratedFunction(ex1)
f2 = @RuntimeGeneratedFunction(ex2)
f3 = @RuntimeGeneratedFunction(ex3)

@test f1 isa Function

du = rand(2)
u = rand(2)
p = nothing
Expand Down