Skip to content

Commit

Permalink
Optimize metadata members and custom attributes reading (dotnet#17364)
Browse files Browse the repository at this point in the history
* wip

* wip

* cleanup

* formatting

* release notes

* release notes

---------

Co-authored-by: Petr <psfinaki@users.noreply.github.com>
Co-authored-by: Vlad Zarytovskii <vzaritovsky@hotmail.com>
  • Loading branch information
3 people committed Jul 27, 2024
1 parent 3dbbcc7 commit aa2a9e1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 27 deletions.
1 change: 1 addition & 0 deletions docs/release-notes/.FSharp.Compiler.Service/9.0.100.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

* Change compiler default setting realsig+ when building assemblies ([Issue #17384](https://github.com/dotnet/fsharp/issues/17384), [PR #17378](https://github.com/dotnet/fsharp/pull/17385))
* Change compiler default setting for compressedMetadata ([Issue #17379](https://github.com/dotnet/fsharp/issues/17379), [PR #17383](https://github.com/dotnet/fsharp/pull/17383))
* Optimize metadata reading for type members and custom attributes. ([PR #17364](https://github.com/dotnet/fsharp/pull/17364))
* Enforce `AttributeTargets` on unions. ([PR #17389](https://github.com/dotnet/fsharp/pull/17389))

### Breaking Changes
74 changes: 47 additions & 27 deletions src/Compiler/AbstractIL/ilread.fs
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ let tomdCompare (TaggedIndex(t1: TypeOrMethodDefTag, idx1)) (TaggedIndex(t2: Typ
elif idx1 > idx2 then 1
else compare t1.Tag t2.Tag

let simpleIndexCompare (idx1: int) (idx2: int) = compare idx1 idx2
let inline simpleIndexCompare (idx1: int) (idx2: int) = compare idx1 idx2

//---------------------------------------------------------------------
// The various keys for the various caches.
Expand Down Expand Up @@ -2314,18 +2314,23 @@ and seekReadTypeDefAsTypeUncached ctxtH (TypeDefAsTypIdx(boxity, ginst, idx)) =
mkILTy boxity (ILTypeSpec.Create(seekReadTypeDefAsTypeRef ctxt idx, ginst))

and seekReadTypeDefAsTypeRef (ctxt: ILMetadataReader) idx =
let mdv = ctxt.mdfile.GetView()

let enc =
if seekIsTopTypeDefOfIdx ctxt idx then
[]
else
let enclIdx =
seekReadIndexedRow (
ctxt.getNumRows TableNames.Nested,
seekReadNestedRow ctxt,
fst,
simpleIndexCompare idx,
id,
id,
(fun i ->
let mutable addr = ctxt.rowAddr TableNames.Nested i
let nestedIdx = seekReadUntaggedIdx TableNames.TypeDef ctxt mdv &addr
simpleIndexCompare idx nestedIdx),
isSorted ctxt TableNames.Nested,
snd
(fun i -> seekReadNestedRow ctxt i |> snd)
)

let tref = seekReadTypeDefAsTypeRef ctxt enclIdx
Expand Down Expand Up @@ -3086,15 +3091,18 @@ and seekReadMethodImpls (ctxt: ILMetadataReader) numTypars tidx =
let mimpls =
seekReadIndexedRows (
ctxt.getNumRows TableNames.MethodImpl,
seekReadMethodImplRow ctxt mdv,
(fun (a, _, _) -> a),
simpleIndexCompare tidx,
id,
id,
(fun i ->
let mutable addr = ctxt.rowAddr TableNames.MethodImpl i
let _tidx = seekReadUntaggedIdx TableNames.TypeDef ctxt mdv &addr
simpleIndexCompare tidx _tidx),
isSorted ctxt TableNames.MethodImpl,
(fun (_, b, c) -> b, c)
seekReadMethodImplRow ctxt mdv
)

mimpls
|> List.map (fun (b, c) ->
|> List.map (fun (_, b, c) ->
{
OverrideBy =
let (MethodData(enclTy, cc, nm, argTys, retTy, methInst)) =
Expand Down Expand Up @@ -3163,11 +3171,14 @@ and seekReadEvents (ctxt: ILMetadataReader) numTypars tidx =
match
seekReadOptionalIndexedRow (
ctxt.getNumRows TableNames.EventMap,
(fun i -> i, seekReadEventMapRow ctxt mdv i),
(fun (_, row) -> fst row),
compare tidx,
id,
id,
(fun i ->
let mutable addr = ctxt.rowAddr TableNames.EventMap i
let _tidx = seekReadUntaggedIdx TableNames.TypeDef ctxt mdv &addr
simpleIndexCompare tidx _tidx),
false,
(fun (i, row) -> (i, snd row))
(fun i -> i, seekReadEventMapRow ctxt mdv i |> snd)
)
with
| None -> []
Expand Down Expand Up @@ -3230,11 +3241,14 @@ and seekReadProperties (ctxt: ILMetadataReader) numTypars tidx =
match
seekReadOptionalIndexedRow (
ctxt.getNumRows TableNames.PropertyMap,
(fun i -> i, seekReadPropertyMapRow ctxt mdv i),
(fun (_, row) -> fst row),
compare tidx,
id,
id,
(fun i ->
let mutable addr = ctxt.rowAddr TableNames.PropertyMap i
let _tidx = seekReadUntaggedIdx TableNames.TypeDef ctxt mdv &addr
simpleIndexCompare tidx _tidx),
false,
(fun (i, row) -> (i, snd row))
(fun i -> i, seekReadPropertyMapRow ctxt mdv i |> snd)
)
with
| None -> []
Expand All @@ -3258,16 +3272,22 @@ and customAttrsReader ctxtH tag : ILAttributesStored =
let (ctxt: ILMetadataReader) = getHole ctxtH
let mdv = ctxt.mdfile.GetView()

let reader =
{ new ISeekReadIndexedRowReader<CustomAttributeRow, TaggedIndex<HasCustomAttributeTag>, ILAttribute> with
member _.GetRow(i, row) =
seekReadCustomAttributeRow ctxt mdv i &row

member _.GetKey(attrRow) = attrRow.parentIndex

member _.CompareKey(key) = hcaCompare (TaggedIndex(tag, idx)) key
let searchedKey = TaggedIndex(tag, idx)

member _.ConvertRow(attrRow) =
let reader =
{ new ISeekReadIndexedRowReader<int, int, ILAttribute> with
member _.GetRow(i, rowIndex) = rowIndex <- i
member _.GetKey(rowIndex) = rowIndex

member _.CompareKey(rowIndex) =
let mutable addr = ctxt.rowAddr TableNames.CustomAttribute rowIndex
// read parentIndex
let key = seekReadHasCustomAttributeIdx ctxt mdv &addr
hcaCompare searchedKey key

member _.ConvertRow(rowIndex) =
let mutable attrRow = Unchecked.defaultof<_>
seekReadCustomAttributeRow ctxt mdv rowIndex &attrRow
seekReadCustomAttr ctxt (attrRow.typeIndex, attrRow.valueIndex)
}

Expand Down

0 comments on commit aa2a9e1

Please sign in to comment.