diff options
author | Ryo Nihei <nihei.dev@gmail.com> | 2021-05-04 17:45:24 +0900 |
---|---|---|
committer | Ryo Nihei <nihei.dev@gmail.com> | 2021-05-04 17:45:24 +0900 |
commit | a0fd0d5a9077947d09777cf7631ca721ffabf9ec (patch) | |
tree | af060dd455d1895676f1ac5c030839560ec3eb6e /compiler/dfa.go | |
parent | Add lex mode (diff) | |
download | tre-a0fd0d5a9077947d09777cf7631ca721ffabf9ec.tar.gz tre-a0fd0d5a9077947d09777cf7631ca721ffabf9ec.tar.xz |
Improve performance of the symbolPositionSet
When using a map to represent a set, performance degrades due to
the increased number of calls of runtime.mapassign.
Especially when the number of symbols is large, as in compiling a pattern that
contains character properties like \p{Letter}, adding elements to the set
alone may take several tens of seconds of CPU time.
Therefore, this commit solves this problem by changing the representation of
the set from map to array.
Diffstat (limited to 'compiler/dfa.go')
-rw-r--r-- | compiler/dfa.go | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/compiler/dfa.go b/compiler/dfa.go index b07954f..049ff3e 100644 --- a/compiler/dfa.go +++ b/compiler/dfa.go @@ -16,18 +16,18 @@ type DFA struct { func genDFA(root astNode, symTab *symbolTable) *DFA { initialState := root.first() initialStateHash := initialState.hash() - stateMap := map[string]symbolPositionSet{} + stateMap := map[string]*symbolPositionSet{} tranTab := map[string][256]string{} { follow := genFollowTable(root) - unmarkedStates := map[string]symbolPositionSet{ + unmarkedStates := map[string]*symbolPositionSet{ initialStateHash: initialState, } for len(unmarkedStates) > 0 { - nextUnmarkedStates := map[string]symbolPositionSet{} + nextUnmarkedStates := map[string]*symbolPositionSet{} for hash, state := range unmarkedStates { - tranTabOfState := [256]symbolPositionSet{} - for _, pos := range state.sort() { + tranTabOfState := [256]*symbolPositionSet{} + for _, pos := range state.set() { if pos.isEndMark() { continue } @@ -66,7 +66,7 @@ func genDFA(root astNode, symTab *symbolTable) *DFA { accTab := map[string]int{} { for h, s := range stateMap { - for pos := range s { + for _, pos := range s.set() { if !pos.isEndMark() { continue } |