Skip to content

Commit 13efffc

Browse files
TIHanbaronfel
authored andcommitted
Using ArrayPool for AssocTable (#8234)
* Using ArrayPool * Remove open * Changed some style nits * Clear arrays
1 parent 387d20a commit 13efffc

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

eng/Versions.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
<SystemThreadingThreadVersion>4.3.0</SystemThreadingThreadVersion>
9696
<SystemThreadingThreadPoolVersion>4.3.0</SystemThreadingThreadPoolVersion>
9797
<SystemValueTupleVersion>4.5.0</SystemValueTupleVersion>
98+
<SystemBuffersVersion>4.5.0</SystemBuffersVersion>
9899
<!-- Roslyn packages -->
99100
<MicrosoftCodeAnalysisEditorFeaturesVersion>$(RoslynVersion)</MicrosoftCodeAnalysisEditorFeaturesVersion>
100101
<MicrosoftCodeAnalysisEditorFeaturesTextVersion>$(RoslynVersion)</MicrosoftCodeAnalysisEditorFeaturesTextVersion>

fcs/FSharp.Compiler.Service/FSharp.Compiler.Service.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,7 @@
683683
<PackageReference Include="FSharp.Core" Version="$(FcsFSharpCorePkgVersion)" />
684684
<PackageReference Include="System.Collections.Immutable" Version="1.5.0" />
685685
<PackageReference Include="System.Reflection.Metadata" Version="1.6.0" />
686+
<PackageReference Include="System.Buffers" Version="4.5.0" />
686687
</ItemGroup>
687688
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
688689
<PackageReference Include="System.Diagnostics.Process" Version="4.1.0" />

src/utils/prim-parsing.fs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace Internal.Utilities.Text.Parsing
77
open Internal.Utilities.Text.Lexing
88

99
open System
10+
open System.Buffers
1011

1112
exception RecoverableParseError
1213
exception Accept of obj
@@ -131,11 +132,7 @@ module internal Implementation =
131132
//-------------------------------------------------------------------------
132133
// Read the tables written by FSYACC.
133134

134-
type AssocTable(elemTab:uint16[], offsetTab:uint16[]) =
135-
let cacheSize = 7919 // the 1000'th prime
136-
// Use a simpler hash table with faster lookup, but only one
137-
// hash bucket per key.
138-
let cache = Array.zeroCreate<int> (cacheSize * 2)
135+
type AssocTable(elemTab: uint16[], offsetTab: uint16[], cache: int[], cacheSize: int) =
139136

140137
member t.ReadAssoc (minElemNum,maxElemNum,defaultValueOfAssoc,keyToFind) =
141138
// do a binary chop on the table
@@ -234,8 +231,21 @@ module internal Implementation =
234231
let ruleValues = (Array.zeroCreate 100 : obj[])
235232
let lhsPos = (Array.zeroCreate 2 : Position[])
236233
let reductions = tables.reductions
237-
let actionTable = new AssocTable(tables.actionTableElements, tables.actionTableRowOffsets)
238-
let gotoTable = new AssocTable(tables.gotos, tables.sparseGotoTableRowOffsets)
234+
let cacheSize = 7919 // the 1000'th prime
235+
// Use a simpler hash table with faster lookup, but only one
236+
// hash bucket per key.
237+
let actionTableCache = ArrayPool<int>.Shared.Rent(cacheSize * 2)
238+
let gotoTableCache = ArrayPool<int>.Shared.Rent(cacheSize * 2)
239+
// Clear the arrays since ArrayPool does not
240+
Array.Clear(actionTableCache, 0, actionTableCache.Length)
241+
Array.Clear(gotoTableCache, 0, gotoTableCache.Length)
242+
use _cacheDisposal =
243+
{ new IDisposable with
244+
member _.Dispose() =
245+
ArrayPool<int>.Shared.Return actionTableCache
246+
ArrayPool<int>.Shared.Return gotoTableCache }
247+
let actionTable = AssocTable(tables.actionTableElements, tables.actionTableRowOffsets, actionTableCache, cacheSize)
248+
let gotoTable = AssocTable(tables.gotos, tables.sparseGotoTableRowOffsets, gotoTableCache, cacheSize)
239249
let stateToProdIdxsTable = new IdxToIdxListTable(tables.stateToProdIdxsTableElements, tables.stateToProdIdxsTableRowOffsets)
240250

241251
let parseState =

0 commit comments

Comments
 (0)