Skip to content
Permalink
Browse files

Make Hash.push/append between 20% and 40% faster

Depending on whether the key in question already existed or not
  • Loading branch information...
lizmat committed Sep 19, 2018
1 parent d275ea0 commit 12cfde2afd71b95eae65c4fff5f3d3e98adf1717
Showing with 10 additions and 10 deletions.
  1. +10 −10 src/core/Hash.pm6
@@ -412,20 +412,20 @@ my class Hash { # declared in BOOTSTRAP

# push a value onto a hash slot, constructing an array if necessary
method !_push_construct(Mu $key, Mu \value --> Nil) {
self.EXISTS-KEY($key)
?? self.AT-KEY($key).^isa(Array)
?? self.AT-KEY($key).push(value)
!! self.ASSIGN-KEY($key,[self.AT-KEY($key),value])
!! self.ASSIGN-KEY($key,value)
nqp::if(
nqp::istype((my \current := self.AT-KEY($key)),Array),
current.push(value),
current = nqp::if(self.EXISTS-KEY($key),[current,value],value)
)
}

# append values into a hash slot, constructing an array if necessary
method !_append_construct(Mu $key, Mu \value --> Nil) {
self.EXISTS-KEY($key)
?? self.AT-KEY($key).^isa(Array)
?? self.AT-KEY($key).append(|value)
!! self.ASSIGN-KEY($key,[|self.AT-KEY($key),|value])
!! self.ASSIGN-KEY($key,value)
nqp::if(
nqp::istype((my \current := self.AT-KEY($key)),Array),
current.append(|value),
current = nqp::if(self.EXISTS-KEY($key),[|current,|value],value)
)
}

my role TypedHash[::TValue] does Associative[TValue] {

0 comments on commit 12cfde2

Please sign in to comment.
You can’t perform that action at this time.