History M3: bound the block alignment and a restore's lookback
A page of 6,000 list items with seven versions took 902 MB to list its history: every edit's summary aligned all of both versions' blocks in a full LCS table, and parsed each body twice. - The blocks the two versions share at either end are matched first and cost nothing; the LCS runs only over the changed middle. - The middle's table is capped at 1<<20 cells (4 MB). Past it nothing is aligned or paired: the middle is removed and added whole, so a chosen version's diff is coarse and the summary is "Edited" (with the heading above the first change, which is still known exactly). - A restore's summary opens at most a screen (30) of older versions to find its source, else "Restored an earlier version". - Each body is parsed once while summarising a screen. The same page now lists its history in 36 MB; the test holds it under 50.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+221
−16
internal/mapa/diff.go+51 −7internal/mapa/diff_test.go+2 −1internal/mapa/versions.go+37 −8internal/mapa/versions_test.go+131 −0
diff --git a/internal/mapa/diff.go b/internal/mapa/diff.go| index 03acaa2..c13bbe0 100644 |
| --- a/internal/mapa/diff.go |
| +++ b/internal/mapa/diff.go |
| @@ -33,10 +33,12 @@ type blockChange struct { |
| // diffBlocks is the steps from one version's blocks to the next's. |
| // Unmatched blocks between two matches pair up in order as changed |
| // blocks when their words are alike; the rest are removed, then added. |
| +// Past the alignment's bound (matchBlocks), nothing pairs: the changed |
| +// middle is all removed, then all added. |
| func diffBlocks(from, to []markup.Block) []blockChange { |
| - matched := matchBlocks(from, to) |
| + matched, coarse := matchBlocks(from, to) |
| alike := func(was, now int) bool { |
| - return similar(tokensOf(from[was]), tokensOf(to[now])) |
| + return !coarse && similar(tokensOf(from[was]), tokensOf(to[now])) |
| } |
| var changes, removed []blockChange |
| next := 0 |
| @@ -110,15 +112,57 @@ func pairUp(removed []blockChange, start, end int, |
| return pairs |
| } |
| +// maxBlockCells bounds the block alignment's table, as maxWordCells |
| +// bounds the word diff's: 4 MB of it. The blocks the two versions share |
| +// at either end are matched first and cost nothing; past the bound, the |
| +// middle between them is not aligned at all (coarse). |
| +const maxBlockCells = 1 << 20 |
| + |
| // matchBlocks maps each block of from to its match in to, by a longest |
| // common subsequence of block sources, or -1 where it has none: the |
| // merge's alignment (markup's align), kept here so history needs |
| -// nothing new from markup. |
| -func matchBlocks(from, to []markup.Block) []int { |
| - same := func(row, column int) bool { |
| - return from[row].Source == to[column].Source |
| +// nothing new from markup. coarse reports a middle too large to align, |
| +// whose blocks are all -1. |
| +func matchBlocks(from, to []markup.Block) (matched []int, coarse bool) { |
| + head, tail := sharedEnds(from, to) |
| + rows, columns := len(from)-head-tail, len(to)-head-tail |
| + matched = make([]int, len(from)) |
| + for index := range matched { |
| + switch { |
| + case index < head: |
| + matched[index] = index |
| + case index >= head+rows: |
| + matched[index] = index + len(to) - len(from) |
| + default: |
| + matched[index] = -1 |
| + } |
| + } |
| + if rows*columns > maxBlockCells { |
| + return matched, true |
| + } |
| + middle := lcs(rows, columns, func(row, column int) bool { |
| + return from[head+row].Source == to[head+column].Source |
| + }) |
| + for row, column := range middle { |
| + if column >= 0 { |
| + matched[head+row] = head + column |
| + } |
| + } |
| + return matched, false |
| +} |
| + |
| +// sharedEnds is how many blocks the two versions share at the start, |
| +// and then at the end of what is left. |
| +func sharedEnds(from, to []markup.Block) (head, tail int) { |
| + shorter := min(len(from), len(to)) |
| + for head < shorter && from[head].Source == to[head].Source { |
| + head++ |
| + } |
| + for tail < shorter-head && from[len(from)-1-tail].Source == |
| + to[len(to)-1-tail].Source { |
| + tail++ |
| } |
| - return lcs(len(from), len(to), same) |
| + return head, tail |
| } |
| // lcs maps each of rows items to its match among columns, or -1, by a |
diff --git a/internal/mapa/diff_test.go b/internal/mapa/diff_test.go| index 351025b..f8a5f03 100644 |
| --- a/internal/mapa/diff_test.go |
| +++ b/internal/mapa/diff_test.go |
| @@ -25,7 +25,8 @@ func TestEditSummaries(t *testing.T) { |
| {strings.Replace(base, "Ana, budget.", "Ana, [[Budget]].", 1), |
| "Edited Contacts"}, |
| } { |
| - if got := editSummary(base, check.body); got != check.want { |
| + if got := editSummary(markup.Parse(base), |
| + markup.Parse(check.body)); got != check.want { |
| t.Errorf("%q: got %q, want %q", check.body, got, check.want) |
| } |
| } |
diff --git a/internal/mapa/versions.go b/internal/mapa/versions.go| index f3f29e7..8497a7c 100644 |
| --- a/internal/mapa/versions.go |
| +++ b/internal/mapa/versions.go |
| @@ -64,6 +64,15 @@ type pageHistory struct { |
| records map[int64]Record |
| verbs map[int64]string |
| names map[int64]string |
| + // parsed is the last version's blocks, which the next older edit's |
| + // summary compares against, so each body is parsed once. |
| + parsed parsedVersion |
| +} |
| + |
| +// parsedVersion is one revision's body as blocks. |
| +type parsedVersion struct { |
| + revision int64 |
| + blocks []markup.Block |
| } |
| // History is one screen of a page's versions, from skip on, if the |
| @@ -250,15 +259,21 @@ func (history *pageHistory) renamed(index int) (string, error) { |
| return "Renamed to " + record.Title, nil |
| } |
| +// maxRevertSearch bounds how many older versions one restore's summary |
| +// opens to find its source: a screen's worth. |
| +const maxRevertSearch = HistoryPage |
| + |
| // reverted names the version it restored: the newest one before it in |
| -// the window with the same text. Restore keeps no pointer to its source, |
| -// so with none in the window it cannot be named. |
| +// the window with the same text, among the maxRevertSearch before it. |
| +// Restore keeps no pointer to its source, so with none there it cannot |
| +// be named. |
| func (history *pageHistory) reverted(index int) (string, error) { |
| record, err := history.record(index) |
| if err != nil { |
| return "", err |
| } |
| - for older := index + 1; older < len(history.rows); older++ { |
| + last := min(len(history.rows), index+1+maxRevertSearch) |
| + for older := index + 1; older < last; older++ { |
| candidate, err := history.record(older) |
| if err != nil { |
| return "", err |
| @@ -287,21 +302,35 @@ func (history *pageHistory) edited(index int) (string, error) { |
| if index+1 >= len(history.rows) { |
| return "Edited", nil |
| } |
| - before, err := history.record(index + 1) |
| + now, err := history.blocks(index) |
| if err != nil { |
| return "", err |
| } |
| - record, err := history.record(index) |
| + before, err := history.blocks(index + 1) |
| if err != nil { |
| return "", err |
| } |
| - return editSummary(before.Body, record.Body), nil |
| + return editSummary(before, now), nil |
| +} |
| + |
| +// blocks is rows[index]'s body parsed, kept until another is asked for. |
| +func (history *pageHistory) blocks(index int) ([]markup.Block, error) { |
| + revision := history.rows[index].Revision |
| + if history.parsed.revision == revision { |
| + return history.parsed.blocks, nil |
| + } |
| + record, err := history.record(index) |
| + if err != nil { |
| + return nil, err |
| + } |
| + history.parsed = parsedVersion{revision: revision, |
| + blocks: markup.Parse(record.Body)} |
| + return history.parsed.blocks, nil |
| } |
| // editSummary is one line for an edit: "Added 2 blocks", "Removed a |
| // block", or "Edited" and the nearest heading above the first change. |
| -func editSummary(old, body string) string { |
| - from, to := markup.Parse(old), markup.Parse(body) |
| +func editSummary(from, to []markup.Block) string { |
| counts := map[string]int{} |
| var first *blockChange |
| for _, change := range diffBlocks(from, to) { |
diff --git a/internal/mapa/versions_test.go b/internal/mapa/versions_test.go| new file mode 100644 |
| index 0000000..f975fd2 |
| --- /dev/null |
| +++ b/internal/mapa/versions_test.go |
| @@ -0,0 +1,131 @@ |
| +package mapa |
| + |
| +import ( |
| + "runtime" |
| + "strconv" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "amadan.net/oficina/mapa/internal/mapa/markup" |
| +) |
| + |
| +// longList is a page of count list items, the nth reading word. |
| +func longList(count int, word func(item int) string) string { |
| + var out strings.Builder |
| + for item := range count { |
| + out.WriteString("- Item " + strconv.Itoa(item) + " " + word(item) + |
| + "\n") |
| + } |
| + return out.String() |
| +} |
| + |
| +// allocated is the bytes run allocates. |
| +func allocated(run func()) uint64 { |
| + var before, after runtime.MemStats |
| + runtime.GC() |
| + runtime.ReadMemStats(&before) |
| + run() |
| + runtime.ReadMemStats(&after) |
| + return after.TotalAlloc - before.TotalAlloc |
| +} |
| + |
| +// Review M3: history on a long page is bounded. A page of 6,000 list |
| +// items edited here and there, then rewritten throughout, reads its |
| +// history in a modest allocation (902 MB before the bound), and the |
| +// rewrite, too large to align, is summarised as plain "Edited". |
| +func TestALongPagesHistoryIsBounded(t *testing.T) { |
| + store := testStore(t) |
| + clock := time.Now().UTC() |
| + store.SetClock(func() time.Time { return clock }) |
| + edited := map[int]bool{} |
| + word := func(item int) string { |
| + if edited[item] { |
| + return "was edited" |
| + } |
| + return "stays as it was" |
| + } |
| + page := mustCreate(t, store, "Long", longList(6000, word)) |
| + for edit := range 5 { |
| + clock = clock.Add(time.Minute) |
| + edited[edit*1000+500] = true |
| + page = commitBody(t, store, page, longList(6000, word)) |
| + } |
| + clock = clock.Add(time.Minute) |
| + page = commitBody(t, store, page, longList(6000, func(int) string { |
| + return "rewritten throughout" |
| + })) |
| + var summaries []string |
| + used := allocated(func() { |
| + list, err := store.History(ctx(), viewerOf(t, store, ana), page.ID, |
| + 0) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, version := range list.Versions { |
| + summaries = append(summaries, version.Summary) |
| + } |
| + }) |
| + t.Logf("history of a 6,000-block page: %d MB", used>>20) |
| + if used > 50<<20 { |
| + t.Errorf("history allocated %d MB, want under 50", used>>20) |
| + } |
| + want := "Edited | Edited | Edited | Edited | Edited | Edited | " + |
| + "Created this page" |
| + if got := strings.Join(summaries, " | "); got != want { |
| + t.Errorf("summaries %q, want %q", got, want) |
| + } |
| +} |
| + |
| +// Past the bound, a chosen version's diff is coarse: the changed middle |
| +// removed and added whole, between the blocks the versions share. |
| +func TestATooLargeChangeIsRemovedAndAddedWhole(t *testing.T) { |
| + old := markup.Parse("Kept first.\n\n" + longList(1100, |
| + func(int) string { return "old" }) + "\nKept last.") |
| + now := markup.Parse("Kept first.\n\n" + longList(1100, |
| + func(int) string { return "new" }) + "\nKept last.") |
| + counts := map[string]int{} |
| + for _, change := range diffBlocks(old, now) { |
| + counts[change.Kind]++ |
| + } |
| + if counts[blockSame] != 2 || counts[blockRemoved] != 1100 || |
| + counts[blockAdded] != 1100 || counts[blockChanged] != 0 { |
| + t.Fatalf("changes %v", counts) |
| + } |
| +} |
| + |
| +// A restore's summary looks back a screen of versions for its source, |
| +// and no further: past that it is "Restored an earlier version". |
| +func TestARestoreLooksBackABoundedWay(t *testing.T) { |
| + store := testStore(t) |
| + clock := time.Now().UTC() |
| + store.SetClock(func() time.Time { return clock }) |
| + page := mustCreate(t, store, "Notes", "First.") |
| + first := page.Revision |
| + for edit := range maxRevertSearch { |
| + clock = clock.Add(time.Minute) |
| + page = commitBody(t, store, page, "Edit "+strconv.Itoa(edit)+".") |
| + } |
| + clock = clock.Add(time.Minute) |
| + if err := store.Revert(ctx(), ana, page.ID, first); err != nil { |
| + t.Fatal(err) |
| + } |
| + list, err := store.History(ctx(), viewerOf(t, store, ana), page.ID, 0) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if got := list.Versions[0].Summary; got != "Restored an earlier "+ |
| + "version" { |
| + t.Fatalf("summary %q", got) |
| + } |
| +} |
| + |
| +// commitBody saves body over page's current revision. |
| +func commitBody(t *testing.T, store *Store, page Page, body string) Page { |
| + t.Helper() |
| + if _, err := store.Commit(ctx(), ana, page.ID, page.Revision, |
| + body); err != nil { |
| + t.Fatal(err) |
| + } |
| + return loadPage(t, store, page.ID) |
| +} |