Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
[patch] add primitives for directly reading 2, 4 or 8 bytes in strings and char bigarrays #5771
Original bug ID: 5771
This patch provides primitives to improve speed of code reading a lot of values from network/files. When those are implemented using C stubs, quite a lot of time is spent in the calls.
the provided primitives are:
type bigstring = (char, int8_unsigned_elt, c_layout) Array1.t
external caml_bigstring_get_16 : bigstring -> int -> int = "%caml_bigstring_get16"
external caml_bigstring_set_16 : bigstring -> int -> int -> unit = "%caml_bigstring_set16"
and the equivalent ones on strings.
external caml_string_get_16 : string -> int -> int = "%caml_string_get16"
external caml_string_set_16 : string -> int -> int -> unit = "%caml_string_set16"
Unsafe versions of the primitives also exist.
Those primitives allow loading values that are not alligned.
It is allowed on x86 and x86-64, but for the other architectures I made the safe guess that it is forbiden. Is it effectively the case on power processors ?
On a future version of the patch I can implement a more efficient unaligned load that requires only 2 loads and some shifts.
Comment author: @lefessan
The patch has been applied a long time ago. The library ocplib-endian uses these primitives when they are available, and non optimized primitives otherwise (while still using as little allocations as possible) in both cases.