Created
September 18, 2025 19:35
-
-
Save stedolan/77ac8b671a5e1fd6885b0eb0d6891118 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let enable_hugepages () = | |
let ch = open_in "/proc/self/maps" in | |
let module Syscall = struct | |
external madvise : (int64[@unboxed]) -> (int64[@unboxed]) -> (int[@untagged]) -> (int[@untagged]) = | |
"unimplemented" "madvise" | |
end in | |
try | |
while true do | |
Scanf.sscanf (input_line ch) "%Lx-%Lx %s %Lx %x:%x %Ld %s" | |
(fun start_addr end_addr perms offset devmaj devmin inode path -> | |
let len = Int64.sub end_addr start_addr in | |
if perms = "rw-p" && inode = 0L && path <> "[heap]" && path <> "[stack]" && len >= 0x200000L then | |
ignore (Syscall.madvise start_addr len 25)) | |
done | |
with End_of_file -> close_in ch | |
type t = { | |
n : int; | |
mutable opt : int option | |
} | |
let bench arr = | |
let before = Sys.time () in | |
let iters = 50_000 in | |
for i = 1 to iters do | |
let sum = ref 0 in | |
for n = 0 to Array.length arr - 1 do | |
sum := !sum + (Array.unsafe_get arr n).n; | |
done; | |
ignore (Sys.opaque_identity !sum); | |
done; | |
let after = Sys.time () in | |
Printf.printf " %.3f s (%.1f ns/elem)\n%!" | |
(after -. before) | |
(1e9 *. ((after -. before) /. float_of_int (iters * Array.length arr))) | |
let () = | |
let opts = Array.init 10_000_000 (fun n -> {n; opt=Some n}) in | |
Gc.minor (); | |
let subset = Array.init 10_000 (fun n -> opts.(n*1000)) in | |
Gc.minor (); | |
Printf.printf "After initialisation and minor GC:\n%!"; | |
for _ = 1 to 3 do bench subset done; | |
enable_hugepages (); | |
Printf.printf "After MADV_COLLAPSE to enable hugepages:\n%!"; | |
for _ = 1 to 3 do bench subset done; | |
(* Clear the options t.opt, making some space in the size-1 pool for compaction *) | |
opts |> Array.iter (fun t -> t.opt <- None); | |
Gc.compact (); | |
Printf.printf "After compaction:\n%!"; | |
for _ = 1 to 3 do bench subset done; | |
ignore (Sys.opaque_identity (opts, subset)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment