Skip to content

Commit

Permalink
avoid copying an empty map if no captures
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Feb 6, 2025
1 parent 8d67f28 commit be1c5ad
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions x/meg/meg.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ type captureFunc *func(string) error
type captureMap map[captureFunc][]string

func (cm captureMap) clone() captureMap {
if cm == nil {
return nil
}
out := make(captureMap, len(cm))
for k, v := range cm {
out[k] = slices.Clone(v)
Expand Down Expand Up @@ -80,6 +83,10 @@ func Match[S ~[]T, T Matchable](s *MatchState, components S) (bool, error) {
if s.kind == matchCode && s.code == c.Code() {
cm := currentStates.captures[i]
if s.capture != nil {
if cm == nil {
cm = make(captureMap)
currentStates.captures[i] = cm
}
cm[s.capture] = append(cm[s.capture], c.Value())
}
nextStates = appendState(nextStates, s.next, currentStates.captures[i], listGeneration)
Expand Down Expand Up @@ -111,9 +118,6 @@ func appendState(arr statesAndCaptures, s *MatchState, c captureMap, listGenerat
if s == nil || s.generation == listGeneration {
return arr
}
if c == nil {
c = make(captureMap)
}
s.generation = listGeneration
if s.kind == split {
arr = appendState(arr, s.next, c, listGeneration)
Expand Down

0 comments on commit be1c5ad

Please sign in to comment.