提交 9e5e058e 编写于 作者: D dotnet bot 提交者: Kevin Ransom (msft)

Reduced allocation by reading from a stream into an array for ILResource (#4689) (#4721)

* Reduced allocation by reading from a stream into an array for ILResource

* GetRawFSharpSignatureData is now lazy getting bytes. This is what GetRawFSharpOptimizationData does

* Using File.ReadBinaryChunk
上级 50287241
......@@ -8,6 +8,7 @@ module Microsoft.FSharp.Compiler.AbstractIL.IL
open System
open System.IO
open System.Collections
open System.Collections.Generic
open System.Collections.Concurrent
......@@ -1986,7 +1987,7 @@ type ILResource =
member r.GetBytes() =
match r.Location with
| ILResourceLocation.LocalIn (file, start, len) ->
FileSystem.ReadAllBytesShim(file).[start .. start + len - 1]
File.ReadBinaryChunk(file, start, len)
| ILResourceLocation.LocalOut bytes -> bytes
| _ -> failwith "GetBytes"
......
......@@ -1291,7 +1291,7 @@ module Shim =
static member ReadBinaryChunk (fileName, start, len) =
use stream = FileSystem.FileStreamReadShim fileName
stream.Seek(int64 start, SeekOrigin.Begin) |> ignore
let buffer = Array.zeroCreate len
let buffer = Array.zeroCreate len
let mutable n = 0
while n < len do
n <- n + stream.Read(buffer, n, len-n)
......
......@@ -2098,7 +2098,7 @@ type IRawFSharpAssemblyData =
/// in the language service
abstract TryGetILModuleDef : unit -> ILModuleDef option
/// The raw F# signature data in the assembly, if any
abstract GetRawFSharpSignatureData : range * ilShortAssemName: string * fileName: string -> (string * byte[]) list
abstract GetRawFSharpSignatureData : range * ilShortAssemName: string * fileName: string -> (string * (unit -> byte[])) list
/// The raw F# optimization data in the assembly, if any
abstract GetRawFSharpOptimizationData : range * ilShortAssemName: string * fileName: string -> (string * (unit -> byte[])) list
/// The table of type forwarders in the assembly
......@@ -3795,7 +3795,7 @@ let PickleToResource inMem file g scope rname p x =
MetadataIndex = NoMetadataIdx }
let GetSignatureData (file, ilScopeRef, ilModule, byteReader) : PickledDataWithReferences<PickledCcuInfo> =
unpickleObjWithDanglingCcus file ilScopeRef ilModule unpickleCcuInfo byteReader
unpickleObjWithDanglingCcus file ilScopeRef ilModule unpickleCcuInfo (byteReader())
let WriteSignatureData (tcConfig: TcConfig, tcGlobals, exportRemapping, ccu: CcuThunk, file, inMem) : ILResource =
let mspec = ccu.Contents
......@@ -3831,16 +3831,15 @@ type RawFSharpAssemblyDataBackedByFileOnDisk (ilModule: ILModuleDef, ilAssemblyR
let sigDataReaders =
[ for iresource in resources do
if IsSignatureDataResource iresource then
let ccuName = GetSignatureDataResourceName iresource
let bytes = iresource.GetBytes()
yield (ccuName, bytes) ]
let ccuName = GetSignatureDataResourceName iresource
yield (ccuName, fun () -> iresource.GetBytes()) ]
let sigDataReaders =
if sigDataReaders.IsEmpty && List.contains ilShortAssemName externalSigAndOptData then
let sigFileName = Path.ChangeExtension(filename, "sigdata")
if not (FileSystem.SafeExists sigFileName) then
error(Error(FSComp.SR.buildExpectedSigdataFile (FileSystem.GetFullPathShim sigFileName), m))
[ (ilShortAssemName, FileSystem.ReadAllBytesShim sigFileName)]
[ (ilShortAssemName, fun () -> FileSystem.ReadAllBytesShim sigFileName)]
else
sigDataReaders
sigDataReaders
......
......@@ -149,7 +149,7 @@ type IRawFSharpAssemblyData =
abstract HasAnyFSharpSignatureDataAttribute: bool
abstract HasMatchingFSharpSignatureDataAttribute: ILGlobals -> bool
/// The raw F# signature data in the assembly, if any
abstract GetRawFSharpSignatureData: range * ilShortAssemName: string * fileName: string -> (string * byte[]) list
abstract GetRawFSharpSignatureData: range * ilShortAssemName: string * fileName: string -> (string * (unit -> byte[])) list
/// The raw F# optimization data in the assembly, if any
abstract GetRawFSharpOptimizationData: range * ilShortAssemName: string * fileName: string -> (string * (unit -> byte[])) list
/// The table of type forwarders in the assembly
......
......@@ -1172,8 +1172,7 @@ type RawFSharpAssemblyDataBackedByLanguageService (tcConfig, tcGlobals, tcState:
let _sigDataAttributes, sigDataResources = Driver.EncodeInterfaceData(tcConfig, tcGlobals, exportRemapping, generatedCcu, outfile, true)
[ for r in sigDataResources do
let ccuName = GetSignatureDataResourceName r
let bytes = r.GetBytes()
yield (ccuName, bytes) ]
yield (ccuName, (fun () -> r.GetBytes())) ]
let autoOpenAttrs = topAttrs.assemblyAttrs |> List.choose (List.singleton >> TryFindFSharpStringAttribute tcGlobals tcGlobals.attrib_AutoOpenAttribute)
let ivtAttrs = topAttrs.assemblyAttrs |> List.choose (List.singleton >> TryFindFSharpStringAttribute tcGlobals tcGlobals.attrib_InternalsVisibleToAttribute)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册