Build a project without Stdlib

I decided to experiment with compiling a project without the standard library. Why? I don’t know. But I could save ~50K. Just sharing my note about it.

You can find an example in my repository.

I found the -nostdlib and -nopervasives (undocumented) flags and after a lot of trying I was able to do what I wanted. It doesn’t disable absolutely everything (lists and other types like option are available).

(flags
  :standard
  -nostdlib
  -nopervasives
  ; add runtime
  -cclib
  -lasmrun
  -ccopt
  "-L %{ocaml_where}"
  -ccopt
  "-lm -ldl")
(* stdlib.ml *)
external print_endline : string -> unit = "caml_print_endline" [@@noalloc]

(* main.ml *)
open Stdlib
let () = print_endline "hello from my stdlib"

Hello World program:

with Stdlib without Stdlib
size 349K 302K
6 Likes

Does it give an improvement for js_of_ocaml programs ?

I don’t know :man_shrugging: I don’t think we should use it.

I am asking because size is a very sensitive property of js_of_ocaml programs, but they have dead-code elimination, so it might not make a difference. The dead-code elimination is not perfect though, so we have to try to find out.