Skip to content

Commit

Permalink
Merge pull request #5218 from psafont/private/paus/ref
Browse files Browse the repository at this point in the history
xapi-types/ref: optimize of_string for real references
  • Loading branch information
lindig authored Nov 2, 2023
2 parents e64f953 + ead4129 commit 412ac00
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
23 changes: 13 additions & 10 deletions ocaml/xapi-types/ref.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type 'a t =

let ref_prefix = "OpaqueRef:"

let ref_prefix_length = String.length ref_prefix

let dummy_prefix = "DummyRef:"

let dummy_sep = "|"
Expand Down Expand Up @@ -89,17 +91,16 @@ let short_string_of = function
let of_string x =
if x = ref_null then
Null
else if String.starts_with ~prefix:ref_prefix x then
Real String.(sub x ref_prefix_length (length x - ref_prefix_length))
else if String.starts_with ~prefix:dummy_prefix x then
match Astring.String.cuts ~sep:dummy_sep x with
| _prefix :: uuid :: name ->
Dummy (uuid, String.concat dummy_sep name)
| _ ->
Other x
else
match Astring.String.cut ~sep:ref_prefix x with
| Some ("", uuid) ->
Real uuid
| _ -> (
match Astring.String.cuts ~sep:dummy_sep x with
| prefix :: uuid :: name when prefix = dummy_prefix ->
Dummy (uuid, String.concat dummy_sep name)
| _ ->
Other x
)
Other x

let to_option = function Null -> None | ref -> Some ref

Expand Down Expand Up @@ -135,3 +136,5 @@ let really_pretty_and_small x =
"O:" ^ Astring.String.with_range ~len:12 x
| Null ->
"NULL"

let pp ppf x = Format.fprintf ppf "%s" (string_of x)
2 changes: 2 additions & 0 deletions ocaml/xapi-types/ref.mli
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ val is_dummy : 'a t -> bool
val name_of_dummy : 'a t -> string

val really_pretty_and_small : 'a t -> string

val pp : Format.formatter -> 'a t -> unit

0 comments on commit 412ac00

Please sign in to comment.