From 80848334a950d91eacbb4528a7523e46524ff8cd Mon Sep 17 00:00:00 2001 From: Chris Foster Date: Sat, 24 Oct 2020 21:55:57 +1000 Subject: [PATCH 1/3] Revert "Shorter stacktrace" This reverts commit 9b9426ea9b2556cfd6726a50468c5decde95aab3. Even though this printing is lovely to the eye, overloading show(::IO, ::Type{SomeType}) has been known to cause subtle problems in the past and suspected of causing them quite recently. --- Project.toml | 2 +- src/RuntimeGeneratedFunctions.jl | 9 --------- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/Project.toml b/Project.toml index e37f192..455976d 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "RuntimeGeneratedFunctions" uuid = "7e49a35a-f44a-4d26-94aa-eba1b4ca6b47" authors = ["Chris Rackauckas and contributors"] -version = "0.4.1" +version = "0.4.0" [deps] ExprTools = "e2ba6199-217a-4e67-a87a-7c52f15ade04" diff --git a/src/RuntimeGeneratedFunctions.jl b/src/RuntimeGeneratedFunctions.jl index ced0a6a..3f642ad 100644 --- a/src/RuntimeGeneratedFunctions.jl +++ b/src/RuntimeGeneratedFunctions.jl @@ -21,15 +21,6 @@ struct RuntimeGeneratedFunction{moduletag,id,argnames} 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) From 1959bba4d036b1198ca485d096bd10d60175c274 Mon Sep 17 00:00:00 2001 From: Chris Foster Date: Sat, 24 Oct 2020 21:57:21 +1000 Subject: [PATCH 2/3] Shorten the RuntimeGeneratedFunction type - Use SHA1 and use UInt32's to encode the 20 bits. sha1 has been good enough for git, and 64 bytes for sha512 is an awful lot. - Reorder the RuntimeGeneratedFunction type parameters to make them easier to read. - Shorten the module tag name --- src/RuntimeGeneratedFunctions.jl | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/RuntimeGeneratedFunctions.jl b/src/RuntimeGeneratedFunctions.jl index 3f642ad..70889f1 100644 --- a/src/RuntimeGeneratedFunctions.jl +++ b/src/RuntimeGeneratedFunctions.jl @@ -10,14 +10,14 @@ export @RuntimeGeneratedFunction This type should be constructed via the macro @RuntimeGeneratedFunction. """ -struct RuntimeGeneratedFunction{moduletag,id,argnames} +struct RuntimeGeneratedFunction{argnames,moduletag,id} 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 @@ -59,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), ")") @@ -71,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 @@ -101,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 @@ -159,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 @@ -177,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 From 173ac5078405bb30f1e0b4e0bf1ae8592cb95fad Mon Sep 17 00:00:00 2001 From: Chris Foster Date: Sun, 25 Oct 2020 07:48:20 +1000 Subject: [PATCH 3/3] Make RuntimeGeneratedFunction <: Function Seems like a good place to put it in the type hierarchy. --- src/RuntimeGeneratedFunctions.jl | 2 +- test/runtests.jl | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/RuntimeGeneratedFunctions.jl b/src/RuntimeGeneratedFunctions.jl index 70889f1..8f2d4de 100644 --- a/src/RuntimeGeneratedFunctions.jl +++ b/src/RuntimeGeneratedFunctions.jl @@ -10,7 +10,7 @@ export @RuntimeGeneratedFunction This type should be constructed via the macro @RuntimeGeneratedFunction. """ -struct RuntimeGeneratedFunction{argnames,moduletag,id} +struct RuntimeGeneratedFunction{argnames,moduletag,id} <: Function body::Expr function RuntimeGeneratedFunction(moduletag, ex) def = splitdef(ex) diff --git a/test/runtests.jl b/test/runtests.jl index 3e24f90..9f73caa 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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