-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Safer handling of aligned buffers? #38
Comments
Why is
http://caml.inria.fr/pub/docs/manual-ocaml/libref/Bigarray.Genarray.html ( |
|
I was also very curious about this claim, as I believed let () = Random.self_init ()
let time ~tag f =
let t1 = Sys.time () in
let r = f () in
let t2 = Sys.time () in
Printf.printf "[time] %s %.04f sec\n%!" tag (t2 -. t1) ;
r
let size = 100000
and n = 10000000
let rix () = Random.int size
let () =
time ~tag:"rng" @@ fun () ->
for i = 1 to n do ignore @@ rix() done
let () =
let open Bigarray in
let arr = Array1.create char c_layout size in
time ~tag:"bigarray" @@ fun () ->
for i = 1 to n do ignore @@ Array1.sub arr (rix()) 1 done
let () =
let cs = Cstruct.create size in
time ~tag:"cstruct" @@ fun () ->
for i = 1 to n do ignore @@ Cstruct.sub cs (rix()) 1 done Taking the average of 10 runs and subtracting rng time from the running time:
Replacing the random index with
It is, apparently. It's around 15-30 times slower. I suspect this might have something to do with new array wrappers never being allocated on the minor heap, as they have a finalizer (as per But lesson learned. Will touch |
thanks for confirming!
|
An alignment improvement is in #87: namely an assert-style function |
OCaml (either trunk or 4.03, can't remember which) can allocate |
In Mirage's low-level I/O code we often require buffers to be aligned e.g.
At the moment we use the [mirage/io-page] library to allocate page-aligned bigarrays (which being page-aligned satisfy both requirements above). Other libraries accept ad-hoc mixtures of
Io_page.t
andCstruct.t
which leads to inefficiency [mirage/mirage-platform#110], bugs and general confusion. We tend to eitherIo_page.t
to spot bugs where unaligned buffers are accidentally produced; but then we end up calling functions likeIo_page.to_pages
which callsBigarray.Array1.sub
which is much slower than a plainCstruct.sub
; orCstruct.t
for convenience but then we lose track of our alignment and accidentally submit an badly-aligned buffer and screw up an I/O. Sometimes we remember to check the alignment and stop with a fatal error, other times we forget and then the wrong buffer fragment is used (!)Can we make
Cstruct.t
a bit more alignment-aware? Perhaps if we had a phantom type (see #14) we could make our low-level functions accept something like a[>
Page_aligned ] Cstruct.t` and perhaps functions liketo_pages: [>
Page_aligned ] Cstruct.t -> [>Page_aligned ] Cstruct.t list
align: 'a Cstruct.t -> [
Page_aligned ] Cstruct.t(where the returned buffer would be
Cstruct.sub`ed from the original to make it aligned)The text was updated successfully, but these errors were encountered: