Skip to content

Instantly share code, notes, and snippets.

@jamii
Created April 16, 2018 12:57
Show Gist options
  • Save jamii/21f35fcf6a025f8fb1442c67307fbda1 to your computer and use it in GitHub Desktop.
Save jamii/21f35fcf6a025f8fb1442c67307fbda1 to your computer and use it in GitHub Desktop.
julia> abstract type Expr end
julia> struct Apply <: Expr
f::Expr
arg::Expr
end
julia> struct Var <: Expr
name::Symbol
end
julia> match_f(x) = Match.@match x begin
Apply(Var(f), Apply(Var(g), Var(a))) => (f,g,a)
end
match_f (generic function with 1 method)
julia> rematch_f(x) = Rematch.@match x begin
Apply(Var(f), Apply(Var(g), Var(a))) => (f,g,a)
end
rematch_f (generic function with 1 method)
julia> match_f(Apply(Var(:inc), Apply(Var(:double), Var(:x))))
(:inc, :double, :x)
julia> rematch_f(Apply(Var(:inc), Apply(Var(:double), Var(:x))))
(:inc, :double, :x)
julia> @code_warntype match_f(Apply(Var(:inc), Apply(Var(:double), Var(:x))))
Variables:
#self# <optimized out>
x::Apply
a::Any
g::Any
f::Any
v#756 <optimized out>
#temp#@_7::Bool
#temp#@_8::Bool
#temp#@_9::Bool
#temp#@_10::Bool
#temp#@_11::Bool
#temp#@_12::Bool
#temp#@_13::Bool
#temp#@_14::Bool
#temp#@_15::Bool
Body:
begin
# meta: location /home/jamie/.julia/v0.6/Match/src/matchmacro.jl # line 438:
# meta: location REPL[84] # line 2:
SSAValue(2) = $(Expr(:invoke, MethodInstance for fieldnames(::DataType), :(Main.fieldnames), :(Main.Apply)))
unless ((Base.arraylen)(SSAValue(2))::Int64 === 2)::Bool goto 55
unless ((Main.getfield)(x::Apply, 1)::Expr isa Main.Var)::Bool goto 50
SSAValue(3) = $(Expr(:invoke, MethodInstance for fieldnames(::DataType), :(Main.fieldnames), :(Main.Var)))
unless ((Base.arraylen)(SSAValue(3))::Int64 === 1)::Bool goto 45
unless ((Main.getfield)(x::Apply, 2)::Expr isa Main.Apply)::Bool goto 40
SSAValue(4) = $(Expr(:invoke, MethodInstance for fieldnames(::DataType), :(Main.fieldnames), :(Main.Apply)))
unless ((Base.arraylen)(SSAValue(4))::Int64 === 2)::Bool goto 35
unless ((Main.getfield)((Main.getfield)(x::Apply, 2)::Expr, 1)::Any isa Main.Var)::Bool goto 30
SSAValue(5) = $(Expr(:invoke, MethodInstance for fieldnames(::DataType), :(Main.fieldnames), :(Main.Var)))
unless ((Base.arraylen)(SSAValue(5))::Int64 === 1)::Bool goto 25
unless ((Main.getfield)((Main.getfield)(x::Apply, 2)::Expr, 2)::Any isa Main.Var)::Bool goto 20
SSAValue(6) = $(Expr(:invoke, MethodInstance for fieldnames(::DataType), :(Main.fieldnames), :(Main.Var)))
#temp#@_15::Bool = ((Base.arraylen)(SSAValue(6))::Int64 === 1)::Bool
goto 22
20:
#temp#@_15::Bool = false
22:
#temp#@_14::Bool = #temp#@_15::Bool
goto 27
25:
#temp#@_14::Bool = false
27:
#temp#@_13::Bool = #temp#@_14::Bool
goto 32
30:
#temp#@_13::Bool = false
32:
#temp#@_12::Bool = #temp#@_13::Bool
goto 37
35:
#temp#@_12::Bool = false
37:
#temp#@_11::Bool = #temp#@_12::Bool
goto 42
40:
#temp#@_11::Bool = false
42:
#temp#@_10::Bool = #temp#@_11::Bool
goto 47
45:
#temp#@_10::Bool = false
47:
#temp#@_9::Bool = #temp#@_10::Bool
goto 52
50:
#temp#@_9::Bool = false
52:
#temp#@_8::Bool = #temp#@_9::Bool
goto 57
55:
#temp#@_8::Bool = false
57:
#temp#@_7::Bool = #temp#@_8::Bool
goto 60
60:
unless #temp#@_7::Bool goto 70
# meta: location /home/jamie/.julia/v0.6/Match/src/matchmacro.jl # line 390:
f::Any = (Main.getfield)((Main.getfield)(x::Apply, 1)::Expr, 1)::Any
g::Any = (Main.getfield)((Main.getfield)((Main.getfield)(x::Apply, 2)::Expr, 1)::Any, 1)::Any
a::Any = (Main.getfield)((Main.getfield)((Main.getfield)(x::Apply, 2)::Expr, 2)::Any, 1)::Any
SSAValue(0) = (Core.tuple)(f::Any, g::Any, a::Any)::Tuple{Any,Any,Any}
# meta: pop location
return SSAValue(0)
70:
# meta: location /home/jamie/.julia/v0.6/Match/src/matchmacro.jl # line 392:
SSAValue(1) = Main.nothing
# meta: pop location
# meta: pop location
# meta: pop location
return SSAValue(1)
end::Union{Tuple{Any,Any,Any}, Void}
julia> @code_warntype rematch_f(Apply(Var(:inc), Apply(Var(:double), Var(:x))))
Variables:
#self# <optimized out>
x::Apply
value <optimized out>
value_1::Expr
value_1_1::Symbol
variable_f::Symbol
value_2::Expr
value_2_1::Expr
value_2_1_1::Symbol
variable_g::Symbol
value_2_2::Expr
value_2_2_1::Symbol
variable_a::Symbol
f::Symbol
a::Symbol
g::Symbol
#temp#@_17::Bool
#temp#@_18::Bool
#temp#@_19::Bool
#temp#@_20::Bool
#temp#@_21::Bool
#temp#@_22::Bool
#temp#@_23::Bool
#temp#@_24::Bool
#temp#@_25::Bool
#temp#@_26::Bool
#temp#@_27::Bool
#temp#@_28::Bool
#temp#@_29::Bool
Body:
begin
NewvarNode(:(value_1::Expr))
NewvarNode(:(value_1_1::Symbol))
NewvarNode(:(variable_f::Symbol))
NewvarNode(:(value_2::Expr))
NewvarNode(:(value_2_1::Expr))
NewvarNode(:(value_2_1_1::Symbol))
NewvarNode(:(variable_g::Symbol))
NewvarNode(:(value_2_2::Expr))
NewvarNode(:(value_2_2_1::Symbol))
NewvarNode(:(variable_a::Symbol))
NewvarNode(:(f::Symbol))
NewvarNode(:(a::Symbol))
NewvarNode(:(g::Symbol))
# meta: location /home/jamie/.julia/v0.6/Rematch/src/Rematch.jl # line 109:
$(Expr(:invoke, MethodInstance for assert_num_fields(::Type{Apply}, ::Int64), :(Rematch.assert_num_fields), :(Main.Apply), 2)) # line 109:
$(Expr(:invoke, MethodInstance for assert_num_fields(::Type{Var}, ::Int64), :(Rematch.assert_num_fields), :(Main.Var), 1)) # line 109:
$(Expr(:invoke, MethodInstance for assert_num_fields(::Type{Apply}, ::Int64), :(Rematch.assert_num_fields), :(Main.Apply), 2)) # line 109:
$(Expr(:invoke, MethodInstance for assert_num_fields(::Type{Var}, ::Int64), :(Rematch.assert_num_fields), :(Main.Var), 1)) # line 109:
$(Expr(:invoke, MethodInstance for assert_num_fields(::Type{Var}, ::Int64), :(Rematch.assert_num_fields), :(Main.Var), 1)) # line 112:
unless (2 === 2)::Bool goto 116 # line 44:
value_1::Expr = (Rematch.getfield)(x::Apply, 1)::Expr # line 112:
unless (value_1::Expr isa Main.Var)::Bool goto 44
unless (1 === 1)::Bool goto 39 # line 44:
value_1_1::Symbol = (Rematch.getfield)(value_1::Var, 1)::Symbol # line 76:
variable_f::Symbol = value_1_1::Symbol # line 77:
#temp#@_20::Bool = true
goto 41
39:
#temp#@_20::Bool = false
41:
#temp#@_19::Bool = #temp#@_20::Bool
goto 46
44:
#temp#@_19::Bool = false
46:
unless #temp#@_19::Bool goto 111 # line 44:
value_2::Expr = (Rematch.getfield)(x::Apply, 2)::Expr # line 112:
unless (value_2::Expr isa Main.Apply)::Bool goto 106
unless (2 === 2)::Bool goto 101 # line 44:
value_2_1::Expr = (Rematch.getfield)(value_2::Apply, 1)::Expr # line 112:
unless (value_2_1::Expr isa Main.Var)::Bool goto 70
unless (1 === 1)::Bool goto 65 # line 44:
value_2_1_1::Symbol = (Rematch.getfield)(value_2_1::Var, 1)::Symbol # line 76:
variable_g::Symbol = value_2_1_1::Symbol # line 77:
#temp#@_25::Bool = true
goto 67
65:
#temp#@_25::Bool = false
67:
#temp#@_24::Bool = #temp#@_25::Bool
goto 72
70:
#temp#@_24::Bool = false
72:
unless #temp#@_24::Bool goto 96 # line 44:
value_2_2::Expr = (Rematch.getfield)(value_2::Apply, 2)::Expr # line 112:
unless (value_2_2::Expr isa Main.Var)::Bool goto 91
unless (1 === 1)::Bool goto 86 # line 44:
value_2_2_1::Symbol = (Rematch.getfield)(value_2_2::Var, 1)::Symbol # line 76:
variable_a::Symbol = value_2_2_1::Symbol # line 77:
#temp#@_28::Bool = true
goto 88
86:
#temp#@_28::Bool = false
88:
#temp#@_27::Bool = #temp#@_28::Bool
goto 93
91:
#temp#@_27::Bool = false
93:
#temp#@_26::Bool = #temp#@_27::Bool
goto 98
96:
#temp#@_26::Bool = false
98:
#temp#@_23::Bool = #temp#@_26::Bool
goto 103
101:
#temp#@_23::Bool = false
103:
#temp#@_22::Bool = #temp#@_23::Bool
goto 108
106:
#temp#@_22::Bool = false
108:
#temp#@_21::Bool = #temp#@_22::Bool
goto 113
111:
#temp#@_21::Bool = false
113:
#temp#@_18::Bool = #temp#@_21::Bool
goto 118
116:
#temp#@_18::Bool = false
118:
#temp#@_17::Bool = #temp#@_18::Bool
goto 121
121:
unless #temp#@_17::Bool goto 132 # line 146:
f::Symbol = variable_f::Symbol # line 146:
a::Symbol = variable_a::Symbol # line 146:
g::Symbol = variable_g::Symbol # line 148:
#temp#@_29::Bool = true
goto 134
132:
#temp#@_29::Bool = false
134:
unless #temp#@_29::Bool goto 138 # line 172:
return (Core.tuple)(f::Symbol, g::Symbol, a::Symbol)::Tuple{Symbol,Symbol,Symbol}
138: # line 174:
(Rematch.throw)($(Expr(:new, :(Rematch.MatchFailure), :(x))))::Union{}
# meta: pop location
end::Tuple{Symbol,Symbol,Symbol}
julia> @benchmark match_f($Apply(Var(:inc), Apply(Var(:double), Var(:x))))
BenchmarkTools.Trial:
memory estimate: 624 bytes
allocs estimate: 11
--------------
minimum time: 1.447 μs (0.00% GC)
median time: 1.499 μs (0.00% GC)
mean time: 1.624 μs (3.28% GC)
maximum time: 310.933 μs (96.93% GC)
--------------
samples: 10000
evals/sample: 10
julia> @benchmark rematch_f($Apply(Var(:inc), Apply(Var(:double), Var(:x))))
BenchmarkTools.Trial:
memory estimate: 624 bytes
allocs estimate: 11
--------------
minimum time: 1.244 μs (0.00% GC)
median time: 1.266 μs (0.00% GC)
mean time: 1.396 μs (3.49% GC)
maximum time: 255.502 μs (97.89% GC)
--------------
samples: 10000
evals/sample: 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment