Explorar el Código

do not clone enum structure when typing the enum statics, instead have a single cached reference (reduce memory usage for big enums) close #2300

Nicolas Cannasse hace 11 años
padre
commit
181bf6a9c0
Se han modificado 3 ficheros con 37 adiciones y 33 borrados
  1. 1 0
      type.ml
  2. 35 2
      typeload.ml
  3. 1 31
      typer.ml

+ 1 - 0
type.ml

@@ -223,6 +223,7 @@ and tenum = {
 	e_pos : Ast.pos;
 	e_private : bool;
 	e_doc : Ast.documentation;
+	e_type : tdef;
 	mutable e_meta : metadata;
 	mutable e_types : type_params;
 

+ 35 - 2
typeload.ml

@@ -76,6 +76,16 @@ let make_module ctx mpath file tdecls loadp =
 				e_extern = List.mem EExtern d.d_flags;
 				e_constrs = PMap.empty;
 				e_names = [];
+				e_type = {
+					t_path = fst path, "#" ^ snd path;
+					t_module = m;
+					t_doc = None;
+					t_pos = p;
+					t_type = mk_mono();
+					t_private = true;
+					t_types = [];
+					t_meta = [];
+				};
 			} in
 			decls := (TEnumDecl e, decl) :: !decls;
 			acc
@@ -2264,6 +2274,7 @@ let rec init_module_type ctx context_init do_init (decl,p) =
 		let names = ref [] in
 		let index = ref 0 in
 		let is_flat = ref true in
+		let fields = ref PMap.empty in
 		List.iter (fun c ->
 			let p = c.ec_pos in
 			let params = ref [] in
@@ -2294,7 +2305,7 @@ let rec init_module_type ctx context_init do_init (decl,p) =
 					) l, rt)
 			) in
 			if PMap.mem c.ec_name e.e_constrs then error ("Duplicate constructor " ^ c.ec_name) p;
-			e.e_constrs <- PMap.add c.ec_name {
+			let f = {
 				ef_name = c.ec_name;
 				ef_type = t;
 				ef_pos = p;
@@ -2302,12 +2313,34 @@ let rec init_module_type ctx context_init do_init (decl,p) =
 				ef_index = !index;
 				ef_params = params;
 				ef_meta = c.ec_meta;
-			} e.e_constrs;
+			} in
+			let cf = {
+				cf_name = f.ef_name;
+				cf_public = true;
+				cf_type = f.ef_type;
+				cf_kind = (match follow f.ef_type with
+					| TFun _ -> Method MethNormal
+					| _ -> Var { v_read = AccNormal; v_write = AccNo }
+				);
+				cf_pos = e.e_pos;
+				cf_doc = None;
+				cf_meta = no_meta;
+				cf_expr = None;
+				cf_params = f.ef_params;
+				cf_overloads = [];
+			} in
+			e.e_constrs <- PMap.add f.ef_name f e.e_constrs;
+			fields := PMap.add cf.cf_name cf !fields;
 			incr index;
 			names := c.ec_name :: !names;
 		) (!constructs);
 		e.e_names <- List.rev !names;
 		e.e_extern <- e.e_extern;
+		e.e_type.t_types <- e.e_types;
+		e.e_type.t_type <- TAnon {
+			a_fields = !fields;
+			a_status = ref (EnumStatics e);
+		};
 		if !is_flat then e.e_meta <- (Meta.FlatEnum,[],e.e_pos) :: e.e_meta;
 	| ETypedef d ->
 		let t = (match get_type d.d_name with TTypeDecl t -> t | _ -> assert false) in

+ 1 - 31
typer.ml

@@ -653,37 +653,7 @@ let rec type_module_type ctx t tparams p =
 		mk (TTypeExpr (TClassDecl c)) (TType (t_tmp,[])) p
 	| TEnumDecl e ->
 		let types = (match tparams with None -> List.map (fun _ -> mk_mono()) e.e_types | Some l -> l) in
-		let fl = PMap.fold (fun f acc ->
-			PMap.add f.ef_name {
-				cf_name = f.ef_name;
-				cf_public = true;
-				cf_type = f.ef_type;
-				cf_kind = (match follow f.ef_type with
-					| TFun _ -> Method MethNormal
-					| _ -> Var { v_read = AccNormal; v_write = AccNo }
-				);
-				cf_pos = e.e_pos;
-				cf_doc = None;
-				cf_meta = no_meta;
-				cf_expr = None;
-				cf_params = f.ef_params;
-				cf_overloads = [];
-			} acc
-		) e.e_constrs PMap.empty in
-		let t_tmp = {
-			t_path = fst e.e_path, "#" ^ snd e.e_path;
-			t_module = e.e_module;
-			t_doc = None;
-			t_pos = e.e_pos;
-			t_type = TAnon {
-				a_fields = fl;
-				a_status = ref (EnumStatics e);
-			};
-			t_private = true;
-			t_types = e.e_types;
-			t_meta = no_meta;
-		} in
-		mk (TTypeExpr (TEnumDecl e)) (TType (t_tmp,types)) p
+		mk (TTypeExpr (TEnumDecl e)) (TType (e.e_type,types)) p
 	| TTypeDecl s ->
 		let t = apply_params s.t_types (List.map (fun _ -> mk_mono()) s.t_types) s.t_type in
 		(match follow t with