Skip to content

Commit

Permalink
Speedup by using Replace instead of ReplaceAll.
Browse files Browse the repository at this point in the history
  • Loading branch information
corywalker committed Jul 16, 2017
1 parent 2933d38 commit 823857f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 19 deletions.
2 changes: 1 addition & 1 deletion expreduce/builtin_replacement.go
Expand Up @@ -224,7 +224,7 @@ func getReplacementDefinitions() (defs []Definition) {
return this
}
for _, rule := range rules {
toReturn, replaced := Replace(this.Parts[1], rule, es, EmptyPD(), "")
toReturn, replaced := Replace(this.Parts[1], rule, es)
if replaced {
return toReturn
}
Expand Down
5 changes: 2 additions & 3 deletions expreduce/evalstate.go
Expand Up @@ -145,9 +145,8 @@ func (this *EvalState) GetDef(name string, lhs Ex) (Ex, bool, *Expression) {
started = time.Now().UnixNano()
}

ismatchq, _ := IsMatchQ(lhs, def.Parts[1], EmptyPD(), this)
if ismatchq {
res := ReplaceAll(lhs, &def, this, EmptyPD(), "")
res, replaced := Replace(lhs, &def, this)
if replaced {
return res, true, &def
}

Expand Down
22 changes: 7 additions & 15 deletions expreduce/replace.go
Expand Up @@ -101,19 +101,9 @@ func ReplacePD(this Ex, es *EvalState, pm *PDManager) Ex {
// Final named pattern substitution will occur at the last possible time.
func ReplaceAll(this Ex, r *Expression, es *EvalState, pm *PDManager,
stopAtHead string) Ex {
_, isFlt := this.(*Flt)
_, isInteger := this.(*Integer)
_, isString := this.(*String)
asExpression, isExpression := this.(*Expression)
_, isSymbol := this.(*Symbol)
_, isRational := this.(*Rational)

if isFlt || isInteger || isString || isSymbol || isRational {
if res, matches := IsMatchQ(this, r.Parts[1], pm, es); res {
return ReplacePD(r.Parts[2], es, matches)
}
return this
} else if isExpression {
if isExpression {
_, isRestrictedHead := HeadAssertion(this, stopAtHead)
if isRestrictedHead {
return this
Expand All @@ -123,12 +113,14 @@ func ReplaceAll(this Ex, r *Expression, es *EvalState, pm *PDManager,
return asExpression.ReplaceAll(r, stopAtHead, es)
}
}
return &Symbol{"$ReplaceAllFailed"}
if res, matches := IsMatchQ(this, r.Parts[1], pm, es); res {
return ReplacePD(r.Parts[2], es, matches)
}
return this
}

func Replace(this Ex, r *Expression, es *EvalState, pm *PDManager,
stopAtHead string) (Ex, bool) {
if res, matches := IsMatchQ(this, r.Parts[1], pm, es); res {
func Replace(this Ex, r *Expression, es *EvalState) (Ex, bool) {
if res, matches := IsMatchQ(this, r.Parts[1], EmptyPD(), es); res {
return ReplacePD(r.Parts[2], es, matches), true
}
return this, false
Expand Down

0 comments on commit 823857f

Please sign in to comment.