Skip to content

Commit fac4696

Browse files
authored
Merge pull request #16 from c42f/cjf/simplify-RGF-type
Simplify RuntimeGeneratedFunction type printing, but don't lie about the type :-)
2 parents 2a3f734 + 173ac50 commit fac4696

File tree

3 files changed

+13
-20
lines changed

3 files changed

+13
-20
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "RuntimeGeneratedFunctions"
22
uuid = "7e49a35a-f44a-4d26-94aa-eba1b4ca6b47"
33
authors = ["Chris Rackauckas <accounts@chrisrackauckas.com> and contributors"]
4-
version = "0.4.1"
4+
version = "0.4.0"
55

66
[deps]
77
ExprTools = "e2ba6199-217a-4e67-a87a-7c52f15ade04"

src/RuntimeGeneratedFunctions.jl

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,17 @@ export @RuntimeGeneratedFunction
1010
1111
This type should be constructed via the macro @RuntimeGeneratedFunction.
1212
"""
13-
struct RuntimeGeneratedFunction{moduletag,id,argnames}
13+
struct RuntimeGeneratedFunction{argnames,moduletag,id} <: Function
1414
body::Expr
1515
function RuntimeGeneratedFunction(moduletag, ex)
1616
def = splitdef(ex)
1717
args, body = normalize_args(def[:args]), def[:body]
18-
id = expr2bytes(body)
18+
id = expr_to_id(body)
1919
cached_body = _cache_body(moduletag, id, body)
20-
new{moduletag,id,Tuple(args)}(cached_body)
20+
new{Tuple(args),moduletag,id}(cached_body)
2121
end
2222
end
2323

24-
function Base.show(io::IO, ::Type{<:RuntimeGeneratedFunction{mod,id,arg}}) where {mod,id,arg}
25-
print(io, "RuntimeGeneratedFunction{$arg}")
26-
end
27-
28-
# don't override typeof
29-
function Base.show(io::IO, ::MIME"text/plain", ::Type{<:RuntimeGeneratedFunction{mod,id,arg}}) where {mod,id,arg}
30-
print(io, "RuntimeGeneratedFunction{$mod, $id, $arg}")
31-
end
32-
3324
"""
3425
@RuntimeGeneratedFunction(function_expression)
3526
@@ -68,7 +59,7 @@ macro RuntimeGeneratedFunction(ex)
6859
end
6960
end
7061

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

83-
function generated_callfunc_body(moduletag, id, argnames, __args)
74+
function generated_callfunc_body(argnames, moduletag, id, __args)
8475
setup = (:($(argnames[i]) = @inbounds __args[$i]) for i in 1:length(argnames))
8576
body = _lookup_body(moduletag, id)
8677
@assert body !== nothing
@@ -110,7 +101,7 @@ end
110101
# @generated function.
111102
_cache_lock = Threads.SpinLock()
112103
_cachename = Symbol("#_RuntimeGeneratedFunctions_cache")
113-
_tagname = Symbol("#_RuntimeGeneratedFunctions_ModTag")
104+
_tagname = Symbol("#_RGF_ModTag")
114105

115106
function _cache_body(moduletag, id, body)
116107
lock(_cache_lock) do
@@ -168,8 +159,8 @@ function init(mod)
168159
# or so. See:
169160
# https://github.com/JuliaLang/julia/pull/32902
170161
# https://github.com/NHDaly/StagedFunctions.jl/blob/master/src/StagedFunctions.jl#L30
171-
@inline @generated function $RuntimeGeneratedFunctions.generated_callfunc(f::$RuntimeGeneratedFunctions.RuntimeGeneratedFunction{$_tagname, id, argnames}, __args...) where {id,argnames}
172-
$RuntimeGeneratedFunctions.generated_callfunc_body($_tagname, id, argnames, __args)
162+
@inline @generated function $RuntimeGeneratedFunctions.generated_callfunc(f::$RuntimeGeneratedFunctions.RuntimeGeneratedFunction{argnames, $_tagname, id}, __args...) where {argnames,id}
163+
$RuntimeGeneratedFunctions.generated_callfunc_body(argnames, $_tagname, id, __args)
173164
end
174165
end)
175166
end
@@ -186,10 +177,10 @@ function normalize_args(arg::Expr)
186177
arg.args[1]
187178
end
188179

189-
function expr2bytes(ex)
180+
function expr_to_id(ex)
190181
io = IOBuffer()
191182
Serialization.serialize(io, ex)
192-
return Tuple(sha512(take!(io)))
183+
return Tuple(reinterpret(UInt32, sha1(take!(io))))
193184
end
194185

195186
end

test/runtests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ f1 = @RuntimeGeneratedFunction(ex1)
3131
f2 = @RuntimeGeneratedFunction(ex2)
3232
f3 = @RuntimeGeneratedFunction(ex3)
3333

34+
@test f1 isa Function
35+
3436
du = rand(2)
3537
u = rand(2)
3638
p = nothing

0 commit comments

Comments
 (0)