Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IL: optimize attribute cluster reading (Fixed version) #15941

Merged
merged 10 commits into from
Oct 31, 2023
56 changes: 42 additions & 14 deletions src/Compiler/AbstractIL/ilread.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1201,9 +1201,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 @@ -1222,12 +1225,13 @@ 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
endRid <- mid

// read backwards
let mutable fin = false
let mutable curr = mid - 1
Expand All @@ -1239,17 +1243,15 @@ 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
let mutable curr = mid + 1

while not fin do
if curr > numRows then
Expand All @@ -1258,23 +1260,49 @@ 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

rid <- rid + 1

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