Optimize some constant string operations#703
Conversation
|
Thanks, @chambart. This seems to help quite a bit. Here's a simple test case, a Ctypes binding for a couple of C functions: module F(F: Cstubs.FOREIGN) =
struct
open F
let succ = foreign "succ" (int @-> returning int)
let sqrt = foreign "sqrt" (double @-> returning double)
endFrom this code, Ctypes generates some C stubs and a module with a external t1_succ : int -> int = "t1_succ"
external t2_sqrt : float -> float = "t2_sqrt"
let foreign : type a b. string -> (a -> b) fn -> (a -> b) =
fun name t -> match t, name with
| Function (Primitive Double, Returns (Primitive Double)), "sqrt" -> t2_sqrt
| Function (Primitive Int, Returns (Primitive Int)), "succ" -> t1_succ
| _, s -> Printf.ksprintf failwith "No match for %s" sand here's the code to link the binding and the generated module: include F(Generated)Since the values passed to (and this is just for the However, the compiler built from this PR optimizes absolutely everything away, including the entire call to (this is for both external t1_succ : int -> int = "t1_succ"
external t2_sqrt : float -> float = "t2_sqrt"
let succ = t1_succ
let sqrt = t2_sqrtSo for simple cases, at least, this PR looks like a significant improvement for Ctypes. |
|
#687 introduces a new configure-time option. I assume the new optimizations should only be triggered when the system has been configured in this mode. |
|
this might be helpful #596 |
|
@yallop Thanks for testing. @alainfrisch this is enabled by your change in Closure_conversion https://github.com/ocaml/ocaml/pull/687/files#diff-cf66b45f8983f3e9e6a793b9d796de3fL129 @bobzhang Not really, this only applies to constant strings that we already know to be immutable here. I don't intend to optimize any complex pattern (when everything is not a constant), so there is no need for more information. If you can produce some interesting use case for some slightly more complex optimization on strings, I would be happy to reconsider that. |
01f8328 to
4482f19
Compare
db19a98 to
158dc94
Compare
This allows removing them when the value of constant strings can be propagated.
158dc94 to
3b6a802
Compare
Optimize some constant string operations
ce76e02 flambda-backend: Bugfix for type_application (ocaml#746) 44f3afb flambda-backend: PR580 for main branch (ocaml#743) b851eaa flambda-backend: Backport first part of ocaml/ocaml PR10498 (ocaml#737) fafb4bd flambda-backend: Fix return mode for eta-expanded function in type_argument (ocaml#735) c31f6c3 flambda-backend: Fix treatment of functions called [@nontail] (ocaml#725) 847781e flambda-backend: Fix build_upstream post-PR703 (ocaml#712) bfcbbf8 flambda-backend: Extend Pblock value kind to handle variants (ocaml#703) b2cab95 flambda-backend: Merge ocaml-jst a6d6e0e flambda-backend: Merge ocaml-jst 88a4f63 flambda-backend: Use Pmakearray for immutable arrays (ocaml#699) eeaa44b flambda-backend: Install an ocamldoc binary (ocaml#695) 48d322b flambda-backend: Ensure that GC is not invoked from bounds check failures (ocaml#681) 4370fa1 flambda-backend: Review changes of term directory (ocaml#602) 65a4566 flambda-backend: Add code coverage using bisect_ppx (ocaml#352) 63ab65f flambda-backend: Bugfix for primitive inclusion (ocaml#662) 7e3e0c8 flambda-backend: Fix inclusion checks for primitives (ocaml#661) 96c68f9 flambda-backend: Speed up linking by changing cmxa format (ocaml#607) 1829150 flambda-backend: Bugfix for Translmod.all_idents (ocaml#659) git-subtree-dir: ocaml git-subtree-split: ce76e02
* Ood: move sorting * Ood: sort reverse instead of rev after sort Co-authored-by: Cuihtlauac ALVARADO <cuihtmlauac@tarides.com>
Since #687 some constant string operations can be simplified.
This currently propose eliminating string switch when the argument is a constant string ( @yallop thinks that this can matter for Ctypes ). This also tweaks a few things to improve char lookup.
Notice that this kind of change makes using
Bytes.unsafe_to_stringmore unsafe.I can add various other cases (
Pervasives.(^), String.equal, String.compare, int_of_string, string_of_int, ...) if someone consider that useful .