Skip to content

Commit 2b43ae4

Browse files
committed
feat: perf concurrent parzer
1 parent 5298bab commit 2b43ae4

5 files changed

Lines changed: 3817 additions & 86 deletions

File tree

lang/golang/parser/ctx.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ var errSysImport = fmt.Errorf("sys import")
3434

3535
// The go file's context. Used to pass information between ast node handlers
3636
type fileContext struct {
37+
parser *GoParser
3738
repoDir string
3839
filePath string
3940
module *Module
@@ -116,14 +117,21 @@ func (p *GoParser) referCodes(ctx *fileContext, id *Identity, depth int) (err er
116117
}
117118

118119
func (p *GoParser) getFileBytes(path string) []byte {
120+
p.filesMu.RLock()
119121
if bs, ok := p.files[path]; ok {
122+
p.filesMu.RUnlock()
120123
return bs
121124
}
125+
p.filesMu.RUnlock()
126+
122127
bs, err := os.ReadFile(path)
123128
if err != nil {
124129
panic(fmt.Sprintf("read file %s failed: %v", path, err))
125130
}
131+
132+
p.filesMu.Lock()
126133
p.files[path] = bs
134+
p.filesMu.Unlock()
127135
return bs
128136
}
129137

@@ -154,7 +162,10 @@ func (ctx *fileContext) GetMod(impt string) (string, error) {
154162
// fileContext 中的 import 信息只有**当前文件的引用路径**,但是存在一种场景就是实际调用的节点在另外的一个Package,导致漏解析
155163
// 常见于"链式调用"、"另一个 pkg 的全局变量的类型在另外一个 pkg 下"
156164
if ctx.module != nil && ctx.module.Packages != nil {
157-
if _, exist := ctx.module.Packages[impt]; exist {
165+
ctx.parser.repoMu.Lock()
166+
_, exist := ctx.module.Packages[impt]
167+
ctx.parser.repoMu.Unlock()
168+
if exist {
158169
return ctx.module.Name, nil
159170
}
160171
}

lang/golang/parser/file.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ func (p *GoParser) newVar(mod string, pkg string, name string, isConst bool) *Va
100100
IsConst: isConst,
101101
IsExported: isUpperCase(name[0]),
102102
}
103+
p.repoMu.Lock()
104+
defer p.repoMu.Unlock()
103105
return p.repo.SetVar(ret.Identity, ret)
104106
}
105107

@@ -276,12 +278,16 @@ func (p *GoParser) newFunc(mod, pkg, name string) *Function {
276278
}
277279

278280
ret := &Function{Identity: NewIdentity(mod, pkg, name), Exported: exported}
281+
p.repoMu.Lock()
282+
defer p.repoMu.Unlock()
279283
return p.repo.SetFunction(ret.Identity, ret)
280284
}
281285

282286
// newType allocate a struct in the repo
283287
func (p *GoParser) newType(mod, pkg, name string) *Type {
284288
ret := &Type{Identity: NewIdentity(mod, pkg, name), Exported: isUpperCase(name[0])}
289+
p.repoMu.Lock()
290+
defer p.repoMu.Unlock()
285291
return p.repo.SetType(ret.Identity, ret)
286292
}
287293

@@ -619,7 +625,9 @@ func (p *GoParser) parseType(ctx *fileContext, typDecl *ast.TypeSpec, doc *ast.C
619625
// check if it implements any parser.interfaces
620626
if obj, ok := ctx.pkgTypeInfo.Defs[typDecl.Name]; ok {
621627
if t := obj.Type(); t != nil {
628+
p.typesMu.Lock()
622629
p.types[t] = st.Identity
630+
p.typesMu.Unlock()
623631
}
624632
}
625633
}
@@ -661,7 +669,9 @@ func (p *GoParser) parseStruct(ctx *fileContext, struName string, name *ast.Iden
661669
st.SubStruct = InsertDependency(st.InlineStruct, dep)
662670
}
663671
// remove the anonymous struct from the repo
672+
p.repoMu.Lock()
664673
delete(p.repo.GetPackage(as.ModPath, as.PkgPath).Types, as.Name)
674+
p.repoMu.Unlock()
665675
} else {
666676
p.collectTypes(ctx, fieldDecl.Type, st, inlined)
667677
}
@@ -671,7 +681,9 @@ func (p *GoParser) parseStruct(ctx *fileContext, struName string, name *ast.Iden
671681
// check if it implements any parser.interfaces
672682
if obj, ok := ctx.pkgTypeInfo.Defs[name]; ok {
673683
if t := obj.Type(); t != nil {
684+
p.typesMu.Lock()
674685
p.types[t] = st.Identity
686+
p.typesMu.Unlock()
675687
}
676688
}
677689
}
@@ -734,7 +746,9 @@ func (p *GoParser) parseInterface(ctx *fileContext, name *ast.Ident, decl *ast.I
734746
iface := named.Underlying().(*types.Interface)
735747
// exclude empty interface
736748
if !iface.Empty() {
749+
p.typesMu.Lock()
737750
p.interfaces[iface] = st.Identity
751+
p.typesMu.Unlock()
738752
}
739753
}
740754
}

0 commit comments

Comments
 (0)