change utils.sml to run using rope (with line metadata) instead of tiny_rope, and verify that line metadata is correct

This commit is contained in:
2024-03-14 23:29:10 +00:00
parent 571b16ba6c
commit f5649b4ee9
3 changed files with 63 additions and 48 deletions

View File

@@ -7,6 +7,8 @@ sig
val toString: t -> string val toString: t -> string
val foldr: ('a * string * int vector -> 'a) * 'a * t -> 'a val foldr: ('a * string * int vector -> 'a) * 'a * t -> 'a
(* The caller should not insert in the middle of a \r\n pair,
* or else line metadata will become invalid. *)
val insert: int * string * t -> t val insert: int * string * t -> t
(* The append and appendLine function both add a string to the end. (* The append and appendLine function both add a string to the end.
@@ -16,6 +18,8 @@ sig
val append: string * t -> t val append: string * t -> t
val appendLine: string * int vector * t -> t val appendLine: string * int vector * t -> t
(* The caller should not delete only a single character in a \r\n pair,
* because then line metadata will become invalid. *)
val delete: int * int * t -> t val delete: int * int * t -> t
(* This below function verifies that line metadata is as expected, (* This below function verifies that line metadata is as expected,
@@ -546,6 +550,15 @@ struct
in endInsert (rope, action) in endInsert (rope, action)
end end
fun isDelLessThanTarget (str1, str2, vec, startPoint, endPoint) =
let
val vecLength = Vector.length vec - (endPoint - startPoint)
in
String.size str1 + String.size str2 <= targetLength
andalso vecLength <= targetVecLength
end
fun delLeaf (startIdx, endIdx, str, vec) = fun delLeaf (startIdx, endIdx, str, vec) =
if if
startIdx <= 0 andalso endIdx >= String.size str startIdx <= 0 andalso endIdx >= String.size str
@@ -563,7 +576,7 @@ struct
val endPoint = binSearch (endIdx, vec, 0, vecLength) val endPoint = binSearch (endIdx, vec, 0, vecLength)
val difference = endIdx - startIdx val difference = endIdx - startIdx
in in
if isLessThanTarget (sub1, sub2) then if isDelLessThanTarget (sub1, sub2, vec, startPoint, endPoint) then
let let
val str = sub1 ^ sub2 val str = sub1 ^ sub2
val vecDifference = endPoint - startPoint val vecDifference = endPoint - startPoint
@@ -599,7 +612,7 @@ struct
then then
let let
val str = String.substring (str, 0, startIdx) val str = String.substring (str, 0, startIdx)
val midPoint = binSearch (startIdx, vec, 0, vecLength) val midPoint = binSearch (startIdx, vec, 0, Vector.length vec - 1)
val vec = val vec =
if Vector.length vec = 0 then emptyVec if Vector.length vec = 0 then emptyVec
else Vector.tabulate (midPoint, fn idx => Vector.sub (vec, idx)) else Vector.tabulate (midPoint, fn idx => Vector.sub (vec, idx))
@@ -609,7 +622,7 @@ struct
else else
let let
val str = String.substring (str, endIdx, String.size str - endIdx) val str = String.substring (str, endIdx, String.size str - endIdx)
val midPoint = binSearch (endIdx, vec, 0, vecLength) val midPoint = binSearch (endIdx, vec, 0, Vector.length vec - 1)
val vec = val vec =
if Vector.length vec = 0 then if Vector.length vec = 0 then
emptyVec emptyVec
@@ -642,10 +655,11 @@ struct
val (l, _) = del (startIdx, endIdx, l) val (l, _) = del (startIdx, endIdx, l)
val (r, _) = del (startIdx - lms, endIdx - lms, r) val (r, _) = del (startIdx - lms, endIdx - lms, r)
in in
makeN2 (l, r) (makeN2 (l, r), false)
end end
| N1 t => del (startIdx, endIdx, t) | N1 t => del (startIdx, endIdx, t)
| N0 (str, vec) => delLeaf (startIdx, endIdx, str, vec) | N0 (str, vec) => delLeaf (startIdx, endIdx, str, vec)
| _ => raise AuxConstructor
fun delete (start, length, rope) = fun delete (start, length, rope) =
let val (rope, didIns) = del (start, start + length, rope) let val (rope, didIns) = del (start, start + length, rope)

View File

@@ -305,10 +305,8 @@ struct
else (L2 (sub1, sub2), true) else (L2 (sub1, sub2), true)
end end
else if startIdx >= 0 andalso endIdx >= String.size str then else if startIdx >= 0 andalso endIdx >= String.size str then
let let val str = String.substring (str, 0, startIdx)
val str = String.substring (str, 0, startIdx) in (N0 str, false)
in
(N0 str, false)
end end
else else
let val str = String.substring (str, endIdx, String.size str - endIdx) let val str = String.substring (str, endIdx, String.size str - endIdx)

View File

@@ -1,11 +1,11 @@
fun time_func title f = fun time_func title f =
let let
val title = String.concat ["Starting " , title , "..."] val title = String.concat ["Starting ", title, "..."]
val _ = (print title) val _ = (print title)
val start_time = Time.now() val start_time = Time.now ()
val start_time = Time.toNanoseconds start_time val start_time = Time.toNanoseconds start_time
val x = f() val x = f ()
val end_time = Time.now() val end_time = Time.now ()
val end_time = Time.toNanoseconds end_time val end_time = Time.toNanoseconds end_time
val time_diff = end_time - start_time val time_diff = end_time - start_time
val time_diff = LargeInt.toString time_diff val time_diff = LargeInt.toString time_diff
@@ -18,36 +18,26 @@ fun time_func title f =
fun run_txns arr = fun run_txns arr =
Vector.foldl Vector.foldl
(fn ((pos, del_num, ins_str), rope) => (fn ((pos, del_num, ins_str), rope) =>
let let
val rope = val rope =
if del_num > 0 if del_num > 0 then Rope.delete (pos, del_num, rope) else rope
then TinyRope.delete(pos, del_num, rope) val str_size = String.size ins_str
else rope val rope =
val str_size = String.size ins_str if str_size > 0 then Rope.insert (pos, ins_str, rope) else rope
val rope = in
if str_size > 0 rope
then TinyRope.insert(pos, ins_str, rope) end) Rope.empty arr
else rope
in
rope
end)
TinyRope.empty arr
fun run_txns_time title arr = fun run_txns_time title arr =
let let val f = (fn () => run_txns arr)
val f = (fn () => run_txns arr) in time_func title f
in
time_func title f
end end
fun run_to_string rope = fun run_to_string rope = Rope.toString rope
TinyRope.toString rope
fun run_to_string_time title rope = fun run_to_string_time title rope =
let let val f = (fn () => run_to_string rope)
val f = (fn () => run_to_string rope) in time_func title f
in
time_func title f
end end
fun run_txns_1000_times (counter, arr, total) = fun run_txns_1000_times (counter, arr, total) =
@@ -61,12 +51,12 @@ fun run_txns_1000_times (counter, arr, total) =
end end
else else
let let
val start_time = Time.now() val start_time = Time.now ()
val start_time = Time.toNanoseconds start_time val start_time = Time.toNanoseconds start_time
val _ = run_txns arr val _ = run_txns arr
val end_time = Time.now() val end_time = Time.now ()
val end_time = Time.toNanoseconds end_time val end_time = Time.toNanoseconds end_time
val time_diff = end_time - start_time val time_diff = end_time - start_time
val counter = counter + 1 val counter = counter + 1
@@ -85,16 +75,29 @@ fun write_file filename acc =
() ()
end end
val _ = fun main () =
let let
(* Timing benchmarks. *)
val start_time = LargeInt.fromInt 0 val start_time = LargeInt.fromInt 0
val svelte = run_txns_1000_times (999 ,svelte_arr, start_time ) val _ = run_txns_1000_times (999, svelte_arr, start_time)
val _ = run_txns_1000_times (999, rust_arr, start_time)
val _ = run_txns_1000_times (999, seph_arr, start_time)
val _ = run_txns_1000_times (999, automerge_arr, start_time)
val rust = run_txns_1000_times (999 ,rust_arr, start_time ) (* Tests that line metadata is correct; will fail if incorrect. *)
val svelte = run_txns svelte_arr
val _ = Rope.verifyLines svelte
val seph = run_txns_1000_times (999, seph_arr, start_time) val rust = run_txns rust_arr
val _ = Rope.verifyLines rust
val automerge = run_txns_1000_times (999, automerge_arr , start_time) val seph = run_txns seph_arr
val _ = Rope.verifyLines seph
val automerge = run_txns automerge_arr
val _ = Rope.verifyLines automerge
in in
() ()
end end
val _ = main ()