-
Notifications
You must be signed in to change notification settings - Fork 16
/
jcollect.go
684 lines (631 loc) · 17 KB
/
jcollect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
package antlr
// Copyright (c) 2012-2022 The ANTLR Project. All rights reserved.
// Use of this file is governed by the BSD 3-clause license that
// can be found in the LICENSE.txt file in the project root.
import (
"container/list"
"runtime/debug"
"sort"
)
// Collectable is an interface that a struct should implement if it is to be
// usable as a key in these collections.
type Collectable[T any] interface {
Hash() int
Equals(other Collectable[T]) bool
}
type Comparator[T any] interface {
Hash1(o T) int
Equals2(T, T) bool
}
type CollectionSource int
type CollectionDescriptor struct {
SybolicName string
Description string
}
const (
UnknownCollection CollectionSource = iota
ATNConfigLookupCollection
ATNStateCollection
DFAStateCollection
ATNConfigCollection
PredictionContextCollection
SemanticContextCollection
ClosureBusyCollection
PredictionVisitedCollection
MergeCacheCollection
PredictionContextCacheCollection
AltSetCollection
ReachSetCollection
)
var CollectionDescriptors = map[CollectionSource]CollectionDescriptor{
UnknownCollection: {
SybolicName: "UnknownCollection",
Description: "Unknown collection type. Only used if the target author thought it was an unimportant collection.",
},
ATNConfigCollection: {
SybolicName: "ATNConfigCollection",
Description: "ATNConfig collection. Used to store the ATNConfigs for a particular state in the ATN." +
"For instance, it is used to store the results of the closure() operation in the ATN.",
},
ATNConfigLookupCollection: {
SybolicName: "ATNConfigLookupCollection",
Description: "ATNConfigLookup collection. Used to store the ATNConfigs for a particular state in the ATN." +
"This is used to prevent duplicating equivalent states in an ATNConfigurationSet.",
},
ATNStateCollection: {
SybolicName: "ATNStateCollection",
Description: "ATNState collection. This is used to store the states of the ATN.",
},
DFAStateCollection: {
SybolicName: "DFAStateCollection",
Description: "DFAState collection. This is used to store the states of the DFA.",
},
PredictionContextCollection: {
SybolicName: "PredictionContextCollection",
Description: "PredictionContext collection. This is used to store the prediction contexts of the ATN and cache computes.",
},
SemanticContextCollection: {
SybolicName: "SemanticContextCollection",
Description: "SemanticContext collection. This is used to store the semantic contexts of the ATN.",
},
ClosureBusyCollection: {
SybolicName: "ClosureBusyCollection",
Description: "ClosureBusy collection. This is used to check and prevent infinite recursion right recursive rules." +
"It stores ATNConfigs that are currently being processed in the closure() operation.",
},
PredictionVisitedCollection: {
SybolicName: "PredictionVisitedCollection",
Description: "A map that records whether we have visited a particular context when searching through cached entries.",
},
MergeCacheCollection: {
SybolicName: "MergeCacheCollection",
Description: "A map that records whether we have already merged two particular contexts and can save effort by not repeating it.",
},
PredictionContextCacheCollection: {
SybolicName: "PredictionContextCacheCollection",
Description: "A map that records whether we have already created a particular context and can save effort by not computing it again.",
},
AltSetCollection: {
SybolicName: "AltSetCollection",
Description: "Used to eliminate duplicate alternatives in an ATN config set.",
},
ReachSetCollection: {
SybolicName: "ReachSetCollection",
Description: "Used as merge cache to prevent us needing to compute the merge of two states if we have already done it.",
},
}
// JStore implements a container that allows the use of a struct to calculate the key
// for a collection of values akin to map. This is not meant to be a full-blown HashMap but just
// serve the needs of the ANTLR Go runtime.
//
// For ease of porting the logic of the runtime from the master target (Java), this collection
// operates in a similar way to Java, in that it can use any struct that supplies a Hash() and Equals()
// function as the key. The values are stored in a standard go map which internally is a form of hashmap
// itself, the key for the go map is the hash supplied by the key object. The collection is able to deal with
// hash conflicts by using a simple slice of values associated with the hash code indexed bucket. That isn't
// particularly efficient, but it is simple, and it works. As this is specifically for the ANTLR runtime, and
// we understand the requirements, then this is fine - this is not a general purpose collection.
type JStore[T any, C Comparator[T]] struct {
store map[int][]T
len int
comparator Comparator[T]
stats *JStatRec
}
func NewJStore[T any, C Comparator[T]](comparator Comparator[T], cType CollectionSource, desc string) *JStore[T, C] {
if comparator == nil {
panic("comparator cannot be nil")
}
s := &JStore[T, C]{
store: make(map[int][]T, 1),
comparator: comparator,
}
if collectStats {
s.stats = &JStatRec{
Source: cType,
Description: desc,
}
// Track where we created it from if we are being asked to do so
if runtimeConfig.statsTraceStacks {
s.stats.CreateStack = debug.Stack()
}
Statistics.AddJStatRec(s.stats)
}
return s
}
// Put will store given value in the collection. Note that the key for storage is generated from
// the value itself - this is specifically because that is what ANTLR needs - this would not be useful
// as any kind of general collection.
//
// If the key has a hash conflict, then the value will be added to the slice of values associated with the
// hash, unless the value is already in the slice, in which case the existing value is returned. Value equivalence is
// tested by calling the equals() method on the key.
//
// # If the given value is already present in the store, then the existing value is returned as v and exists is set to true
//
// If the given value is not present in the store, then the value is added to the store and returned as v and exists is set to false.
func (s *JStore[T, C]) Put(value T) (v T, exists bool) {
if collectStats {
s.stats.Puts++
}
kh := s.comparator.Hash1(value)
var hClash bool
for _, v1 := range s.store[kh] {
hClash = true
if s.comparator.Equals2(value, v1) {
if collectStats {
s.stats.PutHits++
s.stats.PutHashConflicts++
}
return v1, true
}
if collectStats {
s.stats.PutMisses++
}
}
if collectStats && hClash {
s.stats.PutHashConflicts++
}
s.store[kh] = append(s.store[kh], value)
if collectStats {
if len(s.store[kh]) > s.stats.MaxSlotSize {
s.stats.MaxSlotSize = len(s.store[kh])
}
}
s.len++
if collectStats {
s.stats.CurSize = s.len
if s.len > s.stats.MaxSize {
s.stats.MaxSize = s.len
}
}
return value, false
}
// Get will return the value associated with the key - the type of the key is the same type as the value
// which would not generally be useful, but this is a specific thing for ANTLR where the key is
// generated using the object we are going to store.
func (s *JStore[T, C]) Get(key T) (T, bool) {
if collectStats {
s.stats.Gets++
}
kh := s.comparator.Hash1(key)
var hClash bool
for _, v := range s.store[kh] {
hClash = true
if s.comparator.Equals2(key, v) {
if collectStats {
s.stats.GetHits++
s.stats.GetHashConflicts++
}
return v, true
}
if collectStats {
s.stats.GetMisses++
}
}
if collectStats {
if hClash {
s.stats.GetHashConflicts++
}
s.stats.GetNoEnt++
}
return key, false
}
// Contains returns true if the given key is present in the store
func (s *JStore[T, C]) Contains(key T) bool {
_, present := s.Get(key)
return present
}
func (s *JStore[T, C]) SortedSlice(less func(i, j T) bool) []T {
vs := make([]T, 0, len(s.store))
for _, v := range s.store {
vs = append(vs, v...)
}
sort.Slice(vs, func(i, j int) bool {
return less(vs[i], vs[j])
})
return vs
}
func (s *JStore[T, C]) Each(f func(T) bool) {
for _, e := range s.store {
for _, v := range e {
f(v)
}
}
}
func (s *JStore[T, C]) Len() int {
return s.len
}
func (s *JStore[T, C]) Values() []T {
vs := make([]T, 0, len(s.store))
for _, e := range s.store {
vs = append(vs, e...)
}
return vs
}
type entry[K, V any] struct {
key K
val V
}
type JMap[K, V any, C Comparator[K]] struct {
store map[int][]*entry[K, V]
len int
comparator Comparator[K]
stats *JStatRec
}
func NewJMap[K, V any, C Comparator[K]](comparator Comparator[K], cType CollectionSource, desc string) *JMap[K, V, C] {
m := &JMap[K, V, C]{
store: make(map[int][]*entry[K, V], 1),
comparator: comparator,
}
if collectStats {
m.stats = &JStatRec{
Source: cType,
Description: desc,
}
// Track where we created it from if we are being asked to do so
if runtimeConfig.statsTraceStacks {
m.stats.CreateStack = debug.Stack()
}
Statistics.AddJStatRec(m.stats)
}
return m
}
func (m *JMap[K, V, C]) Put(key K, val V) (V, bool) {
if collectStats {
m.stats.Puts++
}
kh := m.comparator.Hash1(key)
var hClash bool
for _, e := range m.store[kh] {
hClash = true
if m.comparator.Equals2(e.key, key) {
if collectStats {
m.stats.PutHits++
m.stats.PutHashConflicts++
}
return e.val, true
}
if collectStats {
m.stats.PutMisses++
}
}
if collectStats {
if hClash {
m.stats.PutHashConflicts++
}
}
m.store[kh] = append(m.store[kh], &entry[K, V]{key, val})
if collectStats {
if len(m.store[kh]) > m.stats.MaxSlotSize {
m.stats.MaxSlotSize = len(m.store[kh])
}
}
m.len++
if collectStats {
m.stats.CurSize = m.len
if m.len > m.stats.MaxSize {
m.stats.MaxSize = m.len
}
}
return val, false
}
func (m *JMap[K, V, C]) Values() []V {
vs := make([]V, 0, len(m.store))
for _, e := range m.store {
for _, v := range e {
vs = append(vs, v.val)
}
}
return vs
}
func (m *JMap[K, V, C]) Get(key K) (V, bool) {
if collectStats {
m.stats.Gets++
}
var none V
kh := m.comparator.Hash1(key)
var hClash bool
for _, e := range m.store[kh] {
hClash = true
if m.comparator.Equals2(e.key, key) {
if collectStats {
m.stats.GetHits++
m.stats.GetHashConflicts++
}
return e.val, true
}
if collectStats {
m.stats.GetMisses++
}
}
if collectStats {
if hClash {
m.stats.GetHashConflicts++
}
m.stats.GetNoEnt++
}
return none, false
}
func (m *JMap[K, V, C]) Len() int {
return m.len
}
func (m *JMap[K, V, C]) Delete(key K) {
kh := m.comparator.Hash1(key)
for i, e := range m.store[kh] {
if m.comparator.Equals2(e.key, key) {
m.store[kh] = append(m.store[kh][:i], m.store[kh][i+1:]...)
m.len--
return
}
}
}
func (m *JMap[K, V, C]) Clear() {
m.store = make(map[int][]*entry[K, V])
}
type JPCMap struct {
store *JMap[*PredictionContext, *JMap[*PredictionContext, *PredictionContext, *ObjEqComparator[*PredictionContext]], *ObjEqComparator[*PredictionContext]]
size int
stats *JStatRec
}
func NewJPCMap(cType CollectionSource, desc string) *JPCMap {
m := &JPCMap{
store: NewJMap[*PredictionContext, *JMap[*PredictionContext, *PredictionContext, *ObjEqComparator[*PredictionContext]], *ObjEqComparator[*PredictionContext]](pContextEqInst, cType, desc),
}
if collectStats {
m.stats = &JStatRec{
Source: cType,
Description: desc,
}
// Track where we created it from if we are being asked to do so
if runtimeConfig.statsTraceStacks {
m.stats.CreateStack = debug.Stack()
}
Statistics.AddJStatRec(m.stats)
}
return m
}
func (pcm *JPCMap) Get(k1, k2 *PredictionContext) (*PredictionContext, bool) {
if collectStats {
pcm.stats.Gets++
}
// Do we have a map stored by k1?
//
m2, present := pcm.store.Get(k1)
if present {
if collectStats {
pcm.stats.GetHits++
}
// We found a map of values corresponding to k1, so now we need to look up k2 in that map
//
return m2.Get(k2)
}
if collectStats {
pcm.stats.GetMisses++
}
return nil, false
}
func (pcm *JPCMap) Put(k1, k2, v *PredictionContext) {
if collectStats {
pcm.stats.Puts++
}
// First does a map already exist for k1?
//
if m2, present := pcm.store.Get(k1); present {
if collectStats {
pcm.stats.PutHits++
}
_, present = m2.Put(k2, v)
if !present {
pcm.size++
if collectStats {
pcm.stats.CurSize = pcm.size
if pcm.size > pcm.stats.MaxSize {
pcm.stats.MaxSize = pcm.size
}
}
}
} else {
// No map found for k1, so we create it, add in our value, then store is
//
if collectStats {
pcm.stats.PutMisses++
m2 = NewJMap[*PredictionContext, *PredictionContext, *ObjEqComparator[*PredictionContext]](pContextEqInst, pcm.stats.Source, pcm.stats.Description+" map entry")
} else {
m2 = NewJMap[*PredictionContext, *PredictionContext, *ObjEqComparator[*PredictionContext]](pContextEqInst, PredictionContextCacheCollection, "map entry")
}
m2.Put(k2, v)
pcm.store.Put(k1, m2)
pcm.size++
}
}
type JPCMap2 struct {
store map[int][]JPCEntry
size int
stats *JStatRec
}
type JPCEntry struct {
k1, k2, v *PredictionContext
}
func NewJPCMap2(cType CollectionSource, desc string) *JPCMap2 {
m := &JPCMap2{
store: make(map[int][]JPCEntry, 1000),
}
if collectStats {
m.stats = &JStatRec{
Source: cType,
Description: desc,
}
// Track where we created it from if we are being asked to do so
if runtimeConfig.statsTraceStacks {
m.stats.CreateStack = debug.Stack()
}
Statistics.AddJStatRec(m.stats)
}
return m
}
func dHash(k1, k2 *PredictionContext) int {
return k1.cachedHash*31 + k2.cachedHash
}
func (pcm *JPCMap2) Get(k1, k2 *PredictionContext) (*PredictionContext, bool) {
if collectStats {
pcm.stats.Gets++
}
h := dHash(k1, k2)
var hClash bool
for _, e := range pcm.store[h] {
hClash = true
if e.k1.Equals(k1) && e.k2.Equals(k2) {
if collectStats {
pcm.stats.GetHits++
pcm.stats.GetHashConflicts++
}
return e.v, true
}
if collectStats {
pcm.stats.GetMisses++
}
}
if collectStats {
if hClash {
pcm.stats.GetHashConflicts++
}
pcm.stats.GetNoEnt++
}
return nil, false
}
func (pcm *JPCMap2) Put(k1, k2, v *PredictionContext) (*PredictionContext, bool) {
if collectStats {
pcm.stats.Puts++
}
h := dHash(k1, k2)
var hClash bool
for _, e := range pcm.store[h] {
hClash = true
if e.k1.Equals(k1) && e.k2.Equals(k2) {
if collectStats {
pcm.stats.PutHits++
pcm.stats.PutHashConflicts++
}
return e.v, true
}
if collectStats {
pcm.stats.PutMisses++
}
}
if collectStats {
if hClash {
pcm.stats.PutHashConflicts++
}
}
pcm.store[h] = append(pcm.store[h], JPCEntry{k1, k2, v})
pcm.size++
if collectStats {
pcm.stats.CurSize = pcm.size
if pcm.size > pcm.stats.MaxSize {
pcm.stats.MaxSize = pcm.size
}
}
return nil, false
}
type VisitEntry struct {
k *PredictionContext
v *PredictionContext
}
type VisitRecord struct {
store map[*PredictionContext]*PredictionContext
len int
stats *JStatRec
}
type VisitList struct {
cache *list.List
lock RWMutex
}
var visitListPool = VisitList{
cache: list.New(),
lock: RWMutex{},
}
// NewVisitRecord returns a new VisitRecord instance from the pool if available.
// Note that this "map" uses a pointer as a key because we are emulating the behavior of
// IdentityHashMap in Java, which uses the `==` operator to compare whether the keys are equal,
// which means is the key the same reference to an object rather than is it .equals() to another
// object.
func NewVisitRecord() *VisitRecord {
visitListPool.lock.Lock()
el := visitListPool.cache.Front()
defer visitListPool.lock.Unlock()
var vr *VisitRecord
if el == nil {
vr = &VisitRecord{
store: make(map[*PredictionContext]*PredictionContext),
}
if collectStats {
vr.stats = &JStatRec{
Source: PredictionContextCacheCollection,
Description: "VisitRecord",
}
// Track where we created it from if we are being asked to do so
if runtimeConfig.statsTraceStacks {
vr.stats.CreateStack = debug.Stack()
}
}
} else {
vr = el.Value.(*VisitRecord)
visitListPool.cache.Remove(el)
vr.store = make(map[*PredictionContext]*PredictionContext)
}
if collectStats {
Statistics.AddJStatRec(vr.stats)
}
return vr
}
func (vr *VisitRecord) Release() {
vr.len = 0
vr.store = nil
if collectStats {
vr.stats.MaxSize = 0
vr.stats.CurSize = 0
vr.stats.Gets = 0
vr.stats.GetHits = 0
vr.stats.GetMisses = 0
vr.stats.GetHashConflicts = 0
vr.stats.GetNoEnt = 0
vr.stats.Puts = 0
vr.stats.PutHits = 0
vr.stats.PutMisses = 0
vr.stats.PutHashConflicts = 0
vr.stats.MaxSlotSize = 0
}
visitListPool.lock.Lock()
visitListPool.cache.PushBack(vr)
visitListPool.lock.Unlock()
}
func (vr *VisitRecord) Get(k *PredictionContext) (*PredictionContext, bool) {
if collectStats {
vr.stats.Gets++
}
v := vr.store[k]
if v != nil {
if collectStats {
vr.stats.GetHits++
}
return v, true
}
if collectStats {
vr.stats.GetNoEnt++
}
return nil, false
}
func (vr *VisitRecord) Put(k, v *PredictionContext) (*PredictionContext, bool) {
if collectStats {
vr.stats.Puts++
}
vr.store[k] = v
vr.len++
if collectStats {
vr.stats.CurSize = vr.len
if vr.len > vr.stats.MaxSize {
vr.stats.MaxSize = vr.len
}
}
return v, false
}