life if-statement up (so that the test is only done once, to help the branch predictor)

This commit is contained in:
2024-07-04 03:24:34 +01:00
parent b70763b7b8
commit c6fef0499e
3 changed files with 90 additions and 77 deletions

BIN
bench/line_gap_svelte Executable file

Binary file not shown.

BIN
bench/line_rope_svelte Executable file

Binary file not shown.

View File

@@ -41,7 +41,6 @@ struct
, rightLines: int vector list
}
val stringLimit = 1024
val vecLimit = 32
@@ -268,33 +267,41 @@ struct
then
(* If we can join newString/lines with sub1 while
* staying in limit. *)
let
val newLeftLines =
if midpoint >= 0 then
(* Implicit: a binSearch match was found. *)
let
val newLeftLinesLength = midpoint + 1 + Vector.length newLines
in
Vector.tabulate (newLeftLinesLength, fn idx =>
val newLeftLines = Vector.tabulate (newLeftLinesLength, fn idx =>
if idx <= midpoint then
Vector.sub (leftLinesHd, idx)
else
Vector.sub (newLines, idx - (midpoint + 1))
+ String.size strSub1)
end
else
Vector.map (fn el => el + String.size strSub1) newLines
val newRightLines =
if midpoint >= 0 then
(* Implicit: a binSearch match was found. *)
Vector.tabulate
( (Vector.length leftLinesHd - midpoint) - 1
, fn idx =>
Vector.sub (leftLinesHd, idx + midpoint + 1)
- String.size strSub1
)
in
{ idx = prevIdx + String.size strSub1 + String.size newString
, line =
(curLine - Vector.length leftLinesHd)
+ Vector.length newLeftLines
, leftStrings = (strSub1 ^ newString) :: leftStringsTl
, leftLines = newLeftLines :: leftLinesTl
, rightStrings = strSub2 :: rightStrings
, rightLines = newRightLines :: rightLines
}
end
else
let
(* No binSearch result found. *)
val newLeftLines =
Vector.map (fn el => el + String.size strSub1) newLines
val newRightLines =
Vector.map (fn idx => idx - String.size strSub1) leftLinesHd
in
{ idx = prevIdx + String.size strSub1 + String.size newString
@@ -594,35 +601,42 @@ struct
then
(* If we can join newString/lines with sub1 while
* staying in limit. *)
let
(* strSub1 ^ newString is placed on the left list. *)
val newLeftStringsHd = strSub1 ^ newString
val newLeftLinesHd =
if midpoint >= 0 then
(* Implicit: a binSearch match was found. *)
let
(* Implicit: a binSearch match was found. *)
val newLeftStringsHd = strSub1 ^ newString
val newLeftLinesLength = midpoint + 1 + Vector.length newLines
in
val newLeftLinesHd =
Vector.tabulate (newLeftLinesLength, fn idx =>
if idx <= midpoint then
Vector.sub (rightLinesHd, idx)
else
Vector.sub (newLines, idx - (midpoint + 1))
+ String.size strSub1)
end
else
Vector.map (fn el => el + String.size strSub1) newLines
val newRightLinesHd =
if midpoint >= 0 then
(* Implicit: a binSearch match was found. *)
Vector.tabulate
( (Vector.length rightLinesHd - midpoint) - 1
, fn idx =>
Vector.sub (rightLinesHd, idx + midpoint + 1)
- String.size strSub1
)
in
{ idx = curIdx + String.size newLeftStringsHd
, line = curLine + Vector.length newLeftLinesHd
, leftStrings = newLeftStringsHd :: leftStrings
, leftLines = newLeftLinesHd :: leftLines
, rightStrings = strSub2 :: rightStringsTl
, rightLines = newRightLinesHd :: rightLinesTl
}
end
else
let
(* No binSearch match found. *)
val newLeftStringsHd = strSub1 ^ newString
val newLeftLinesHd =
Vector.map (fn el => el + String.size strSub1) newLines
val newRightLinesHd =
Vector.map (fn idx => idx - String.size strSub1) rightLinesHd
in
{ idx = curIdx + String.size newLeftStringsHd
@@ -668,7 +682,6 @@ struct
+ Vector.length newLeftLinesHd
) - String.size strSub1) + String.size newString
)
in
{ idx = curIdx + String.size strSub1
, line = curLine + Vector.length newLeftLinesHd