move if statement that only needs to be checked once from helpBinSearch, which is recursive, to binSearch which is not recursive, for negligibly better performance (fewer branch predictions)

This commit is contained in:
2024-06-30 01:37:52 +01:00
parent 75aba5e8e1
commit ba7d3579b4

View File

@@ -49,7 +49,13 @@ struct
val vecLimit = 32
val empty =
{idx = 0, leftStrings = [], rightStrings = [], line = 0, leftLines = [], rightLines = []}
{ idx = 0
, leftStrings = []
, rightStrings = []
, line = 0
, leftLines = []
, rightLines = []
}
local
fun helpToString (acc, input) =
@@ -71,9 +77,6 @@ struct
local
fun helpBinSearch (findNum, lines, low, high) =
if Vector.length lines = 0 then
0
else
let
val mid = low + ((high - low) div 2)
in
@@ -93,7 +96,8 @@ struct
end
in
fun binSearch (findNum, lines) =
helpBinSearch (findNum, lines, 0, Vector.length lines)
if Vector.length lines = 0 then 0
else helpBinSearch (findNum, lines, 0, Vector.length lines)
end
fun insWhenIdxAndCurIdxAreEqual
@@ -211,14 +215,17 @@ struct
end
else
(* Just cons everything; no way we can join while staying in limit. *)
let val _ = print "line 210\n" in
let
val _ = print "line 210\n"
in
{ idx = curIdx + String.size newString
, line = curLine + Vector.length newLines
, leftStrings = leftStringsHd :: newString :: leftStringsTl
, leftLines = leftLinesHd :: newLines :: leftLinesTl
, rightStrings = rightStrings
, rightLines = rightLines
} end
}
end
else
(* Need to insert in the middle of the left list. *)
let
@@ -228,7 +235,8 @@ struct
val strSub2 = String.substring
(leftStringsHd, strLength, String.size leftStringsHd - strLength)
val midpoint = binSearch (String.size strSub1, leftLinesHd)
val _ = print ("str size:" ^ Int.toString (Vector.length leftLinesHd) ^ "\n")
val _ = print
("str size:" ^ Int.toString (Vector.length leftLinesHd) ^ "\n")
val _ = print ("midpoint:" ^ Int.toString (midpoint) ^ "\n")
in
if
@@ -268,9 +276,9 @@ struct
* staying in limit. *)
let
val _ = print "line 268\n"
val _ = print ("vector length: " ^ Int.toString (Vector.length newLines ) ^ "\n")
val _ = print ("midpoint: " ^ Int.toString (
midpoint) ^ "\n")
val _ = print
("vector length: " ^ Int.toString (Vector.length newLines) ^ "\n")
val _ = print ("midpoint: " ^ Int.toString (midpoint) ^ "\n")
val newLeftLines =
Vector.tabulate (midpoint + Vector.length newLines, fn idx =>
if idx < midpoint then Vector.sub (leftLinesHd, idx)
@@ -505,14 +513,17 @@ struct
else
(* Cons newString and newLines to after-the-head,
* because we can't join while staying in the limit.*)
let val _ = print "line 498\n" in
let
val _ = print "line 498\n"
in
{ idx = curIdx
, line = curLine
, leftStrings = leftStrings
, leftLines = leftLines
, rightStrings = rightStringsHd :: newString :: rightStringsTl
, rightLines = rightLinesHd :: newLines :: rightLinesTl
} end
}
end
else
(* Have to split rightStringsHd and rightLinesHd in the middle. *)
let
@@ -563,11 +574,8 @@ struct
val newLeftStringsHd = strSub1 ^ newString
val newLeftLinesHd =
Vector.tabulate (Vector.length newLines + midpoint, fn idx =>
if idx < midpoint then
Vector.sub (rightLinesHd, idx)
else
Vector.sub (newLines, accessIdx) + String.size strSub1
)
if idx < midpoint then Vector.sub (rightLinesHd, idx)
else Vector.sub (newLines, accessIdx) + String.size strSub1)
val _ = print "line 584\n"
val newRightLinesHd =
@@ -734,14 +742,17 @@ struct
end
| (_, _) =>
(* Right string/line is empty. *)
let val _ = print "line 723\n" in
let
val _ = print "line 723\n"
in
{ idx = curIdx
, line = curLine
, leftStrings = leftStrings
, leftLines = leftLines
, rightStrings = [newString]
, rightLines = [newLines]
} end
}
end
fun ins
( idx