Skip to content

reduce AC automaton memory by ~250MB#14

Merged
danslo merged 1 commit intomainfrom
memory
Mar 3, 2026
Merged

reduce AC automaton memory by ~250MB#14
danslo merged 1 commit intomainfrom
memory

Conversation

@danslo
Copy link
Contributor

@danslo danslo commented Mar 3, 2026

Shrink the state struct from 80 to 32 bytes via two changes:

  • Move matches []pattern out of state into a map[stateID][]pattern on iNFA. Only match states (rare) need this; the hot path (hasMatch) uses the matchBitset and is unchanged. Saves 24 bytes/state (~85MB).

  • Move dense transitions out of state into a flat []stateID table on iNFA, indexed by a per-state int32 offset (-1 for sparse). Eliminates the transitions struct entirely. Only ~66K dense states (depth < 3) use this; the other ~3.7M sparse states no longer carry a nil 24-byte slice header. Saves another 24 bytes/state (~85MB). The contiguous table also improves cache locality (parse-bench ratio 1.27x -> 1.23x).

Peak RSS on a real scan: ~710MB -> ~460MB.

Shrink the state struct from 80 to 32 bytes via two changes:

- Move matches []pattern out of state into a map[stateID][]pattern on
  iNFA. Only match states (rare) need this; the hot path (hasMatch) uses
  the matchBitset and is unchanged. Saves 24 bytes/state (~85MB).

- Move dense transitions out of state into a flat []stateID table on
  iNFA, indexed by a per-state int32 offset (-1 for sparse). Eliminates
  the transitions struct entirely. Only ~66K dense states (depth < 3)
  use this; the other ~3.7M sparse states no longer carry a nil 24-byte
  slice header. Saves another 24 bytes/state (~85MB). The contiguous
  table also improves cache locality (parse-bench ratio 1.27x -> 1.23x).

Peak RSS on a real scan: ~710MB -> ~460MB.
@danslo danslo merged commit 83bef40 into main Mar 3, 2026
3 checks passed
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