From bd698f632538fa8ba071f19d0600e88fb7613863 Mon Sep 17 00:00:00 2001 From: Ming Lu Date: Mon, 7 Jul 2025 09:43:48 +0800 Subject: [PATCH 1/2] CP-308690: Avoid unnecessary condition evaluations The "" operator was introduced to simplify deeply nested conditionals. However, it also led to unintended side effects: all condition expressions are evaluated eagerly, even when some may not be needed. This happens because OCaml uses call-by-value semantics, meaning function arguments are evaluated before the function is applied. The change makes the evaluations lazily to avoid unnecessary condition evaluations. Signed-off-by: Ming Lu --- ocaml/networkd/lib/network_device_order.ml | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/ocaml/networkd/lib/network_device_order.ml b/ocaml/networkd/lib/network_device_order.ml index 3231633304..c543dee5a6 100644 --- a/ocaml/networkd/lib/network_device_order.ml +++ b/ocaml/networkd/lib/network_device_order.ml @@ -65,11 +65,12 @@ module Pciaddr = struct let compare t1 t2 = let open Xcp_pci in - let ( ) a b = if a = 0 then b else a in - compare t1.domain t2.domain - compare t1.bus t2.bus - compare t1.dev t2.dev - compare t1.fn t2.fn + let ( ) a b = match a () with 0 -> b | x -> Fun.const x in + let cmp1 () = compare t1.domain t2.domain in + let cmp2 () = compare t1.bus t2.bus in + let cmp3 () = compare t1.dev t2.dev in + let cmp4 () = compare t1.fn t2.fn in + (cmp1 cmp2 cmp3 cmp4) () end module Macaddr = struct From beac20c47eb8879c2bcb65e671b044a05c87592f Mon Sep 17 00:00:00 2001 From: Ming Lu Date: Mon, 7 Jul 2025 15:10:22 +0800 Subject: [PATCH 2/2] fixup! CP-308690: Avoid unnecessary condition evaluations Signed-off-by: Ming Lu --- ocaml/networkd/lib/network_device_order.ml | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/ocaml/networkd/lib/network_device_order.ml b/ocaml/networkd/lib/network_device_order.ml index c543dee5a6..da056cae84 100644 --- a/ocaml/networkd/lib/network_device_order.ml +++ b/ocaml/networkd/lib/network_device_order.ml @@ -65,12 +65,11 @@ module Pciaddr = struct let compare t1 t2 = let open Xcp_pci in - let ( ) a b = match a () with 0 -> b | x -> Fun.const x in - let cmp1 () = compare t1.domain t2.domain in - let cmp2 () = compare t1.bus t2.bus in - let cmp3 () = compare t1.dev t2.dev in - let cmp4 () = compare t1.fn t2.fn in - (cmp1 cmp2 cmp3 cmp4) () + let ( ) a b = if a = 0 then Lazy.force b else a in + compare t1.domain t2.domain + lazy (compare t1.bus t2.bus) + lazy (compare t1.dev t2.dev) + lazy (compare t1.fn t2.fn) end module Macaddr = struct