Debian Codegraph @

gopls/internal/cache/typerefs/packageset.go

raw symbols SCIP
1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package typerefs
6
7 import (
8 "fmt"
9 "math/bits"
10 "strings"
11 "sync"
12
13 "golang.org/x/tools/gopls/internal/cache/metadata"
14 "golang.org/x/tools/gopls/internal/util/moremaps"
15 )
16
17 // PackageIndex stores common data to enable efficient representation of
18 // references and package sets.
19 type PackageIndex struct {
20 // For now, PackageIndex just indexes package ids, to save space and allow for
21 // faster unions via sparse int vectors.
22 mu sync.Mutex
23 ids []metadata.PackageID
24 m map[metadata.PackageID]IndexID
25 }
26
27 // NewPackageIndex creates a new PackageIndex instance for use in building
28 // reference and package sets.
29 func NewPackageIndex() *PackageIndex {
30 return &PackageIndex{
31 m: make(map[metadata.PackageID]IndexID),
32 }
33 }
34
35 // IndexID returns the packageIdx referencing id, creating one if id is not yet
36 // tracked by the receiver.
37 func (index *PackageIndex) IndexID(id metadata.PackageID) IndexID {
38 index.mu.Lock()
39 defer index.mu.Unlock()
40 if i, ok := index.m[id]; ok {
41 return i
42 }
43 i := IndexID(len(index.ids))
44 index.m[id] = i
45 index.ids = append(index.ids, id)
46 return i
47 }
48
49 // PackageID returns the PackageID for idx.
50 //
51 // idx must have been created by this PackageIndex instance.
52 func (index *PackageIndex) PackageID(idx IndexID) metadata.PackageID {
53 index.mu.Lock()
54 defer index.mu.Unlock()
55 return index.ids[idx]
56 }
57
58 // A PackageSet is a set of metadata.PackageIDs, optimized for inuse memory
59 // footprint and efficient union operations.
60 type PackageSet struct {
61 // PackageSet is a sparse int vector of package indexes from parent.
62 parent *PackageIndex
63 sparse map[int]blockType // high bits in key, set of low bits in value
64 }
65
66 type blockType = uint // type of each sparse vector element
67 const blockSize = bits.UintSize
68
69 // NewSet creates a new PackageSet bound to this PackageIndex instance.
70 //
71 // PackageSets may only be combined with other PackageSets from the same
72 // instance.
73 func (index *PackageIndex) NewSet() *PackageSet {
74 return &PackageSet{
75 parent: index,
76 sparse: make(map[int]blockType),
77 }
78 }
79
80 // DeclaringPackage returns the ID of the symbol's declaring package.
81 // The package index must be the one used during decoding.
82 func (index *PackageIndex) DeclaringPackage(sym Symbol) metadata.PackageID {
83 return index.PackageID(sym.Package)
84 }
85
86 // Add records a new element in the package set, for the provided package ID.
87 func (s *PackageSet) AddPackage(id metadata.PackageID) {
88 s.Add(s.parent.IndexID(id))
89 }
90
91 // Add records a new element in the package set.
92 // It is the caller's responsibility to ensure that idx was created with the
93 // same PackageIndex as the PackageSet.
94 func (s *PackageSet) Add(idx IndexID) {
95 i := int(idx)
96 s.sparse[i/blockSize] |= 1 << (i % blockSize)
97 }
98
99 // Union records all elements from other into the receiver, mutating the
100 // receiver set but not the argument set. The receiver must not be nil, but the
101 // argument set may be nil.
102 //
103 // Precondition: both package sets were created with the same PackageIndex.
104 func (s *PackageSet) Union(other *PackageSet) {
105 if other == nil {
106 return // e.g. unsafe
107 }
108 if other.parent != s.parent {
109 panic("other set is from a different PackageIndex instance")
110 }
111 for k, v := range other.sparse {
112 if v0 := s.sparse[k]; v0 != v {
113 s.sparse[k] = v0 | v
114 }
115 }
116 }
117
118 // Contains reports whether id is contained in the receiver set.
119 func (s *PackageSet) Contains(id metadata.PackageID) bool {
120 i := int(s.parent.IndexID(id))
121 return s.sparse[i/blockSize]&(1<<(i%blockSize)) != 0
122 }
123
124 // Elems calls f for each element of the set in ascending order.
125 func (s *PackageSet) Elems(f func(IndexID)) {
126 for i, v := range moremaps.Sorted(s.sparse) {
127 for b := range blockSize {
128 if (v & (1 << b)) != 0 {
129 f(IndexID(i*blockSize + b))
130 }
131 }
132 }
133 }
134
135 // String returns a human-readable representation of the set: {A, B, ...}.
136 func (s *PackageSet) String() string {
137 var ids []string
138 s.Elems(func(id IndexID) {
139 ids = append(ids, string(s.parent.PackageID(id)))
140 })
141 return fmt.Sprintf("{%s}", strings.Join(ids, ", "))
142 }