Skip to content

Commit 0423914

Browse files
committed
CONTRIBUTING: Document chain-of-segments pattern
1 parent 4644e4a commit 0423914

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

CONTRIBUTING.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,20 @@ Cache parameter guidelines:
899899
- Set `max` and `limit` high enough to avoid evictions when memory allows
900900

901901

902+
**Persistent chain of segments for per-state DP**
903+
904+
When an optimiser maintains a candidate sequence per state and extends them
905+
per character, the naive `getinterval putinterval` copy-and-extend pattern is
906+
O(n^2) overall. Represent each candidate as a persistent chain of segments —
907+
a linked list of `[parent_node, segment_array]` cons cells where extension
908+
adds a new head node sharing the existing prefix via the parent pointer.
909+
Materialise once at the end by walking head-to-root.
910+
911+
This is the pattern used by `azteccode`, `pdf417` and `micropdf417` in their
912+
encoding optimisers; see `pdf417.ps.src` (simplest variant) for the
913+
canonical form including the `flatten` helper.
914+
915+
902916
### Anti-patterns
903917

904918
- Creating variables (dictionary entries) in hot loops
@@ -944,6 +958,9 @@ pre-allocate and, if size is bounded, over-size then trim with `getinterval`.
944958
Extension of small arrays with `/arr [ arr aload pop ... ] def` is
945959
acceptable for one-time operations outside loops.
946960

961+
For the per-state case where many candidate sequences are extended in
962+
parallel, see "Persistent chain of segments for per-state DP" above.
963+
947964

948965
**Hot Loop Stack Pattern**
949966

0 commit comments

Comments
 (0)