Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go/token: use fine-grained locking in token.File #18348

Closed
alandonovan opened this issue Dec 16, 2016 · 2 comments
Closed

go/token: use fine-grained locking in token.File #18348

alandonovan opened this issue Dec 16, 2016 · 2 comments

Comments

@alandonovan
Copy link
Contributor

alandonovan commented Dec 16, 2016

Because access to the lines and info fields of token.File are protected by the mutex of the parent FileSet, all concurrent calls to AddLine in a typical application contend for the same lock, which becomes a major bottleneck when parsing files in parallel, as done by golang.org/x/tools/go/loader for example.

Fine-grained locking, that is, an additional mutex in each token.File that protects these two fields, eliminates the bottleneck. I quickly prototyped and measured it on a realistic application, and execution time fell to 580ms from 700m, an improvement of nearly 20%.

The necessary change is straightforward except for two places:
(1) In its loop, (*FileSet).Write needs to make a copy of each File's lines and infos while holding the file's mutex.
(2) (*File).PositionFor seems to have a pre-existing data race, since it calls File.position without a lock, which calls unpack, which reads the lines and info fields.

@alandonovan alandonovan added this to the Go1.9 milestone Dec 16, 2016
@alandonovan
Copy link
Contributor Author

/cc @griesemer

@gopherbot
Copy link

CL https://golang.org/cl/34591 mentions this issue.

@golang golang locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants