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