Skip to content

Replace per-save-point array snapshots with an undo trail - #870

Merged
natecook1000 merged 7 commits into
mainfrom
savepoint-simplify
Sep 22, 2026
Merged

natecook1000 merged 7 commits into
mainfrom
savepoint-simplify

Conversation

@natecook1000

Copy link
Copy Markdown
Member

Creating a save point previously copied the four mutable register arrays (ints, positions, values, and storedCaptures) to enable restoration during backtracking. This led to a large amount of reference counting traffic and copy-on-write of the original register arrays whenever they were updated.

This change moves the restoration data from using full copies of each array in every save point, to tracking a log of the changes to each array in the processor itself and only storing the current state of the logs in each save point. This allows SavePoint to be a trivial type (almost) and removes all the copy-on-write traffic. Backtracking now uses the tracking logs to unwind the registers to the specific previous state.

This change also includes a minor refactor, moving the storedCaptures array to the Registers struct, alongside the other mutable registers. Future work includes breaking out the mutable registers into their own type, creating a flat structure for these instead of maintaining four different dynamically allocated arrays, and moving further toward making SavePoint trivial.

@natecook1000
natecook1000 requested a review from milseman July 29, 2026 15:55
@natecook1000
natecook1000 force-pushed the savepoint-simplify branch 3 times, most recently from bb51e50 to 819dd70 Compare August 27, 2026 04:20
Creating a save point previously copied the four mutable register arrays
(ints, positions, values, and storedCaptures) to enable restoration
during backtracking. This led to a large amount of reference counting
traffic and copy-on-write of the original register arrays whenever they
were updated.

This change moves the restoration data from using full copies of each
array in every save point, to tracking a log of the changes to each array
in the processor itself and only storing the current state of the logs
in each save point. This allows `SavePoint` to be a trivial type (almost)
and removes all the copy-on-write traffic. Backtracking now uses the
tracking logs to unwind the registers to the specific previous state.

This change also includes a minor refactor, moving the `storedCaptures`
array to the `Registers` struct, alongside the other mutable registers.
Future work includes breaking out the mutable registers into their own
type, creating a flat structure for these instead of maintaining four
different dynamically allocated arrays, and moving further toward
making `SavePoint` trivial.
@natecook1000

Copy link
Copy Markdown
Member Author

@swift-ci Please test Linux platform

This combines the current position and current range into a single
enum along with the semantic Boolean that only applies to range-
based save points. The log counts are also dropped to 32-bit integers,
shrinking the overall size of `SavePoint` from 76 bytes to 40.
Doing the isDirty check inside the UndoableArray was losing out
on the early exit for Registers.reset().
@natecook1000

Copy link
Copy Markdown
Member Author

@swift-ci Please test Linux platform

@natecook1000
natecook1000 merged commit d25aa38 into main Sep 22, 2026
24 checks passed
@natecook1000
natecook1000 deleted the savepoint-simplify branch September 22, 2026 14:28
natecook1000 added a commit that referenced this pull request Sep 23, 2026
Creating a save point previously copied the four mutable register arrays
(ints, positions, values, and storedCaptures) to enable restoration
during backtracking. This led to a large amount of reference counting
traffic and copy-on-write of the original register arrays whenever they
were updated.

This change moves the restoration data from using full copies of each
array in every save point, to tracking a log of the changes to each array
in the processor itself and only storing the current state of the logs
in each save point. This allows `SavePoint` to be a trivial type (almost)
and removes all the copy-on-write traffic. Backtracking now uses the
tracking logs to unwind the registers to the specific previous state.

This change also includes a minor refactor, moving the `storedCaptures`
array to the `Registers` struct, alongside the other mutable registers.
JacobHearst pushed a commit to JacobHearst/swift-experimental-string-processing that referenced this pull request Sep 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant