Skip to content

Commit

Permalink
Much faster evaluation now that EmptyPD() lazily creates a map.
Browse files Browse the repository at this point in the history
  • Loading branch information
corywalker committed Sep 5, 2017
1 parent 54686af commit 92c72c6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
1 change: 1 addition & 0 deletions expreduce/blank.go
Expand Up @@ -56,6 +56,7 @@ func IsBlankTypeCapturing(e Ex, target Ex, head Ex, pm *PDManager, cl *CASLogger
toMatch, ispd := pm.patternDefined[sAsSymbol.Name]
if !ispd {
toMatch = target
pm.LazyMakeMap()
pm.patternDefined[sAsSymbol.Name] = target
}
if !IsSameQ(toMatch, target, cl) {
Expand Down
1 change: 1 addition & 0 deletions expreduce/builtin_system.go
Expand Up @@ -195,6 +195,7 @@ func applyModuleFn(this *Expression, es *EvalState) (Ex, bool) {
} else {
es.defined[pl.uniqueName] = Def{}
}
pm.LazyMakeMap()
pm.patternDefined[pl.sym.Name] = &Symbol{pl.uniqueName}
}
toReturn = ReplacePD(toReturn, es, pm)
Expand Down
25 changes: 25 additions & 0 deletions expreduce/cas_test.go
Expand Up @@ -110,6 +110,31 @@ func TestLowLevel(t *testing.T) {

es := NewEvalState()

lhs := NewExpression([]Ex{
&Symbol{"System`Power"},
NewExpression([]Ex{
&Symbol{"System`Plus"},
&Symbol{"Global`a"},
&Symbol{"Global`b"},
&Symbol{"Global`c"},
}),
NewInt(0),
})
rule := NewExpression([]Ex{
&Symbol{"System`Rule"},
NewExpression([]Ex{
&Symbol{"System`Power"},
NewExpression([]Ex{
&Symbol{"System`Blank"},
}),
NewInt(0),
}),
NewInt(99),
})
for numi := 0; numi < 700000; numi++ {
Replace(lhs, rule, es)
}

// Test basic float functionality
var f *Flt = &Flt{big.NewFloat(5.5)}
assert.Equal(t, "5.5", f.String())
Expand Down
32 changes: 25 additions & 7 deletions expreduce/pdmanager.go
Expand Up @@ -11,26 +11,41 @@ type PDManager struct {
}

func EmptyPD() *PDManager {
return &PDManager{make(map[string]Ex)}
return &PDManager{nil}
}

func CopyPD(orig *PDManager) (dest *PDManager) {
dest = EmptyPD()
// We do not care that this iterates in a random order.
for k, v := range (*orig).patternDefined {
(*dest).patternDefined[k] = v.DeepCopy()
if (*orig).Len() > 0 {
dest.LazyMakeMap()
for k, v := range (*orig).patternDefined {
(*dest).patternDefined[k] = v.DeepCopy()
}
}
return
}

func (this *PDManager) LazyMakeMap() {
if this.patternDefined == nil {
this.patternDefined = make(map[string]Ex)
}
}

func (this *PDManager) Update(toAdd *PDManager) {
if (*toAdd).Len() > 0 {
this.LazyMakeMap()
}
// We do not care that this iterates in a random order.
for k, v := range (*toAdd).patternDefined {
(*this).patternDefined[k] = v
}
}

func (this *PDManager) Len() int {
if this.patternDefined == nil {
return 0
}
return len(this.patternDefined)
}

Expand Down Expand Up @@ -94,11 +109,14 @@ func DefineSequence(lhs parsedForm, sequence []Ex, pm *PDManager, sequenceHead s
attemptDefine = NewExpression(append([]Ex{head}, sequence...))
}

defined, ispd := pm.patternDefined[lhs.patSym.Name]
if ispd && !IsSameQ(defined, attemptDefine, &es.CASLogger) {
es.Debugf("patterns do not match! continuing.")
return false
if pm.patternDefined != nil {
defined, ispd := pm.patternDefined[lhs.patSym.Name]
if ispd && !IsSameQ(defined, attemptDefine, &es.CASLogger) {
es.Debugf("patterns do not match! continuing.")
return false
}
}
pm.LazyMakeMap()
pm.patternDefined[lhs.patSym.Name] = attemptDefine
}
return true
Expand Down

0 comments on commit 92c72c6

Please sign in to comment.