Skip to content

Commit

Permalink
IL: optimize attribute cluster reading (#13821)
Browse files Browse the repository at this point in the history
* IL: optimize attribute cluster reading

* Set endRid when there's a single attribute

* formatter

* More fixes

Co-authored-by: Vlad Zarytovskii <[email protected]>
Co-authored-by: Don Syme <[email protected]>
  • Loading branch information
3 people authored Sep 23, 2022
1 parent 2f471de commit 179db4e
Showing 1 changed file with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions src/Compiler/AbstractIL/ilread.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1220,9 +1220,12 @@ type ISeekReadIndexedRowReader<'RowT, 'KeyT, 'T when 'RowT: struct> =
abstract CompareKey: 'KeyT -> int
abstract ConvertRow: byref<'RowT> -> 'T

let seekReadIndexedRowsByInterface numRows binaryChop (reader: ISeekReadIndexedRowReader<'RowT, _, _>) =
let seekReadIndexedRowsRange numRows binaryChop (reader: ISeekReadIndexedRowReader<'RowT, _, _>) =
let mutable row = Unchecked.defaultof<'RowT>

let mutable startRid = -1
let mutable endRid = -1

if binaryChop then
let mutable low = 0
let mutable high = numRows + 1
Expand All @@ -1241,12 +1244,12 @@ let seekReadIndexedRowsByInterface numRows binaryChop (reader: ISeekReadIndexedR
elif c < 0 then high <- mid
else fin <- true

let res = ImmutableArray.CreateBuilder()

if high - low > 1 then
// now read off rows, forward and backwards
let mid = (low + high) / 2

startRid <- mid

// read backwards
let mutable fin = false
let mutable curr = mid - 1
Expand All @@ -1258,14 +1261,12 @@ let seekReadIndexedRowsByInterface numRows binaryChop (reader: ISeekReadIndexedR
reader.GetRow(curr, &row)

if reader.CompareKey(reader.GetKey(&row)) = 0 then
res.Add(reader.ConvertRow(&row))
startRid <- curr
else
fin <- true

curr <- curr - 1

res.Reverse()

// read forward
let mutable fin = false
let mutable curr = mid
Expand All @@ -1277,23 +1278,47 @@ let seekReadIndexedRowsByInterface numRows binaryChop (reader: ISeekReadIndexedR
reader.GetRow(curr, &row)

if reader.CompareKey(reader.GetKey(&row)) = 0 then
res.Add(reader.ConvertRow(&row))
endRid <- curr
else
fin <- true

curr <- curr + 1

res.ToArray()
else
let res = ImmutableArray.CreateBuilder()
let mutable rid = 1

for i = 1 to numRows do
reader.GetRow(i, &row)
while rid <= numRows && startRid = -1 do
reader.GetRow(rid, &row)

if reader.CompareKey(reader.GetKey(&row)) = 0 then
res.Add(reader.ConvertRow(&row))
startRid <- rid
endRid <- rid

rid <- rid + 1

let mutable fin = false

while rid <= numRows && not fin do
reader.GetRow(rid, &row)

if reader.CompareKey(reader.GetKey(&row)) = 0 then
endRid <- rid
else
fin <- true

startRid, endRid

let seekReadIndexedRowsByInterface numRows binaryChop (reader: ISeekReadIndexedRowReader<'RowT, _, _>) =
let startRid, endRid = seekReadIndexedRowsRange numRows binaryChop reader

if startRid <= 0 || endRid < startRid then
[||]
else

res.ToArray()
Array.init (endRid - startRid + 1) (fun i ->
let mutable row = Unchecked.defaultof<'RowT>
reader.GetRow(startRid + i, &row)
reader.ConvertRow(&row))

[<Struct>]
type CustomAttributeRow =
Expand Down

0 comments on commit 179db4e

Please sign in to comment.