Skip to content

Commit

Permalink
Using ArrayPool for AssocTable (dotnet#8234)
Browse files Browse the repository at this point in the history
* Using ArrayPool

* Remove open

* Changed some style nits

* Clear arrays
  • Loading branch information
TIHan authored and nosami committed Feb 22, 2021
1 parent dcbfa58 commit 9233270
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions eng/Versions.props
Expand Up @@ -95,6 +95,7 @@
<SystemThreadingThreadVersion>4.3.0</SystemThreadingThreadVersion>
<SystemThreadingThreadPoolVersion>4.3.0</SystemThreadingThreadPoolVersion>
<SystemValueTupleVersion>4.5.0</SystemValueTupleVersion>
<SystemBuffersVersion>4.5.0</SystemBuffersVersion>
<!-- Roslyn packages -->
<MicrosoftCodeAnalysisEditorFeaturesVersion>$(RoslynVersion)</MicrosoftCodeAnalysisEditorFeaturesVersion>
<MicrosoftCodeAnalysisEditorFeaturesTextVersion>$(RoslynVersion)</MicrosoftCodeAnalysisEditorFeaturesTextVersion>
Expand Down
1 change: 1 addition & 0 deletions fcs/FSharp.Compiler.Service/FSharp.Compiler.Service.fsproj
Expand Up @@ -680,6 +680,7 @@
<PackageReference Include="FSharp.Core" Version="$(FcsFSharpCorePkgVersion)" />
<PackageReference Include="System.Collections.Immutable" Version="1.5.0" />
<PackageReference Include="System.Reflection.Metadata" Version="1.6.0" />
<PackageReference Include="System.Buffers" Version="4.5.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Diagnostics.Process" Version="4.1.0" />
Expand Down
Expand Up @@ -749,6 +749,7 @@
<PackageReference Include="System.Threading.Tasks.Parallel" Version="$(SystemThreadingTasksParallelVersion)" />
<PackageReference Include="System.Threading.Thread" Version="$(SystemThreadingThreadVersion)" />
<PackageReference Include="System.Threading.ThreadPool" Version="$(SystemThreadingThreadPoolVersion)" />
<PackageReference Include="System.Buffers" Version="$(SystemBuffersVersion)" />
</ItemGroup>

</Project>
24 changes: 17 additions & 7 deletions src/utils/prim-parsing.fs
Expand Up @@ -7,6 +7,7 @@ namespace Internal.Utilities.Text.Parsing
open Internal.Utilities.Text.Lexing

open System
open System.Buffers

exception RecoverableParseError
exception Accept of obj
Expand Down Expand Up @@ -131,11 +132,7 @@ module internal Implementation =
//-------------------------------------------------------------------------
// Read the tables written by FSYACC.

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

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

let parseState =
Expand Down

0 comments on commit 9233270

Please sign in to comment.