Skip to content

Commit

Permalink
Aggressive hash caching.
Browse files Browse the repository at this point in the history
  • Loading branch information
corywalker committed Jul 20, 2017
1 parent 1380e28 commit 0b1fd05
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 24 deletions.
11 changes: 4 additions & 7 deletions expreduce/ex_expression.go
Expand Up @@ -237,6 +237,7 @@ func (this *Expression) Eval(es *EvalState) Ex {
}
if attrs.Orderless {
sort.Sort(curr)
curr.cachedHash = 0
}
if attrs.Listable {
changed := false
Expand Down Expand Up @@ -464,20 +465,16 @@ func (this *Expression) NeedsEval() bool {
}

func (this *Expression) Hash() uint64 {
// Will generate stale hashes but offers significant speedup. Use with care.
//if this.cachedHash > 0 {
//return this.cachedHash
//}
if this.cachedHash > 0 {
return this.cachedHash
}
h := fnv.New64a()
h.Write([]byte{72, 5, 244, 86, 5, 210, 69, 30})
for _, part := range this.Parts {
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, part.Hash())
h.Write(b)
}
//if this.cachedHash > 0 && h.Sum64() != this.cachedHash {
//fmt.Printf("%v stale hash!!!\n", this)
//}
this.cachedHash = h.Sum64()
return h.Sum64()
}
Expand Down
20 changes: 3 additions & 17 deletions expreduce/sameq.go
Expand Up @@ -11,30 +11,16 @@ func IsSameQ(a Ex, b Ex, cl *CASLogger) bool {
_, bIsSymbol := b.(*Symbol)
_, aIsRational := a.(*Rational)
_, bIsRational := b.(*Rational)
aExpression, aIsExpression := a.(*Expression)
bExpression, bIsExpression := b.(*Expression)
_, aIsExpression := a.(*Expression)
_, bIsExpression := b.(*Expression)

if (aIsFlt && bIsFlt) || (aIsString && bIsString) || (aIsInteger && bIsInteger) || (aIsSymbol && bIsSymbol) || (aIsRational && bIsRational) {
// a and b are identical raw types
return a.IsEqual(b, cl) == "EQUAL_TRUE"
} else if aIsExpression && bIsExpression {
// a and b are both expressions
return FunctionIsSameQ(aExpression.Parts, bExpression.Parts, cl)
return a.Hash() == b.Hash()
}

// This should never happen
return false
}

func FunctionIsSameQ(components []Ex, other_components []Ex, cl *CASLogger) bool {
if len(components) != len(other_components) {
return false
}
for i := range components {
res := IsSameQ(components[i], other_components[i], cl)
if !res {
return false
}
}
return true
}

0 comments on commit 0b1fd05

Please sign in to comment.