From 9e951aa8a1e92176232f5013bbfde71f8fc56870 Mon Sep 17 00:00:00 2001 From: Eugene Auduchinok Date: Thu, 1 Sep 2022 12:05:32 +0200 Subject: [PATCH 1/4] IL: optimize attribute cluster reading --- src/Compiler/AbstractIL/ilread.fs | 45 ++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/src/Compiler/AbstractIL/ilread.fs b/src/Compiler/AbstractIL/ilread.fs index c323a9eb939..1421480171f 100644 --- a/src/Compiler/AbstractIL/ilread.fs +++ b/src/Compiler/AbstractIL/ilread.fs @@ -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, _, _>) : int * int = 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 @@ -1241,8 +1244,6 @@ 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 @@ -1256,16 +1257,13 @@ let seekReadIndexedRowsByInterface numRows binaryChop (reader: ISeekReadIndexedR fin <- true else 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 @@ -1275,25 +1273,40 @@ let seekReadIndexedRowsByInterface numRows binaryChop (reader: ISeekReadIndexedR fin <- true else 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() - - for i = 1 to numRows do - reader.GetRow(i, &row) + let mutable rid = 1 + while rid <= numRows && startRid = -1 do + reader.GetRow(rid, &row) + if reader.CompareKey(reader.GetKey(&row)) = 0 then + startRid <- rid + endRid <- rid + rid <- rid + 1 + let mutable fin = false + while not fin do + reader.GetRow(rid, &row) if reader.CompareKey(reader.GetKey(&row)) = 0 then - res.Add(reader.ConvertRow(&row)) + 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)) [] type CustomAttributeRow = From 24d63abc45f3586bbc06a3347c0a2bdd9b781758 Mon Sep 17 00:00:00 2001 From: Eugene Auduchinok Date: Mon, 12 Sep 2022 12:12:42 +0200 Subject: [PATCH 2/4] Set endRid when there's a single attribute --- src/Compiler/AbstractIL/ilread.fs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Compiler/AbstractIL/ilread.fs b/src/Compiler/AbstractIL/ilread.fs index 1421480171f..143a682b31e 100644 --- a/src/Compiler/AbstractIL/ilread.fs +++ b/src/Compiler/AbstractIL/ilread.fs @@ -1220,7 +1220,7 @@ type ISeekReadIndexedRowReader<'RowT, 'KeyT, 'T when 'RowT: struct> = abstract CompareKey: 'KeyT -> int abstract ConvertRow: byref<'RowT> -> 'T -let seekReadIndexedRowsRange numRows binaryChop (reader: ISeekReadIndexedRowReader<'RowT, _, _>) : int * int = +let seekReadIndexedRowsRange numRows binaryChop (reader: ISeekReadIndexedRowReader<'RowT, _, _>) = let mutable row = Unchecked.defaultof<'RowT> let mutable startRid = -1 @@ -1264,6 +1264,8 @@ let seekReadIndexedRowsRange numRows binaryChop (reader: ISeekReadIndexedRowRead curr <- curr - 1 + endRid <- mid + // read forward let mutable fin = false let mutable curr = mid From eeefd2c239bdc24269a4eb948298a0efa9e5ed29 Mon Sep 17 00:00:00 2001 From: Eugene Auduchinok Date: Mon, 12 Sep 2022 12:21:41 +0200 Subject: [PATCH 3/4] formatter --- src/Compiler/AbstractIL/ilread.fs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/Compiler/AbstractIL/ilread.fs b/src/Compiler/AbstractIL/ilread.fs index 143a682b31e..af6dd95d00a 100644 --- a/src/Compiler/AbstractIL/ilread.fs +++ b/src/Compiler/AbstractIL/ilread.fs @@ -1257,6 +1257,7 @@ let seekReadIndexedRowsRange numRows binaryChop (reader: ISeekReadIndexedRowRead fin <- true else reader.GetRow(curr, &row) + if reader.CompareKey(reader.GetKey(&row)) = 0 then startRid <- curr else @@ -1275,6 +1276,7 @@ let seekReadIndexedRowsRange numRows binaryChop (reader: ISeekReadIndexedRowRead fin <- true else reader.GetRow(curr, &row) + if reader.CompareKey(reader.GetKey(&row)) = 0 then endRid <- curr else @@ -1284,16 +1286,21 @@ let seekReadIndexedRowsRange numRows binaryChop (reader: ISeekReadIndexedRowRead else let mutable rid = 1 + while rid <= numRows && startRid = -1 do reader.GetRow(rid, &row) + if reader.CompareKey(reader.GetKey(&row)) = 0 then startRid <- rid endRid <- rid + rid <- rid + 1 let mutable fin = false + while not fin do reader.GetRow(rid, &row) + if reader.CompareKey(reader.GetKey(&row)) = 0 then endRid <- rid else @@ -1303,12 +1310,15 @@ let seekReadIndexedRowsRange numRows binaryChop (reader: ISeekReadIndexedRowRead let seekReadIndexedRowsByInterface numRows binaryChop (reader: ISeekReadIndexedRowReader<'RowT, _, _>) = let startRid, endRid = seekReadIndexedRowsRange numRows binaryChop reader - if startRid < 0 || endRid < startRid then [||] else - Array.init (endRid - startRid + 1) (fun i -> - let mutable row = Unchecked.defaultof<'RowT> - reader.GetRow(startRid + i, &row) - reader.ConvertRow(&row)) + if startRid < 0 || endRid < startRid then + [||] + else + + Array.init (endRid - startRid + 1) (fun i -> + let mutable row = Unchecked.defaultof<'RowT> + reader.GetRow(startRid + i, &row) + reader.ConvertRow(&row)) [] type CustomAttributeRow = From 168304c56e7cbc42126d6f2f7628e163a1db6eaa Mon Sep 17 00:00:00 2001 From: Eugene Auduchinok Date: Mon, 12 Sep 2022 13:10:21 +0200 Subject: [PATCH 4/4] More fixes --- src/Compiler/AbstractIL/ilread.fs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Compiler/AbstractIL/ilread.fs b/src/Compiler/AbstractIL/ilread.fs index af6dd95d00a..06c50483a2e 100644 --- a/src/Compiler/AbstractIL/ilread.fs +++ b/src/Compiler/AbstractIL/ilread.fs @@ -1248,6 +1248,8 @@ let seekReadIndexedRowsRange numRows binaryChop (reader: ISeekReadIndexedRowRead // 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 @@ -1265,8 +1267,6 @@ let seekReadIndexedRowsRange numRows binaryChop (reader: ISeekReadIndexedRowRead curr <- curr - 1 - endRid <- mid - // read forward let mutable fin = false let mutable curr = mid @@ -1298,7 +1298,7 @@ let seekReadIndexedRowsRange numRows binaryChop (reader: ISeekReadIndexedRowRead let mutable fin = false - while not fin do + while rid <= numRows && not fin do reader.GetRow(rid, &row) if reader.CompareKey(reader.GetKey(&row)) = 0 then @@ -1311,7 +1311,7 @@ let seekReadIndexedRowsRange numRows binaryChop (reader: ISeekReadIndexedRowRead let seekReadIndexedRowsByInterface numRows binaryChop (reader: ISeekReadIndexedRowReader<'RowT, _, _>) = let startRid, endRid = seekReadIndexedRowsRange numRows binaryChop reader - if startRid < 0 || endRid < startRid then + if startRid <= 0 || endRid < startRid then [||] else