ocaml-flambda / flambda-backend

The Flambda backend project for OCaml
93 stars 67 forks source link

Stop using exceptions for control flow in `camlinternalOO` #2691

Closed ncik-roberts closed 3 weeks ago

ncik-roberts commented 3 weeks ago

We have some evidence that doing this will speed up uses of the object system in JSOO.

I don't see a compelling reason to use exceptions in ocaml/stdlib/camlinternalOO.ml, and think using options is fine. I don't think this will affect bytecode or native compilation meaningfully. Perhaps this will add some more short-lived allocations for uses of the object system, but I don't think it's meaningfully more, judging from the surrounding code.

lthls commented 3 weeks ago

I'm surprised you can observe any difference. All of these functions are called in parts of the code that are evaluated once per class (not even once per class created at runtime, but once per class in the source code). So if you notice a difference with this PR, it means you're spending significantly more time creating classes than actually using them. I don't mind the PR though, I think options are better than exceptions for these cases anyway.

TyOverby commented 3 weeks ago

@lthls: In applications that are compiled to javascript via Js_of_ocaml, our benchmarks show that throwing and catching an exception can cost as much as 2ms. In a program that had three levels of inheritence on a 50-method class, we saw that over one second was spent throwing and catching exceptions in camlinternalOO.ml at program startup time.

lthls commented 3 weeks ago

@lthls: In applications that are compiled to javascript via Js_of_ocaml, our benchmarks show that throwing and catching an exception can cost as much as 2ms. In a program that had three levels of inheritence on a 50-method class, we saw that over one second was spent throwing and catching exceptions in camlinternalOO.ml at program startup time.

That makes sense, thanks for the context.

chambart commented 3 weeks ago

Have you tried using OCamlClean https://github.com/bvaugon/ocamlclean ? It does partially evaluate bytecode programs. I know that it is supposed to be quite good with class initialisation. It has not been updated recently but you could at least try on your files to see if it reduces the size. Also it might not right away with JSOO.