| 1 | // Copyright 2025 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package recursiveiter defines an Analyzer that checks for mistakes |
| 6 | // in iterators for recursive data structures. |
| 7 | // |
| 8 | // # Analyzer recursiveiter |
| 9 | // |
| 10 | // recursiveiter: check for inefficient recursive iterators |
| 11 | // |
| 12 | // This analyzer reports when a function that returns an iterator |
| 13 | // (iter.Seq or iter.Seq2) calls itself as the operand of a range |
| 14 | // statement, as this is inefficient. |
| 15 | // |
| 16 | // When implementing an iterator (e.g. iter.Seq[T]) for a recursive |
| 17 | // data type such as a tree or linked list, it is tempting to |
| 18 | // recursively range over the iterator for each child element. |
| 19 | // |
| 20 | // Here's an example of a naive iterator over a binary tree: |
| 21 | // |
| 22 | // type tree struct { |
| 23 | // value int |
| 24 | // left, right *tree |
| 25 | // } |
| 26 | // |
| 27 | // func (t *tree) All() iter.Seq[int] { |
| 28 | // return func(yield func(int) bool) { |
| 29 | // if t != nil { |
| 30 | // for elem := range t.left.All() { // "inefficient recursive iterator" |
| 31 | // if !yield(elem) { |
| 32 | // return |
| 33 | // } |
| 34 | // } |
| 35 | // if !yield(t.value) { |
| 36 | // return |
| 37 | // } |
| 38 | // for elem := range t.right.All() { // "inefficient recursive iterator" |
| 39 | // if !yield(elem) { |
| 40 | // return |
| 41 | // } |
| 42 | // } |
| 43 | // } |
| 44 | // } |
| 45 | // } |
| 46 | // |
| 47 | // Though it correctly enumerates the elements of the tree, it hides a |
| 48 | // significant performance problem--two, in fact. Consider a balanced |
| 49 | // tree of N nodes. Iterating the root node will cause All to be |
| 50 | // called once on every node of the tree. This results in a chain of |
| 51 | // nested active range-over-func statements when yield(t.value) is |
| 52 | // called on a leaf node. |
| 53 | // |
| 54 | // The first performance problem is that each range-over-func |
| 55 | // statement must typically heap-allocate a variable, so iteration of |
| 56 | // the tree allocates as many variables as there are elements in the |
| 57 | // tree, for a total of O(N) allocations, all unnecessary. |
| 58 | // |
| 59 | // The second problem is that each call to yield for a leaf of the |
| 60 | // tree causes each of the enclosing range loops to receive a value, |
| 61 | // which they then immediately pass on to their respective yield |
| 62 | // function. This results in a chain of log(N) dynamic yield calls per |
| 63 | // element, a total of O(N*log N) dynamic calls overall, when only |
| 64 | // O(N) are necessary. |
| 65 | // |
| 66 | // A better implementation strategy for recursive iterators is to |
| 67 | // first define the "every" operator for your recursive data type, |
| 68 | // where every(f) reports whether an arbitrary predicate f(x) is true |
| 69 | // for every element x in the data type. For our tree, the every |
| 70 | // function would be: |
| 71 | // |
| 72 | // func (t *tree) every(f func(int) bool) bool { |
| 73 | // return t == nil || |
| 74 | // t.left.every(f) && f(t.value) && t.right.every(f) |
| 75 | // } |
| 76 | // |
| 77 | // For example, this use of the every operator prints whether every |
| 78 | // element in the tree is an even number: |
| 79 | // |
| 80 | // even := func(x int) bool { return x&1 == 0 } |
| 81 | // println(t.every(even)) |
| 82 | // |
| 83 | // Then the iterator can be simply expressed as a trivial wrapper |
| 84 | // around the every operator: |
| 85 | // |
| 86 | // func (t *tree) All() iter.Seq[int] { |
| 87 | // return func(yield func(int) bool) { |
| 88 | // _ = t.every(yield) |
| 89 | // } |
| 90 | // } |
| 91 | // |
| 92 | // In effect, tree.All computes whether yield returns true for each |
| 93 | // element, short-circuiting if it ever returns false, then discards |
| 94 | // the final boolean result. |
| 95 | // |
| 96 | // This has much better performance characteristics: it makes one |
| 97 | // dynamic call per element of the tree, and it doesn't heap-allocate |
| 98 | // anything. It is also clearer. |
| 99 | package recursiveiter |