Skip to content

Commit 6bcf3c4

Browse files
committed
optimizes the merge function for OGRE documents
The new implementation will untouch the document if the other one is an empty document or is the same. When the documents are different and non-trivial we will take the larger one and update it with the smaller one, which is also faster. At the end, it makes merge more than 10 times faster under certain scenarios.
1 parent 4eb72ae commit 6bcf3c4

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

lib/ogre/ogre.ml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ module Doc = struct
232232
else (packed :: data)) in
233233
{ doc with entries }
234234

235-
let merge doc {scheme; entries} =
235+
let do_merge doc {scheme; entries} =
236236
Map.to_sequence scheme |>
237237
Error.Seq.fold ~init:doc ~f:(fun doc (name,sign) ->
238238
update_scheme name sign doc) >>= fun doc ->
@@ -241,6 +241,14 @@ module Doc = struct
241241
Error.List.fold values ~init:doc ~f:(fun doc value ->
242242
update_entries name value doc))
243243

244+
let merge d1 d2 = match is_empty d1, is_empty d2 with
245+
| true,true -> Ok d1
246+
| true,false -> Ok d2
247+
| false,true -> Ok d1
248+
| false,false -> match compare d1 d2 with
249+
| 0 -> Ok d1
250+
| 1 -> do_merge d1 d2
251+
| _ -> do_merge d2 d1
244252

245253
let put k attr =
246254
let {Attribute.name; save; sign} = attr () in

0 commit comments

Comments
 (0)