Skip to content

Commit b51e350

Browse files
authored
edge_query: use shapeID instead of index.idForShape lookup (golang#136)
Performance of maybeAddResult is not great since it invokes idForShape twice. idForShape iterates through all the shapes in shapeIndex to find id for the shape. If the ShapeIndex contains a big number of shapes it can be quite a big task to find the id for the shape. Since shapeID is available for maybeAddResult use it instead of idForShape.
1 parent dc45a10 commit b51e350

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

s2/edge_query.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -498,28 +498,28 @@ func (e *EdgeQuery) addResult(r EdgeQueryResult) {
498498
// is used for the results.
499499
}
500500

501-
func (e *EdgeQuery) maybeAddResult(shape Shape, edgeID int32) {
502-
if _, ok := e.testedEdges[ShapeEdgeID{e.index.idForShape(shape), edgeID}]; e.avoidDuplicates && !ok {
501+
func (e *EdgeQuery) maybeAddResult(shape Shape, shapeID, edgeID int32) {
502+
if _, ok := e.testedEdges[ShapeEdgeID{shapeID, edgeID}]; e.avoidDuplicates && !ok {
503503
return
504504
}
505505
edge := shape.Edge(int(edgeID))
506506
dist := e.distanceLimit
507507

508508
if dist, ok := e.target.updateDistanceToEdge(edge, dist); ok {
509-
e.addResult(EdgeQueryResult{dist, e.index.idForShape(shape), edgeID})
509+
e.addResult(EdgeQueryResult{dist, shapeID, edgeID})
510510
}
511511
}
512512

513513
func (e *EdgeQuery) findEdgesBruteForce() {
514514
// Range over all shapes in the index. Does order matter here? if so
515515
// switch to for i = 0 .. n?
516-
for _, shape := range e.index.shapes {
516+
for shapeID, shape := range e.index.shapes {
517517
// TODO(roberts): can this happen if we are only ranging over current entries?
518518
if shape == nil {
519519
continue
520520
}
521521
for edgeID := int32(0); edgeID < int32(shape.NumEdges()); edgeID++ {
522-
e.maybeAddResult(shape, edgeID)
522+
e.maybeAddResult(shape, shapeID, edgeID)
523523
}
524524
}
525525
}
@@ -749,7 +749,7 @@ func (e *EdgeQuery) processEdges(entry *queryQueueEntry) {
749749
for _, clipped := range entry.indexCell.shapes {
750750
shape := e.index.Shape(clipped.shapeID)
751751
for j := 0; j < clipped.numEdges(); j++ {
752-
e.maybeAddResult(shape, int32(clipped.edges[j]))
752+
e.maybeAddResult(shape, clipped.shapeID, int32(clipped.edges[j]))
753753
}
754754
}
755755
}

0 commit comments

Comments
 (0)