Skip to content

Commit

Permalink
Minor performance improvements. Constant comparision
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRT committed Jul 15, 2021
1 parent 1f667d3 commit 25b4909
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 48 deletions.
45 changes: 23 additions & 22 deletions src/matchcontinue.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
include("fixLines.jl")

macro splice(iterator, body)
@assert iterator.head == :call
@assert iterator.args[1] == :in
@assert iterator.head === :call
@assert iterator.args[1] === :in
Expr(:..., :(($(esc(body)) for $(esc(iterator.args[2])) in $(esc(iterator.args[3])))))
end

Expand All @@ -38,7 +38,7 @@ end
fieldcount.
"""
@generated function evaluated_fieldcount(t::Type{T}) where {T}
res = T != NONE ? fieldcount(T) : 0
res = T !== NONE ? fieldcount(T) : 0
end

"""
Expand All @@ -58,14 +58,14 @@ function handle_destruct_fields(value::Symbol, pattern, subpatterns, len, get::S
fields = []
seen_splat = false
for (i, subpattern) in enumerate(subpatterns)
if (subpattern isa Expr) && (subpattern.head == :(...))
if (subpattern isa Expr) && (subpattern.head === :(...))
@assert allow_splat && !seen_splat "Too many ... in pattern $pattern"
@assert length(subpattern.args) == 1
seen_splat = true
push!(fields, (:($i:($len-$(length(subpatterns) - i))), subpattern.args[1]))
elseif seen_splat
push!(fields, (:($len - $(length(subpatterns) - i)), subpattern))
elseif (subpattern isa Expr) && (subpattern.head == :kw)
elseif (subpattern isa Expr) && (subpattern.head === :kw)
T_sym = Meta.quot(subpattern.args[1])
push!(fields, (:($T_sym), subpattern.args[2]))
else
Expand All @@ -87,7 +87,7 @@ end
Handles deconstruction of patterns together with the value symbol.
"""
function handle_destruct(value::Symbol, pattern, bound::Set{Symbol}, asserts::Vector{Expr})
if pattern == :(_)
if pattern === :(_)
# wildcard
true
elseif !(pattern isa Expr || pattern isa Symbol) ||
Expand All @@ -96,14 +96,15 @@ function handle_destruct(value::Symbol, pattern, bound::Set{Symbol}, asserts::Ve
@capture(pattern, Symbol(_))
# constant
# TODO do we have to be careful about QuoteNode etc?
#Probably not //John
quote
$value == $pattern
$value === $pattern
end
elseif @capture(pattern, subpattern_Symbol)
# variable
# if the pattern doesn't match, we don't want to set the variable
# so for now just set a temp variable
our_sym = Symbol("variable_$pattern")
our_sym = Symbol("variable_", pattern)
if pattern in bound
# already bound, check that this value matches
quote
Expand All @@ -118,7 +119,7 @@ function handle_destruct(value::Symbol, pattern, bound::Set{Symbol}, asserts::Ve
end
end
elseif @capture(pattern, subpattern1_ || subpattern2_) ||
(@capture(pattern, f_(subpattern1_, subpattern2_)) && f == :|)
(@capture(pattern, f_(subpattern1_, subpattern2_)) && f === :|)
# disjunction
# need to only bind variables which exist in both branches
bound1 = copy(bound)
Expand All @@ -130,7 +131,7 @@ function handle_destruct(value::Symbol, pattern, bound::Set{Symbol}, asserts::Ve
$body1 || $body2
end
elseif @capture(pattern, subpattern1_ && subpattern2_) ||
(@capture(pattern, f_(subpattern1_, subpattern2_)) && f == :&)
(@capture(pattern, f_(subpattern1_, subpattern2_)) && f === :&)
# conjunction
body1 = handle_destruct(value, subpattern1, bound, asserts)
body2 = handle_destruct(value, subpattern2, bound, asserts)
Expand All @@ -146,13 +147,13 @@ function handle_destruct(value::Symbol, pattern, bound::Set{Symbol}, asserts::Ve
$(handle_destruct(value, subpattern, bound, asserts)) && let $(bound...)
# bind variables locally so they can be used in the guard
$(@splice variable in bound quote
$(esc(variable)) = $(Symbol("variable_$variable"))
$(esc(variable)) = $(Symbol("variable_", variable))
end)
$(esc(guard))
end
end
elseif @capture(pattern, T_(subpatterns__)) #= All wild =#
if length(subpatterns) == 1 && subpatterns[1] == :(__)
if length(subpatterns) == 1 && subpatterns[1] === :(__)
#=
Fields not interesting when matching against a wildcard.
NONE() matched against a wildcard is also true
Expand All @@ -164,7 +165,7 @@ function handle_destruct(value::Symbol, pattern, bound::Set{Symbol}, asserts::Ve
T = handleSugar(T)
len = length(subpatterns)
named_fields = [pat.args[1]
for pat in subpatterns if (pat isa Expr) && pat.head == :(kw)]
for pat in subpatterns if (pat isa Expr) && pat.head === :(kw)]
nNamed = length(named_fields)
@assert length(named_fields) == length(unique(named_fields)) "Pattern $pattern has duplicate named arguments: $(named_fields)"
@assert nNamed == 0 || len == nNamed "Pattern $pattern mixes named and positional arguments"
Expand All @@ -175,7 +176,7 @@ function handle_destruct(value::Symbol, pattern, bound::Set{Symbol}, asserts::Ve
quote
a = typeof($(esc(T)))
#= NONE is a function. However, we treat it a bit special=#
if $(esc(T)) != NONE && typeof($(esc(T))) <: Function
if $(esc(T)) !== NONE && typeof($(esc(T))) <: Function
func = $(esc(T))
throw(LoadError("Attempted to match on a function", @__LINE__,
AssertionError("Incorrect match usage attempted to match on: $func")))
Expand All @@ -186,7 +187,7 @@ function handle_destruct(value::Symbol, pattern, bound::Set{Symbol}, asserts::Ve
AssertionError("Incorrect match usage. Attempted to match on a pattern that is not a struct")))
end
pattern = $(esc(T))
if $(esc(T)) != NONE
if $(esc(T)) !== NONE
if evaluated_fieldcount($(esc(T))) < $(esc(len))
error("Field count for pattern of type: $pattern is $($(esc(len))) expected $(evaluated_fieldcount($(esc(T))))")
end
Expand All @@ -213,7 +214,7 @@ function handle_destruct(value::Symbol, pattern, bound::Set{Symbol}, asserts::Ve
end)
end
quote
$value === nothing && $(esc(T)) == Nothing ||
$value === nothing && $(esc(T)) === Nothing ||
$value isa $(esc(T)) &&
$(handle_destruct_fields(value, pattern, subpatterns, length(subpatterns),
:getfield, bound, asserts; allow_splat=false))
Expand Down Expand Up @@ -252,16 +253,16 @@ end
Parenthesis for these expressions are skipped
"""
function handleSugar(T)
T = if string(T) == "<|"
T = if T === :(<|)
# Syntactic sugar cons.
:Cons
elseif string(T) == "_cons"
elseif T === :_cons
#= This is legacy for the code generator. For match equation we need to allow this as well =#
:Cons
elseif string(T) == "nil"
elseif T === :nil
# Syntactic sugar for Nil
:Nil
elseif string(T) == "NONE"
elseif T === :NONE
# Syntactic sugar for Nothing
:Nothing
else
Expand Down Expand Up @@ -308,7 +309,7 @@ function handle_match_case(value, case, tail, asserts, matchcontinue::Bool)
res = let $(bound...)
# export bindings
$(@splice variable in bound quote
$(esc(variable)) = $(Symbol("variable_$variable"))
$(esc(variable)) = $(Symbol("variable_", variable))
end)
$(esc(result))
end
Expand Down Expand Up @@ -342,7 +343,7 @@ function handle_match_case(value, case, tail, asserts, matchcontinue::Bool)
res = let $(bound...)
# export bindings
$(@splice variable in bound quote
$(esc(variable)) = $(Symbol("variable_$variable"))
$(esc(variable)) = $(Symbol("variable_", variable))
end)
$(esc(result))
end
Expand Down
44 changes: 18 additions & 26 deletions src/metaRuntime.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ end
const Option{T} = Union{SOME{T},Nothing}

""" NONE is defined as nothing. """
NONE() = Nothing()
const NONE() = Nothing()

Base.convert(::Type{Option{S}}, x::SOME{T}) where {S,T<:S} =
let
Expand Down Expand Up @@ -60,7 +60,7 @@ end

""" Compares two Booleans """
function boolEq(b1::Bool, b2::Bool)::Bool
b = b1 == b2
b1 === b2
end

""" Returns \\\"true\\\" or \\\"false\\\" string """
Expand Down Expand Up @@ -208,7 +208,7 @@ function intBitRShift(i::ModelicaInteger, s::ModelicaInteger)::ModelicaInteger
end

""" Converts Integer to Real """
function intReal(i::ModelicaInteger)::ModelicaReal
function intReal(i::ModelicaInteger)::Float64
Float64(i)
end

Expand All @@ -217,14 +217,14 @@ function intString(i::ModelicaInteger)::String
string(i)
end

function realAdd(r1::ModelicaReal, r2::ModelicaReal)::ModelicaReal
function realAdd(r1::ModelicaReal, r2::ModelicaReal)::Float64
local r::ModelicaReal

r = r1 + r2
r
end

function realSub(r1::ModelicaReal, r2::ModelicaReal)::ModelicaReal
function realSub(r1::ModelicaReal, r2::ModelicaReal)::Float64
local r::ModelicaReal

r = r1 - r2
Expand Down Expand Up @@ -319,7 +319,7 @@ function realInt(r::ModelicaReal)::ModelicaInteger
end

function realString(r::ModelicaReal)::String
local str::String = "$r"
local str::String = string(r)
str
end

Expand Down Expand Up @@ -405,10 +405,6 @@ end

""" O(1) """
function stringGet(str::String, index::ModelicaInteger)::ModelicaInteger
if index < 0
println("stringGet: index < 0!")
fail()
end
str[index]
end

Expand Down Expand Up @@ -519,7 +515,7 @@ function arrayCreate(size::ModelicaInteger, initialValue::A)::Array{A} where {A}
fill(initialValue, size)
end

""" Better """
""" O(N) """
function arrayList(arr::Array{T})::List{T} where {T}
local lst::List{T} = nil
for i = length(arr):-1:1
Expand All @@ -529,30 +525,26 @@ function arrayList(arr::Array{T})::List{T} where {T}
end

""" O(n) """
function listArray(lst::List{T})::Array{T} where {T}
local arr::Array{T} = []
function listArray(lst::Cons{T})::Array{T} where {T}
local arr::Vector{T} = T[]
for i in lst
push!(arr, i)
end
arr
end

""" O(1) """
function listArray(lst::Nil)
[]
end

""" O(1) """
function arrayUpdate(arr::Array{A}, index::ModelicaInteger,
newValue::B)::Array{A} where {A,B}
local newArray::Array{A} = arr #= same as the input array; used for folding =#
#if !(A <: B)
# println("!(A<:B)")
# @show A
# @show B
#end
if index < 0
println("arrayUpdate: index < 0!")
fail()
end
newArray[index] = newValue
#local newArray::Array{A} = arr #= same as the input array; used for folding =#
arr[index] = newValue
#= Defined in the runtime =#
newArray #= same as the input array; used for folding =#
arr #= same as the input array; used for folding =#
end

""" O(n) """
Expand Down Expand Up @@ -701,7 +693,7 @@ end

""" Returns true if the input is NONE() """
function isNone(opt::Option{A})::Bool where {A}
(opt == nothing) # isa(opt, NONE))
(opt === nothing) # isa(opt, NONE))
end

""" Returns true if the input is SOME() """
Expand Down

0 comments on commit 25b4909

Please sign in to comment.