-
Notifications
You must be signed in to change notification settings - Fork 869
Make SourceTextData thread-safe with ConcurrentDictionary
#20113
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -459,30 +459,28 @@ module internal Tokenizer = | |
| && let lineContents = textLine.Text.ToString(textLine.Span) in | ||
| data.HashCode = lineContents.GetHashCode() | ||
|
|
||
| // Shared by concurrent editor operations (classification and symbol lookup), so must be thread-safe. | ||
| type private SourceTextData(approxLines: int) = | ||
| let data = ResizeArray<SourceLineData option>(approxLines) | ||
|
|
||
| let extendTo i = | ||
| if i >= data.Count then | ||
| data.Capacity <- i + 1 | ||
|
|
||
| for j in data.Count .. i do | ||
| data.Add(None) | ||
| let data = | ||
| ConcurrentDictionary<int, SourceLineData>(Environment.ProcessorCount, approxLines) | ||
|
|
||
| member x.Item | ||
| with get (i: int) = | ||
| extendTo i | ||
| data.[i] | ||
| match data.TryGetValue(i) with | ||
| | true, v -> Some v | ||
| | _ -> None | ||
| and set (i: int) v = | ||
| extendTo i | ||
| data.[i] <- v | ||
| match v with | ||
| | Some v -> data.[i] <- v | ||
| | None -> data.TryRemove(i) |> ignore | ||
|
|
||
| member x.ClearFrom(n) = | ||
| let mutable i = n | ||
| let mutable cont = true | ||
|
|
||
| while i < data.Count && data.[i].IsSome do | ||
| data.[i] <- None | ||
| i <- i + 1 | ||
| while cont do | ||
| let removed, _ = data.TryRemove(i) | ||
| if removed then i <- i + 1 else cont <- false | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-blocking observation: |
||
|
|
||
| /// This saves the tokenization data for a file for as long as the DocumentId object is alive. | ||
| /// This seems risky - if one single thing leaks a DocumentId (e.g. stores it in some global table of documents | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This getter allocates a new
Somefor every cache hit. One million reads allocated 24 MB, while the old getter andvoptionallocated none. Returnvoptionor expose an allocation-free lookup.