Skip to content

Commit

Permalink
Minor performance improvements to MetaModelica.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRT committed Dec 17, 2021
1 parent 25b4909 commit 44a5d74
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
35 changes: 13 additions & 22 deletions src/dangerous.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,61 +9,52 @@ import ExportAll
using ..MetaModelica

""" O(1) """
function arrayGetNoBoundsChecking(arr::Array{A}, index::ModelicaInteger)::A where {A<:Any}
if index < 0
println("arrayGetNoBoundsChecking: index < 0!")
end
function arrayGetNoBoundsChecking(arr::Vector{A}, index::ModelicaInteger) where {A}
@inbounds arr[index]
end

""" O(1) """
function arrayUpdateNoBoundsChecking(arr::Array{A}, index::ModelicaInteger,
newValue::A)::Array{A} where {A<:Any}
local newArray::Array{A} = arr
if index < 0
println("arrayUpdateNoBoundsChecking: index < 0!")
end
function arrayUpdateNoBoundsChecking(arr::Vector{A}, index::ModelicaInteger,
newValue::A) where {A}
local newArray = arr
@inbounds newArray[index] = newValue
newArray
return newArray
end

""" Creates a new array where the elements are *not* initialized!. Any attempt to
access an uninitialized elements may cause segmentation faults if you're
lucky, and pretty much anything else if you're not. Do not use unless you will
immediately fill the whole array with data. The dummy variable is used to fix
the type of the array. """
function arrayCreateNoInit(size::ModelicaInteger, dummy::A)::Array{A} where {A<:Any}
function arrayCreateNoInit(size::ModelicaInteger, dummy::A)::Array{A} where {A}
local arr::Array{A} = fill(dummy, size)
arr
end

""" O(1) """
function stringGetNoBoundsChecking(str::String, index::ModelicaInteger)::ModelicaInteger
function stringGetNoBoundsChecking(str::String, index::ModelicaInteger)
local ch::ModelicaInteger
if index < 0
println("stringGetNoBoundsChecking: index < 0!")
end
ch = @inbounds str[index]
end

""" Not possible unless we write a C list impl for Julia """
function listReverseInPlace(inList::List{T})::List{T} where {T<:Any}
function listReverseInPlace(inList::List{T})::List{T} where {T}
MetaModelica.listReverse(inList)
end

""" O(1). A destructive operation changing the \"first\" part of a cons-cell. """
function listSetFirst(inConsCell::List{A}, inNewContent::A) where {A<:Any} #= A non-empty list =#
#= Defined in the runtime =#
function listSetFirst(inConsCell::List{A}, inNewContent::A) where {A} #= A non-empty list =#
@error "Not listSetFirst defined in the runtime"
end

""" O(1). A destructive operation changing the rest part of a cons-cell """
#= NOTE: Make sure you do NOT create cycles as infinite lists are not handled well in the compiler. =#
function listSetRest(inConsCell::List{A}, inNewRest::List{A}) where {A<:Any} #= A non-empty list =#
#= Defined in the runtime =#
function listSetRest(inConsCell::List{A}, inNewRest::List{A}) where {A} #= A non-empty list =#
@error "Not listSetRest defined in the runtime"
end

""" O(n) """
function listArrayLiteral(lst::List{A})::Array{A} where {A<:Any}
function listArrayLiteral(lst::List{A})::Array{A} where {A}
local arr::Array{A} = listArray(lst)
arr
end
Expand Down
2 changes: 1 addition & 1 deletion src/metaRuntime.jl
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ function stringDelimitList(strs::List{String}, delimiter::String)::String
str
end

function stringDelimitList(strs::List{Any}, delimiter::String)::String
function stringDelimitList(strs::List, delimiter::String)::String
local str::String = ""
for n in strs
if isempty(str)
Expand Down

0 comments on commit 44a5d74

Please sign in to comment.