Skip to content

Commit

Permalink
improve rowWithRange SkipSearch performance
Browse files Browse the repository at this point in the history
  • Loading branch information
HLWeil committed Jan 26, 2024
1 parent 974a4cd commit e7cc638
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/FsSpreadsheet/FsWorksheet.fs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,13 @@ type FsWorksheet (name, ?fsRows, ?fsTables, ?fsCellsCollection) =
/// <summary>
/// Returns the FsRow at the given index. If it does not exist, it is created and appended first.
/// </summary>
member self.Row(rowIndex) =
member self.Row(rowIndex, ?SkipSearch) =
let skipSearch = defaultArg SkipSearch false
if skipSearch then
let row = FsRow.createAt(rowIndex,self.CellCollection)
_rows.Add row
row
else
match _rows |> Seq.tryFind (fun row -> row.Index = rowIndex) with
| Some row ->
row
Expand All @@ -150,7 +156,7 @@ type FsWorksheet (name, ?fsRows, ?fsTables, ?fsCellsCollection) =
if rangeAddress.FirstAddress.RowNumber <> rangeAddress.LastAddress.RowNumber then
failwithf "Row may not have a range address spanning over different row indices"
if skipSearch then
let row = FsRow.createAt(rangeAddress.FirstAddress.RowNumber,self.CellCollection)
let row = FsRow (rangeAddress, self.CellCollection)
row.RangeAddress <- rangeAddress
_rows.Add row
row
Expand Down
30 changes: 28 additions & 2 deletions tests/FsSpreadsheet.Tests/FsRowTests.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module FsRow


open TestingUtils
#if FABLE_COMPILER
open Fable.Mocha
#else
Expand All @@ -17,7 +17,27 @@ let getDummyWorkSheet() =
worksheet.RescanRows()
worksheet

let main =

let performace =
testList "performance" [
testCase "FrowFromRange-SkipSearch" (fun () ->
let rowCount= 100000
let ws = FsWorksheet.init("MyWorksheet")
let timer = Stopwatch()
timer.Start()
for i = 1 to rowCount do
let address = FsRangeAddress(FsAddress(i,1), FsAddress(i,1))
ws.RowWithRange(address,true) |> ignore
timer.Stop()
let runtime = timer.Elapsed.Milliseconds
let expected = 50 // this is too high and should be reduced

Expect.equal ws.Rows.Count rowCount "Row count"
Expect.isTrue (runtime <= expected) $"Expected conversion to be finished in under {expected}, but it took {runtime}"
)
]

let rowOperations =
testList "rowOperations" [
testList "Prerequisites" [
let dummyWorkSheet = getDummyWorkSheet()
Expand Down Expand Up @@ -117,3 +137,9 @@ let main =
Expect.equal maxColIndex 1 "Incorrect index"
]
]

let main =
testList "FsRow" [
rowOperations
performace
]
1 change: 1 addition & 0 deletions tests/FsSpreadsheet.Tests/FsSpreadsheet.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\FsSpreadsheet\FsSpreadsheet.fsproj" />
<ProjectReference Include="..\TestUtils\TestUtils.fsproj" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit e7cc638

Please sign in to comment.