fix overshoot and optimize better

This commit is contained in:
Barak Michener 2014-08-06 03:20:33 -04:00
parent 09244ddd38
commit 24f57df859
4 changed files with 21 additions and 10 deletions

View file

@ -348,7 +348,7 @@ func TestQueries(t *testing.T) {
// TODO(kortschak) Be more rigorous in this result validation. // TODO(kortschak) Be more rigorous in this result validation.
if len(got) != len(test.expect) { if len(got) != len(test.expect) {
t.Errorf("Unexpected number of results, got:%d expect:%d.", len(got), len(test.expect)) t.Errorf("Unexpected number of results, got:%d expect:%d on %s.", len(got), len(test.expect), test.message)
} }
} }
} }

View file

@ -154,11 +154,14 @@ func Next(it Iterator) (Value, bool) {
} }
// Height is a convienence function to measure the height of an iterator tree. // Height is a convienence function to measure the height of an iterator tree.
func Height(it Iterator) int { func Height(it Iterator, until Type) int {
if it.Type() == until {
return 1
}
subs := it.SubIterators() subs := it.SubIterators()
maxDepth := 0 maxDepth := 0
for _, sub := range subs { for _, sub := range subs {
h := Height(sub) h := Height(sub, until)
if h > maxDepth { if h > maxDepth {
maxDepth = h maxDepth = h
} }

View file

@ -300,7 +300,7 @@ func materializeIts(its []graph.Iterator) []graph.Iterator {
for _, it := range its { for _, it := range its {
stats := it.Stats() stats := it.Stats()
if stats.Size*stats.NextCost < stats.ContainsCost { if stats.Size*stats.NextCost < stats.ContainsCost {
if graph.Height(it) > 10 { if graph.Height(it, graph.Materialize) > 10 {
out = append(out, NewMaterialize(it)) out = append(out, NewMaterialize(it))
continue continue
} }

View file

@ -73,10 +73,16 @@ func (it *Materialize) TagResults(dst map[string]graph.Value) {
if !it.hasRun { if !it.hasRun {
return return
} }
if it.aborted {
it.subIt.TagResults(dst)
return
}
if it.lastIndex > len(it.values) {
return
}
for _, tag := range it.tags.Tags() { for _, tag := range it.tags.Tags() {
dst[tag] = it.Result() dst[tag] = it.Result()
} }
for tag, value := range it.values[it.lastIndex].tags { for tag, value := range it.values[it.lastIndex].tags {
dst[tag] = value dst[tag] = value
} }
@ -154,6 +160,7 @@ func (it *Materialize) Stats() graph.IteratorStats {
} }
func (it *Materialize) Next() (graph.Value, bool) { func (it *Materialize) Next() (graph.Value, bool) {
graph.NextLogIn(it)
if !it.hasRun { if !it.hasRun {
it.materializeSet() it.materializeSet()
} }
@ -164,14 +171,15 @@ func (it *Materialize) Next() (graph.Value, bool) {
lastVal := it.Result() lastVal := it.Result()
for it.lastIndex < len(it.values) { for it.lastIndex < len(it.values) {
it.lastIndex++ it.lastIndex++
if it.Result() != lastVal { if it.Result() != lastVal && it.Result() != nil {
return it.Result(), true return graph.NextLogOut(it, it.Result(), true)
} }
} }
return nil, false return graph.NextLogOut(it, nil, false)
} }
func (it *Materialize) Contains(v graph.Value) bool { func (it *Materialize) Contains(v graph.Value) bool {
graph.ContainsLogIn(it, v)
if !it.hasRun { if !it.hasRun {
it.materializeSet() it.materializeSet()
} }
@ -180,9 +188,9 @@ func (it *Materialize) Contains(v graph.Value) bool {
} }
if i, ok := it.containsMap[v]; ok { if i, ok := it.containsMap[v]; ok {
it.lastIndex = i it.lastIndex = i
return true return graph.ContainsLogOut(it, v, true)
} }
return false return graph.ContainsLogOut(it, v, false)
} }
func (it *Materialize) NextResult() bool { func (it *Materialize) NextResult() bool {