「アサート」OCaml 3.12.1 コンストラクト用に生成されたラムダ コードを改善したいと考えています。次に例を示します。
let f x =
assert (x = 4);
assert (2 + x = 6);
assert (x - x = 0);
exit x
上記のファイル longfilename.ml は、ラムダ コード生成を改善してほしい大規模な OCaml モジュールの代表例です。コンパイルすると次のようになります。
$ ocamlopt -S longfilename.ml
$ cat longfilename.s
...
.data
.quad 3072
_camlLongfilename__2:
.quad L100007
.quad 9
.quad 9
.quad 2300
L100007: .L100007:
.ascii "longfilename.ml"
.byte 0
.data
.quad 3072
_camlLongfilename__3:
.quad L100006
.quad 7
.quad 9
.quad 2300
L100006: .L100006:
.ascii "longfilename.ml"
.byte 0
.data
.quad 3072
_camlLongfilename__4:
.quad L100005
.quad 5
.quad 9
.quad 2300
L100005: .L100005:
.ascii "longfilename.ml"
.byte 0
...
上記はひどく冗長です。各アサーションのソース ファイルの名前が重複しています。犯人は bytecomp/translcore.ml のようです:
let assert_failed loc =
(* [Location.get_pos_info] is too expensive *)
let fname = match loc.Location.loc_start.Lexing.pos_fname with
| "" -> !Location.input_name
| x -> x
in
let pos = loc.Location.loc_start in
let line = pos.Lexing.pos_lnum in
let char = pos.Lexing.pos_cnum - pos.Lexing.pos_bol in
Lprim(Praise, [Lprim(Pmakeblock(0, Immutable),
[transl_path Predef.path_assert_failure;
Lconst(Const_block(0,
[Const_base(Const_string fname);
Const_base(Const_int line);
Const_base(Const_int char)]))])])
;;
Const_base(Const_string fname)
一見すると、 に名前を付けて、コンパイル時のハッシュ テーブルに格納して再利用するだけで十分に見え
ます。モジュール内最適化の場合、変更は管理可能かもしれません (ハッシュテーブルが各コンパイル単位でリセットされる限り)。
ここでは、特に「各コンパイル単位でのリセット」の部分で、少し深みがありません。ヒントはありますか?