Skip to content

Commit 1959bba

Browse files
committed
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
1 parent 8084833 commit 1959bba

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/RuntimeGeneratedFunctions.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ 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}
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

@@ -59,7 +59,7 @@ macro RuntimeGeneratedFunction(ex)
5959
end
6060
end
6161

62-
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}
6363
mod = parentmodule(moduletag)
6464
func_expr = Expr(:->, Expr(:tuple, argnames...), f.body)
6565
print(io, "RuntimeGeneratedFunction(#=in $mod=#, ", repr(func_expr), ")")
@@ -71,7 +71,7 @@ end
7171
# @RuntimeGeneratedFunction
7272
function generated_callfunc end
7373

74-
function generated_callfunc_body(moduletag, id, argnames, __args)
74+
function generated_callfunc_body(argnames, moduletag, id, __args)
7575
setup = (:($(argnames[i]) = @inbounds __args[$i]) for i in 1:length(argnames))
7676
body = _lookup_body(moduletag, id)
7777
@assert body !== nothing
@@ -101,7 +101,7 @@ end
101101
# @generated function.
102102
_cache_lock = Threads.SpinLock()
103103
_cachename = Symbol("#_RuntimeGeneratedFunctions_cache")
104-
_tagname = Symbol("#_RuntimeGeneratedFunctions_ModTag")
104+
_tagname = Symbol("#_RGF_ModTag")
105105

106106
function _cache_body(moduletag, id, body)
107107
lock(_cache_lock) do
@@ -159,8 +159,8 @@ function init(mod)
159159
# or so. See:
160160
# https://github.com/JuliaLang/julia/pull/32902
161161
# https://github.com/NHDaly/StagedFunctions.jl/blob/master/src/StagedFunctions.jl#L30
162-
@inline @generated function $RuntimeGeneratedFunctions.generated_callfunc(f::$RuntimeGeneratedFunctions.RuntimeGeneratedFunction{$_tagname, id, argnames}, __args...) where {id,argnames}
163-
$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)
164164
end
165165
end)
166166
end
@@ -177,10 +177,10 @@ function normalize_args(arg::Expr)
177177
arg.args[1]
178178
end
179179

180-
function expr2bytes(ex)
180+
function expr_to_id(ex)
181181
io = IOBuffer()
182182
Serialization.serialize(io, ex)
183-
return Tuple(sha512(take!(io)))
183+
return Tuple(reinterpret(UInt32, sha1(take!(io))))
184184
end
185185

186186
end

0 commit comments

Comments
 (0)