From 528882864686823d0656fe83b3e2f32938179636 Mon Sep 17 00:00:00 2001 From: TIHan Date: Wed, 15 Jan 2020 18:00:48 -0800 Subject: [PATCH 1/4] Using ArrayPool --- eng/Versions.props | 1 + .../FSharp.Compiler.Service.fsproj | 1 + .../FSharp.Compiler.Private.fsproj | 1 + src/utils/prim-lexing.fs | 1 + src/utils/prim-parsing.fs | 17 ++++++++++------- 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/eng/Versions.props b/eng/Versions.props index f7611980e00..64a7ddd7135 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -95,6 +95,7 @@ 4.3.0 4.3.0 4.5.0 + 4.5.0 $(RoslynVersion) $(RoslynVersion) diff --git a/fcs/FSharp.Compiler.Service/FSharp.Compiler.Service.fsproj b/fcs/FSharp.Compiler.Service/FSharp.Compiler.Service.fsproj index f99d7aac7af..4ba9a9b2c32 100644 --- a/fcs/FSharp.Compiler.Service/FSharp.Compiler.Service.fsproj +++ b/fcs/FSharp.Compiler.Service/FSharp.Compiler.Service.fsproj @@ -680,6 +680,7 @@ + diff --git a/src/fsharp/FSharp.Compiler.Private/FSharp.Compiler.Private.fsproj b/src/fsharp/FSharp.Compiler.Private/FSharp.Compiler.Private.fsproj index 8710d929be5..c919f3056b1 100644 --- a/src/fsharp/FSharp.Compiler.Private/FSharp.Compiler.Private.fsproj +++ b/src/fsharp/FSharp.Compiler.Private/FSharp.Compiler.Private.fsproj @@ -743,6 +743,7 @@ + diff --git a/src/utils/prim-lexing.fs b/src/utils/prim-lexing.fs index 1f772d6e87e..41ab908ee4a 100644 --- a/src/utils/prim-lexing.fs +++ b/src/utils/prim-lexing.fs @@ -6,6 +6,7 @@ namespace FSharp.Compiler.Text open System open System.IO +open System.Buffers type ISourceText = diff --git a/src/utils/prim-parsing.fs b/src/utils/prim-parsing.fs index b58e592278d..10a25fd8fe5 100644 --- a/src/utils/prim-parsing.fs +++ b/src/utils/prim-parsing.fs @@ -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 @@ -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 (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 @@ -234,8 +231,14 @@ 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.Shared.Rent(cacheSize * 2) + let gotoTableCache = ArrayPool.Shared.Rent(cacheSize * 2) + use _cacheDisposal = { new IDisposable with member _.Dispose() = ArrayPool.Shared.Return actionTableCache; ArrayPool.Shared.Return gotoTableCache } + let actionTable = new AssocTable(tables.actionTableElements, tables.actionTableRowOffsets, actionTableCache, cacheSize) + let gotoTable = new AssocTable(tables.gotos, tables.sparseGotoTableRowOffsets, gotoTableCache, cacheSize) let stateToProdIdxsTable = new IdxToIdxListTable(tables.stateToProdIdxsTableElements, tables.stateToProdIdxsTableRowOffsets) let parseState = From c0cc66698841e74e1babb13da1c3e3c9c9cd9feb Mon Sep 17 00:00:00 2001 From: TIHan Date: Thu, 16 Jan 2020 13:09:19 -0800 Subject: [PATCH 2/4] Remove open --- src/utils/prim-lexing.fs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/utils/prim-lexing.fs b/src/utils/prim-lexing.fs index 41ab908ee4a..1f772d6e87e 100644 --- a/src/utils/prim-lexing.fs +++ b/src/utils/prim-lexing.fs @@ -6,7 +6,6 @@ namespace FSharp.Compiler.Text open System open System.IO -open System.Buffers type ISourceText = From 1882da1f571b0a81dc6b238e40e26c72ed0d1329 Mon Sep 17 00:00:00 2001 From: TIHan Date: Thu, 16 Jan 2020 13:10:54 -0800 Subject: [PATCH 3/4] Changed some style nits --- src/utils/prim-parsing.fs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/utils/prim-parsing.fs b/src/utils/prim-parsing.fs index 10a25fd8fe5..ab8eb69540d 100644 --- a/src/utils/prim-parsing.fs +++ b/src/utils/prim-parsing.fs @@ -132,7 +132,7 @@ module internal Implementation = //------------------------------------------------------------------------- // Read the tables written by FSYACC. - type AssocTable(elemTab:uint16[], offsetTab:uint16[], cache:int[], cacheSize:int) = + type AssocTable(elemTab: uint16[], offsetTab: uint16[], cache: int[], cacheSize: int) = member t.ReadAssoc (minElemNum,maxElemNum,defaultValueOfAssoc,keyToFind) = // do a binary chop on the table @@ -236,9 +236,13 @@ module internal Implementation = // hash bucket per key. let actionTableCache = ArrayPool.Shared.Rent(cacheSize * 2) let gotoTableCache = ArrayPool.Shared.Rent(cacheSize * 2) - use _cacheDisposal = { new IDisposable with member _.Dispose() = ArrayPool.Shared.Return actionTableCache; ArrayPool.Shared.Return gotoTableCache } - let actionTable = new AssocTable(tables.actionTableElements, tables.actionTableRowOffsets, actionTableCache, cacheSize) - let gotoTable = new AssocTable(tables.gotos, tables.sparseGotoTableRowOffsets, gotoTableCache, cacheSize) + use _cacheDisposal = + { new IDisposable with + member _.Dispose() = + ArrayPool.Shared.Return actionTableCache + ArrayPool.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 = From 44c2459a431332ffb450b551497299998b9124b6 Mon Sep 17 00:00:00 2001 From: TIHan Date: Thu, 16 Jan 2020 15:24:11 -0800 Subject: [PATCH 4/4] Clear arrays --- src/utils/prim-parsing.fs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/utils/prim-parsing.fs b/src/utils/prim-parsing.fs index ab8eb69540d..996cdc67d59 100644 --- a/src/utils/prim-parsing.fs +++ b/src/utils/prim-parsing.fs @@ -236,6 +236,9 @@ module internal Implementation = // hash bucket per key. let actionTableCache = ArrayPool.Shared.Rent(cacheSize * 2) let gotoTableCache = ArrayPool.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() =