Skip to content

Commit

Permalink
Merge pull request #16 from c42f/cjf/simplify-RGF-type
Browse files Browse the repository at this point in the history
Simplify RuntimeGeneratedFunction type printing, but don't lie about the type :-)
  • Loading branch information
YingboMa authored Oct 27, 2020
2 parents 2a3f734 + 173ac50 commit fac4696
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
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
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

0 comments on commit fac4696

Please sign in to comment.