Skip to content

Commit

Permalink
LSIF: conversion: simplify documentation grouping mutex
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Gutekanst <[email protected]>
  • Loading branch information
Stephen Gutekanst authored and Stephen Gutekanst committed Jun 28, 2021
1 parent 8c09b15 commit 80bed31
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (s *Store) scanFirstDocumentationPageData(rows *sql.Rows, queryErr error) (

// DocumentationPathInfo returns info describing what is at the given pathID.
func (s *Store) DocumentationPathInfo(ctx context.Context, bundleID int, pathID string) (_ *semantic.DocumentationPathInfoData, err error) {
ctx, _, endObservation := s.operations.documentationPage.WithAndLogger(ctx, &err, observation.Args{LogFields: []log.Field{
ctx, _, endObservation := s.operations.documentationPathInfo.WithAndLogger(ctx, &err, observation.Args{LogFields: []log.Field{
log.Int("bundleID", bundleID),
log.String("pathID", pathID),
}})
Expand Down
31 changes: 14 additions & 17 deletions lib/codeintel/lsif/conversion/group_documentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ type pageCollector struct {
}

func (p *pageCollector) collect(ctx context.Context, ch chan<- *semantic.DocumentationPageData) (remainingPages []*pageCollector) {
var remainingPagesMu sync.RWMutex
var walk func(parent *semantic.DocumentationNode, documentationResult int, pathID string)
walk = func(parent *semantic.DocumentationNode, documentationResult int, pathID string) {
labelID := p.state.DocumentationStringLabel[documentationResult]
Expand Down Expand Up @@ -137,7 +136,6 @@ func (p *pageCollector) collect(ctx context.Context, ch chan<- *semantic.Documen
PathID: this.PathID,
})
if p.walkedPages.add(this.PathID) {
remainingPagesMu.Lock()
remainingPages = append(remainingPages, &pageCollector{
isChildPage: true,
parentPathID: parent.PathID,
Expand All @@ -146,7 +144,6 @@ func (p *pageCollector) collect(ctx context.Context, ch chan<- *semantic.Documen
dupChecker: p.dupChecker,
walkedPages: p.walkedPages,
})
remainingPagesMu.Unlock()
}
return
} else {
Expand All @@ -172,38 +169,38 @@ func (p *pageCollector) collect(ctx context.Context, ch chan<- *semantic.Documen
}
walk(nil, p.startingDocumentationResult, p.parentPathID)
if p.isChildPage {
remainingPagesMu.RLock()
defer remainingPagesMu.RUnlock()
return remainingPages
}

// We are the root project page! Collect all the remaining pages.
var (
remainingWorkMu sync.RWMutex
remainingWork = remainingPages
)
wg := &sync.WaitGroup{}
remainingPagesMu.RLock()
wg.Add(len(remainingPages))
remainingPagesMu.RUnlock()
wg.Add(len(remainingWork))
for i := 0; i <= p.numWorkers; i++ {
go func() {
for {
// Get a remaining page to process.
remainingPagesMu.Lock()
if len(remainingPages) == 0 { // HERE
remainingPagesMu.Unlock()
remainingWorkMu.Lock()
if len(remainingWork) == 0 {
remainingWorkMu.Unlock()
return // no more work
}
work := remainingPages[0]
remainingPages = remainingPages[1:]
remainingPagesMu.Unlock()
work := remainingWork[0]
remainingWork = remainingWork[1:]
remainingWorkMu.Unlock()

// Perform work.
newRemainingPages := work.collect(ctx, ch)

// Add new work, if needed.
if len(newRemainingPages) > 0 {
wg.Add(len(newRemainingPages))
remainingPagesMu.Lock()
remainingPages = append(remainingPages, newRemainingPages...)
remainingPagesMu.Unlock()
remainingWorkMu.Lock()
remainingWork = append(remainingWork, newRemainingPages...)
remainingWorkMu.Unlock()
}
wg.Done()
}
Expand Down

0 comments on commit 80bed31

Please sign in to comment.