|
| 1 | +package dotty.tools.dotc |
| 2 | +package transform |
| 3 | +package init |
| 4 | + |
| 5 | +import core.* |
| 6 | +import Contexts.* |
| 7 | + |
| 8 | +import ast.tpd |
| 9 | +import tpd.Tree |
| 10 | + |
| 11 | +class Cache[Config, Res]: |
| 12 | + import Cache.* |
| 13 | + |
| 14 | + /** The cache for expression values from last iteration */ |
| 15 | + protected var last: ExprValueCache[Config, Res] = Map.empty |
| 16 | + |
| 17 | + /** The output cache for expression values |
| 18 | + * |
| 19 | + * The output cache is computed based on the cache values `last` from the |
| 20 | + * last iteration. |
| 21 | + * |
| 22 | + * Both `last` and `current` are required to make sure an encountered |
| 23 | + * expression is evaluated once in each iteration. |
| 24 | + */ |
| 25 | + protected var current: ExprValueCache[Config, Res] = Map.empty |
| 26 | + |
| 27 | + /** Whether the current heap is different from the last heap? |
| 28 | + * |
| 29 | + * `changed == false` implies that the fixed point has been reached. |
| 30 | + */ |
| 31 | + protected var changed: Boolean = false |
| 32 | + |
| 33 | + /** Used to avoid allocation, its state does not matter */ |
| 34 | + protected given MutableTreeWrapper = new MutableTreeWrapper |
| 35 | + |
| 36 | + def get(config: Config, expr: Tree): Option[Res] = |
| 37 | + current.get(config, expr) |
| 38 | + |
| 39 | + /** Copy the value (config, expr)` from the last cache to the current cache |
| 40 | + * |
| 41 | + * It assumes `default` if it doesn't exist in the last cache. |
| 42 | + * |
| 43 | + * It updates the current caches if the values change. |
| 44 | + * |
| 45 | + * The two caches are required because we want to make sure in a new iteration, an expression is evaluated once. |
| 46 | + */ |
| 47 | + def cachedEval(config: Config, expr: Tree, cacheResult: Boolean, default: Res)(eval: => Res): Res = |
| 48 | + this.get(config, expr) match |
| 49 | + case Some(value) => value |
| 50 | + case None => |
| 51 | + val assumeValue: Res = |
| 52 | + last.get(config, expr) match |
| 53 | + case Some(value) => value |
| 54 | + case None => |
| 55 | + this.last = last.updatedNested(config, expr, default) |
| 56 | + default |
| 57 | + |
| 58 | + this.current = current.updatedNested(config, expr, assumeValue) |
| 59 | + |
| 60 | + val actual = eval |
| 61 | + if actual != assumeValue then |
| 62 | + // println("Changed! from = " + assumeValue + ", to = " + actual) |
| 63 | + this.changed = true |
| 64 | + // TODO: respect cacheResult to reduce cache size |
| 65 | + this.current = this.current.updatedNested(config, expr, actual) |
| 66 | + // this.current = this.current.removed(config, expr) |
| 67 | + end if |
| 68 | + |
| 69 | + actual |
| 70 | + end cachedEval |
| 71 | + |
| 72 | + def hasChanged = changed |
| 73 | + |
| 74 | + /** Prepare cache for the next iteration |
| 75 | + * |
| 76 | + * 1. Reset changed flag. |
| 77 | + * |
| 78 | + * 2. Use current cache as last cache and set current cache to be empty. |
| 79 | + */ |
| 80 | + def prepareForNextIteration()(using Context) = |
| 81 | + this.changed = false |
| 82 | + this.last = this.current |
| 83 | + this.current = Map.empty |
| 84 | +end Cache |
| 85 | + |
| 86 | +object Cache: |
| 87 | + type ExprValueCache[Config, Res] = Map[Config, Map[TreeWrapper, Res]] |
| 88 | + |
| 89 | + /** A wrapper for trees for storage in maps based on referential equality of trees. */ |
| 90 | + abstract class TreeWrapper: |
| 91 | + def tree: Tree |
| 92 | + |
| 93 | + override final def equals(other: Any): Boolean = |
| 94 | + other match |
| 95 | + case that: TreeWrapper => this.tree eq that.tree |
| 96 | + case _ => false |
| 97 | + |
| 98 | + override final def hashCode = tree.hashCode |
| 99 | + |
| 100 | + /** The immutable wrapper is intended to be stored as key in the heap. */ |
| 101 | + class ImmutableTreeWrapper(val tree: Tree) extends TreeWrapper |
| 102 | + |
| 103 | + /** For queries on the heap, reuse the same wrapper to avoid unnecessary allocation. |
| 104 | + * |
| 105 | + * A `MutableTreeWrapper` is only ever used temporarily for querying a map, |
| 106 | + * and is never inserted to the map. |
| 107 | + */ |
| 108 | + class MutableTreeWrapper extends TreeWrapper: |
| 109 | + var queryTree: Tree | Null = null |
| 110 | + def tree: Tree = queryTree match |
| 111 | + case tree: Tree => tree |
| 112 | + case null => ??? |
| 113 | + |
| 114 | + extension [Config, Res](cache: ExprValueCache[Config, Res]) |
| 115 | + def get(config: Config, expr: Tree)(using queryWrapper: MutableTreeWrapper): Option[Res] = |
| 116 | + queryWrapper.queryTree = expr |
| 117 | + cache.get(config).flatMap(_.get(queryWrapper)) |
| 118 | + |
| 119 | + def removed(config: Config, expr: Tree)(using queryWrapper: MutableTreeWrapper) = |
| 120 | + queryWrapper.queryTree = expr |
| 121 | + val innerMap2 = cache(config).removed(queryWrapper) |
| 122 | + cache.updated(config, innerMap2) |
| 123 | + |
| 124 | + def updatedNested(config: Config, expr: Tree, result: Res): ExprValueCache[Config, Res] = |
| 125 | + val wrapper = new ImmutableTreeWrapper(expr) |
| 126 | + updatedNestedWrapper(config, wrapper, result) |
| 127 | + |
| 128 | + def updatedNestedWrapper(config: Config, wrapper: ImmutableTreeWrapper, result: Res): ExprValueCache[Config, Res] = |
| 129 | + val innerMap = cache.getOrElse(config, Map.empty[TreeWrapper, Res]) |
| 130 | + val innerMap2 = innerMap.updated(wrapper, result) |
| 131 | + cache.updated(config, innerMap2) |
| 132 | + end extension |
0 commit comments