Skip to content

Commit

Permalink
xenopsd: Optimize lazy evaluation
Browse files Browse the repository at this point in the history
The manual notes that `Lazy.from_fun` "should only be used if the function f
is already defined. In particular it is always less efficient to write
`from_fun (fun () -> expr)` than `lazy expr`.

So, replace `Lazy.from_fun` with `lazy` in this particular case

Compare the lambda dump for `lazy (fun () -> [| 1;2;3 |] |> Array.map (fun x -> x+1))`:
```
(seq
  (let
    (l/269 =
       (function param/319[int]
         (apply (field_imm 12 (global Stdlib__Array!))
           (function x/318[int] : int (+ x/318 1)) (makearray[int] 1 2 3))))
    (setfield_ptr(root-init) 0 (global Main!) l/269))
  0)
```

with the lambda dump of the `let x = Lazy.from_fun (fun () -> [| 1;2;3 |] |>
Array.map (fun x -> x+1))`:
```
(seq
  (let
    (x/269 =
       (apply (field_imm 5 (global Stdlib__Lazy!))
         (function param/332[int]
           (apply (field_imm 12 (global Stdlib__Array!))
             (function x/331[int] : int (+ x/331 1)) (makearray[int] 1 2 3)))))
    (setfield_ptr(root-init) 0 (global Main!) x/269))
  0)
```

See: https://patricoferris.github.io/js_of_ocamlopt/#code=bGV0IGwgPSBsYXp5IChmdW4gKCkgLT4gW3wgMTsyOzMgfF0gfD4gQXJyYXkubWFwIChmdW4geCAtPiB4KzEpKQoKbGV0IHggPSBMYXp5LmZyb21fZnVuIChmdW4gKCkgLT4gW3wgMTsyOzMgfF0gfD4gQXJyYXkubWFwIChmdW4geCAtPiB4KzEpKQ%3D%3D

Signed-off-by: Andrii Sultanov <andrii.sultanov@cloud.com>
  • Loading branch information
last-genius committed Nov 22, 2024
1 parent f0c9b4c commit aaabb6c
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions ocaml/xenopsd/xc/domain.ml
Original file line number Diff line number Diff line change
Expand Up @@ -835,12 +835,12 @@ let create_channels ~xc uuid domid =
let numa_hierarchy =
let open Xenctrlext in
let open Topology in
Lazy.from_fun (fun () ->
let xcext = get_handle () in
let distances = (numainfo xcext).distances in
let cpu_to_node = cputopoinfo xcext |> Array.map (fun t -> t.node) in
NUMA.make ~distances ~cpu_to_node
)
lazy
(let xcext = get_handle () in
let distances = (numainfo xcext).distances in
let cpu_to_node = cputopoinfo xcext |> Array.map (fun t -> t.node) in
NUMA.make ~distances ~cpu_to_node
)

let numa_mutex = Mutex.create ()

Expand Down

0 comments on commit aaabb6c

Please sign in to comment.