Skip to content

Commit eb41476

Browse files
committed
Optimization: Reduce traversals in generation
1 parent ced1b84 commit eb41476

File tree

20 files changed

+331
-137
lines changed

20 files changed

+331
-137
lines changed

src/cpp.ml

Lines changed: 278 additions & 57 deletions
Large diffs are not rendered by default.

src/translation.ml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2450,6 +2450,45 @@ let gen_dfuns_spec (ns,bs,tys) =
24502450
[(decl_to_spec ds, empty_env ())]
24512451
) (List.mapi (fun i name -> (i, name)) (Array.to_list ns))
24522452

2453+
(* Generate both spec and def for a group of mutually recursive functions in one pass.
2454+
Calls gen_decl_for_dfuns ONCE per function, then derives:
2455+
- spec: decl_to_spec of the full definition (forward declaration)
2456+
- def: the full definition (for templates) or None (for non-templates in header mode)
2457+
Returns list of (spec, def_option, lifted_decls) *)
2458+
let gen_dfuns_dual ~is_header (ns,bs,tys) =
2459+
List.concat_map (fun (i, name) ->
2460+
let (ds, env, tvars) = gen_decl_for_dfuns name bs.(i) tys.(i) in
2461+
let lifted = take_lifted_decls () in
2462+
let spec = (decl_to_spec ds, env) in
2463+
let def = match tvars, is_header with
2464+
| _::_, true -> Some (ds, env) (* Template + header: full def in .h *)
2465+
| _::_, false -> None (* Template + source: already in .h *)
2466+
| [], true -> None (* Non-template + header: def goes in .cpp *)
2467+
| [], false -> Some (ds, env) (* Non-template + source: full def in .cpp *)
2468+
in
2469+
[(spec, def, lifted)]
2470+
) (List.mapi (fun i name -> (i, name)) (Array.to_list ns))
2471+
2472+
(* Generate both spec and def for a single Dterm function in one pass.
2473+
Calls gen_decl_for_pp ONCE, then derives both spec and def.
2474+
Returns (spec_opt, def_opt, tvars) *)
2475+
let gen_decl_for_pp_dual ~is_header n b ty =
2476+
let (ds_opt, env, tvars) = gen_decl_for_pp n b ty in
2477+
match ds_opt, tvars with
2478+
| Some ds, _::_ ->
2479+
(* Template function: spec is decl_to_spec, def only in header *)
2480+
let def = if is_header then Some (ds, env) else None in
2481+
(Some (decl_to_spec ds, env), def, tvars)
2482+
| Some ds, [] ->
2483+
(* Non-template function: spec via gen_spec, def only in source mode *)
2484+
let (spec_ds, spec_env) = gen_spec n b ty in
2485+
let def = if is_header then None else Some (ds, env) in
2486+
(Some (spec_ds, spec_env), def, tvars)
2487+
| None, _ ->
2488+
(* Non-function type: no def needed *)
2489+
let (spec_ds, spec_env) = gen_spec n b ty in
2490+
(Some (spec_ds, spec_env), None, tvars)
2491+
24532492
let gen_ind_header vars name cnames tys =
24542493
let templates = List.map (fun n -> (TTtypename, n)) vars in
24552494
let add_templates d = match templates with

src/translation.mli

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ val gen_dfuns_header : GlobRef.t array * ml_ast array * ml_type array -> (cpp_de
7272
this always derives specs from gen_decl_for_dfuns for signature consistency. *)
7373
val gen_dfuns_spec : GlobRef.t array * ml_ast array * ml_type array -> (cpp_decl * env) list
7474

75+
(** Generate both spec and def for a group of mutually recursive functions in one pass.
76+
Calls gen_decl_for_dfuns ONCE per function, then derives both spec and def.
77+
Returns list of (spec, def_option, lifted_decls). *)
78+
val gen_dfuns_dual : is_header:bool ->
79+
GlobRef.t array * ml_ast array * ml_type array ->
80+
((cpp_decl * env) * (cpp_decl * env) option * cpp_decl list) list
81+
82+
(** Generate both spec and def for a single Dterm function in one pass.
83+
Calls gen_decl_for_pp ONCE, then derives both spec and def.
84+
Returns (spec_opt, def_opt, tvars). *)
85+
val gen_decl_for_pp_dual : is_header:bool ->
86+
GlobRef.t -> ml_ast -> ml_type ->
87+
(cpp_decl * env) option * (cpp_decl * env) option * variable list
88+
7589
(** Convert a definition (Dfundef) to a declaration (Dfundecl) by stripping the body. *)
7690
val decl_to_spec : cpp_decl -> cpp_decl
7791

tests/basics/module/module.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,5 +217,3 @@ const NatMap::t mymap = NatMap::add(
217217
1) +
218218
1),
219219
NatMap::empty)));
220-
221-
comparison compare(const unsigned int n, const unsigned int m);

tests/basics/top/top.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -533,9 +533,6 @@ ListDef::map(F0 &&f, const std::shared_ptr<List::list<T1>> &l) {
533533
l->v());
534534
}
535535

536-
std::shared_ptr<List::list<unsigned int>> seq(const unsigned int start,
537-
const unsigned int len);
538-
539536
template <typename T1>
540537
std::shared_ptr<List::list<T1>> List::concat(
541538
const std::shared_ptr<List::list<std::shared_ptr<List::list<T1>>>> &l) {

tests/basics/top_bde/top_bde.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -699,9 +699,6 @@ bsl::shared_ptr<List::list<T2> > ListDef::map(
699699
l->v());
700700
}
701701

702-
bsl::shared_ptr<List::list<unsigned int> > seq(const unsigned int start,
703-
const unsigned int len);
704-
705702
template <typename T1>
706703
bsl::shared_ptr<List::list<T1> > List::concat(
707704
const bsl::shared_ptr<List::list<bsl::shared_ptr<List::list<T1> > > >& l)

tests/basics/tree/tree.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,3 @@ struct Tree {
317317
Nat::nat::ctor::S_(Nat::nat::ctor::S_(Nat::nat::ctor::O_())),
318318
tree<std::shared_ptr<Nat::nat>>::ctor::leaf_())));
319319
};
320-
321-
std::shared_ptr<Nat::nat> add(const std::shared_ptr<Nat::nat> &n,
322-
std::shared_ptr<Nat::nat> m);
323-
324-
std::shared_ptr<Nat::nat> max(std::shared_ptr<Nat::nat> n,
325-
std::shared_ptr<Nat::nat> m);

tests/regression/nested_mod/nested_mod.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,3 @@ struct NestedMod {
182182

183183
static inline const unsigned int test_color = Outer::color_code(my_color);
184184
};
185-
186-
std::pair<unsigned int, unsigned int> divmod(const unsigned int x,
187-
const unsigned int y,
188-
const unsigned int q,
189-
const unsigned int u);
190-
191-
unsigned int div(const unsigned int x, const unsigned int y);

tests/regression/polymorphic_helper/polymorphic_helper.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,6 @@ std::shared_ptr<Nat::nat> _foo_aux(const T1 a,
118118
return ListDef::repeat<T1>(a, n)->length();
119119
}
120120

121-
std::shared_ptr<Nat::nat> add(const std::shared_ptr<Nat::nat> &n,
122-
std::shared_ptr<Nat::nat> m);
123-
124121
template <typename T1>
125122
std::shared_ptr<List::list<T1>>
126123
ListDef::repeat(const T1 x, const std::shared_ptr<Nat::nat> &n) {

tests/wip/canon_struct/canon_struct.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,3 @@ struct CanonStruct {
6969

7070
static inline const bool test_bool = same(bool_eqType, true, false);
7171
};
72-
73-
bool eqb(const bool b1, const bool b2);

0 commit comments

Comments
 (0)