From: Christian Lindig <christian.lindig@citrix.com>
To: Edwin Torok <edvin.torok@citrix.com>,
"xen-devel@lists.xenproject.org" <xen-devel@lists.xenproject.org>
Cc: David Scott <dave@recoil.org>,
Ian Jackson <Ian.Jackson@citrix.com>, "Wei Liu" <wl@xen.org>
Subject: Re: [PATCH v4 2/4] Map: backport find_opt/update from 4.06
Date: Fri, 28 Aug 2020 08:30:15 +0000 [thread overview]
Message-ID: <1598603415021.35327@citrix.com> (raw)
In-Reply-To: <72b1f39ce900870819630cc7ba5bcb1f6610de77.1598548188.git.edvin.torok@citrix.com>
________________________________________
From: Edwin Török <edvin.torok@citrix.com>
Sent: 27 August 2020 18:35
To: xen-devel@lists.xenproject.org
Cc: Edwin Torok; Christian Lindig; David Scott; Ian Jackson; Wei Liu
Subject: [PATCH v4 2/4] Map: backport find_opt/update from 4.06
We are currently on OCaml 4.02 as minimum version.
To make the followup optimizations compile backport these functions from
OCaml 4.06.
This implementation is less efficient than the one in the 4.06 standard
library which has access to the internals of the Map.
Signed-off-by: Edwin Török <edvin.torok@citrix.com>
---
Changes since V3:
* this patch is new in V4
---
tools/ocaml/xenstored/stdext.ml | 21 +++++++++++++++++++++
tools/ocaml/xenstored/trie.ml | 2 ++
2 files changed, 23 insertions(+)
diff --git a/tools/ocaml/xenstored/stdext.ml b/tools/ocaml/xenstored/stdext.ml
index 4f2f3a2c8c..5bebe2aa27 100644
--- a/tools/ocaml/xenstored/stdext.ml
+++ b/tools/ocaml/xenstored/stdext.ml
@@ -44,6 +44,27 @@ let default d v =
let maybe f v =
match v with None -> () | Some x -> f x
+module Map = struct
+module Make(Ord: Map.OrderedType) = struct
+
+include Map.Make(Ord)
+
+let find_opt k t =
+ (* avoid raising exceptions, they can be expensive *)
+ if mem k t then Some (find k t) else None
I disagree with this argument. Exceptions in OCaml are cheap because they don't walk the stack and cut to the exception handler directly. Is there a reason why they could be expensive here? In any case, the code is correct.
+
+let update k f t =
+ let r = find_opt k t in
+ let r' = f r in
+ match r, r' with
+ | None, None -> t
+ | Some _, None -> remove k t
+ | Some r, Some r' when r == r' -> t
+ | _, Some r' -> add k r' t
This looks correct to me.
+
+end
+end
+
module String = struct include String
let of_char c = String.make 1 c
diff --git a/tools/ocaml/xenstored/trie.ml b/tools/ocaml/xenstored/trie.ml
index dc42535092..f513f4e608 100644
--- a/tools/ocaml/xenstored/trie.ml
+++ b/tools/ocaml/xenstored/trie.ml
@@ -13,6 +13,8 @@
* GNU Lesser General Public License for more details.
*)
+open Stdext
+
module Node =
struct
type ('a,'b) t = {
--
2.25.1
--
Acked-by: Christian Lindig <christian.lindig@citrix.com>
next prev parent reply other threads:[~2020-08-28 8:30 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-08-27 17:35 [PATCH v4 0/4] tools/ocaml/xenstored: simplify code Edwin Török
2020-08-27 17:35 ` [PATCH v4 1/4] tools/ocaml/xenstored: replace hand rolled GC with weak GC references Edwin Török
2020-08-27 17:35 ` [PATCH v4 2/4] Map: backport find_opt/update from 4.06 Edwin Török
2020-08-28 8:30 ` Christian Lindig [this message]
2020-08-28 17:43 ` Edwin Torok
2020-09-01 12:39 ` Christian Lindig
2020-08-27 17:35 ` [PATCH v4 3/4] tools/ocaml/xenstored: use more efficient node trees Edwin Török
2020-08-27 17:35 ` [PATCH v4 4/4] tools/ocaml/xenstored: use more efficient tries Edwin Török
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1598603415021.35327@citrix.com \
--to=christian.lindig@citrix.com \
--cc=Ian.Jackson@citrix.com \
--cc=dave@recoil.org \
--cc=edvin.torok@citrix.com \
--cc=wl@xen.org \
--cc=xen-devel@lists.xenproject.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.