diff options
| author | Marcos Magueta <maguetamarcos@gmail.com> | 2026-09-13 14:59:29 -0300 |
|---|---|---|
| committer | Marcos Magueta <maguetamarcos@gmail.com> | 2026-09-13 14:59:29 -0300 |
| commit | 49af3fc7c2e3b27f5925c153ce6ce9bd05f19c45 (patch) | |
| tree | 5920b57ba4f85cc3d3cb1e4920b4edfef064f129 /lib/kernel/SubstantialRelation.ml | |
| parent | 097cf958cf25c63035f6434f12b421dd9ab1c60e (diff) | |
| download | RNT-49af3fc7c2e3b27f5925c153ce6ce9bd05f19c45.tar.gz RNT-49af3fc7c2e3b27f5925c153ce6ce9bd05f19c45.zip | |
actual lazy substantial relation
Diffstat (limited to '')
| -rw-r--r-- | lib/kernel/SubstantialRelation.ml | 38 |
1 files changed, 15 insertions, 23 deletions
diff --git a/lib/kernel/SubstantialRelation.ml b/lib/kernel/SubstantialRelation.ml index 3094eb8..6592acb 100644 --- a/lib/kernel/SubstantialRelation.ml +++ b/lib/kernel/SubstantialRelation.ml @@ -126,33 +126,25 @@ module Make (S : Abstract.Storage.STORAGE) = struct let open Utilities.Result in let* tx = S.start storage in let* node = tuple_node tx relation in - (* TODO: `TupleSet.keys` walks the whole Merkle structure eagerly, - before the first tuple is yielded. Fine for this sketch; a real - implementation should walk it lazily instead (its own small - generator, or an explicit stack over Merkle.node) so that - stacking e.g. `restrict` on a huge relation does not pay for the - entire scan before the first `next`. *) - let* keys = TupleSet.keys tx node in - let remaining = ref keys in let closed = ref false in let close () = if not !closed then begin closed := true; ignore (S.abort tx) end in + (* The fold is suspended by [yield], so the tree is walked one node + and one tuple at a time rather than materialized before the + first tuple is produced. *) let produce ~yield = - let rec loop () = - match BatFingerTree.front !remaining with - | None -> - close (); - Ok () - | Some (rest, addr) -> - remaining := rest; - match SI.get_req tx (S.Hash addr) |> fmap Concepts.Tuple.Representation.of_blob with - | Error _ as e -> - close (); - e - | Ok tuple -> - yield tuple; - loop () + let* walked = + TupleSet.fold_left tx + (fun acc _ addr -> + let* () = acc in + let* tuple = + SI.get_req tx (S.Hash addr) |> fmap Concepts.Tuple.Representation.of_blob + in + yield tuple; + Ok ()) + (Ok ()) node in - loop () + close (); + walked in Ok (Generator.cursor_of ~on_release:close produce) |
