Skip to content

Commit

Permalink
Speedup.
Browse files Browse the repository at this point in the history
  • Loading branch information
corywalker committed Sep 23, 2017
1 parent dbbc890 commit bdf694e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion expreduce/ex_expression.go
Expand Up @@ -501,8 +501,8 @@ func (this *Expression) Hash() uint64 {
}
h := fnv.New64a()
h.Write([]byte{72, 5, 244, 86, 5, 210, 69, 30})
b := make([]byte, 8)
for _, part := range this.Parts {
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, part.Hash())
h.Write(b)
}
Expand Down
21 changes: 14 additions & 7 deletions expreduce/replace.go
Expand Up @@ -40,25 +40,31 @@ func FlatReplace(e *Expression, lhs *Expression, rhs Ex, orderless bool, es *Eva
return e
}

func ReplacePDInternal(e Ex, pm *PDManager) Ex {
func ReplacePDInternal(e Ex, pm *PDManager) (Ex, bool) {
asSym, isSym := e.(*Symbol)
if isSym {
for k, def := range pm.patternDefined {
if k == asSym.Name {
// Shouldn't need the copy
return def
return def, true
}
}
}
thisDirty := false
asExpr, isExpr := e.(*Expression)
if isExpr {
asExpr.evaledHash = 0
asExpr.cachedHash = 0
for i := range asExpr.Parts {
asExpr.Parts[i] = ReplacePDInternal(asExpr.Parts[i], pm)
possiblyNewExpr, dirty := ReplacePDInternal(asExpr.Parts[i], pm)
if dirty {
thisDirty = true
// Mark the expression as dirty and needing eval.
asExpr.evaledHash = 0
asExpr.cachedHash = 0
}
asExpr.Parts[i] = possiblyNewExpr
}
}
return e
return e, thisDirty
}

func ReplacePD(this Ex, es *EvalState, pm *PDManager) Ex {
Expand All @@ -78,7 +84,8 @@ func ReplacePD(this Ex, es *EvalState, pm *PDManager) Ex {

// Expressions are immutable. Any time we change an expression, we must
// first copy it.
return ReplacePDInternal(this.Copy(), pm)
res, _ := ReplacePDInternal(this.Copy(), pm)
return res
}

// The goal of this function is to replace all matching expressions with the
Expand Down

0 comments on commit bdf694e

Please sign in to comment.