Bladeren bron

[hxb] POC new strategy for display dependencies

Rudy Ges 11 maanden geleden
bovenliggende
commit
84cf5c4f40
4 gewijzigde bestanden met toevoegingen van 35 en 30 verwijderingen
  1. 13 3
      src/compiler/compilationCache.ml
  2. 1 0
      src/compiler/hxb/hxbData.ml
  3. 19 25
      src/compiler/hxb/hxbWriter.ml
  4. 2 2
      src/compiler/server.ml

+ 13 - 3
src/compiler/compilationCache.ml

@@ -88,12 +88,22 @@ class context_cache (index : int) (sign : Digest.t) = object(self)
 		| _ ->
 			let writer = HxbWriter.create config (Some string_pool) warn anon_identification in
 			HxbWriter.write_module writer m;
-			let chunks = HxbWriter.get_chunks writer in
+
+			(* TODO: avoid running the whole writer again... *)
+			let anon_identification = new Tanon_identification.tanon_identification in
+			let min_writer = HxbWriter.create config None warn anon_identification in
+			min_writer.minimal <- true;
+			HxbWriter.write_module min_writer m;
+
 			Hashtbl.replace binary_cache path {
 				mc_path = path;
 				mc_id = m.m_id;
-				mc_chunks = chunks;
-				mc_extra = { m.m_extra with m_cache_state = MSGood }
+				mc_chunks = HxbWriter.get_chunks writer;
+				mc_min_chunks = HxbWriter.get_chunks min_writer;
+				mc_extra = { m.m_extra with
+					m_cache_state = MSGood;
+					m_sig_deps = Some (HxbWriter.get_dependencies min_writer)
+				}
 			}
 
 	method cache_module_in_memory path m =

+ 1 - 0
src/compiler/hxb/hxbData.ml

@@ -60,6 +60,7 @@ type module_cache = {
 	mc_path : path;
 	mc_id : int;
 	mc_chunks : cached_chunks;
+	mc_min_chunks : cached_chunks;
 	mc_extra : module_def_extra;
 }
 

+ 19 - 25
src/compiler/hxb/hxbWriter.ml

@@ -415,8 +415,8 @@ type hxb_writer = {
 	docs : StringPool.t;
 	mutable chunk : Chunk.t;
 
-	mutable in_expr : bool;
-	mutable sig_deps : module_def list;
+	mutable minimal : bool;
+	mutable deps : module_def list;
 
 	classes : (path,tclass) Pool.t;
 	enums : (path,tenum) Pool.t;
@@ -870,8 +870,8 @@ module HxbWriter = struct
 	(* References *)
 
 	let maybe_add_sig_dep writer m =
-		if not writer.in_expr && m.m_path <> writer.current_module.m_path && not (List.exists (fun m' -> m'.m_path = m.m_path) writer.sig_deps) then
-			writer.sig_deps <- m :: writer.sig_deps
+		if writer.minimal && m.m_path <> writer.current_module.m_path && not (List.exists (fun m' -> m'.m_path = m.m_path) writer.deps) then
+			writer.deps <- m :: writer.deps
 
 	let write_class_ref writer (c : tclass) =
 		let i = Pool.get_or_add writer.classes c.cl_path c in
@@ -1795,27 +1795,24 @@ module HxbWriter = struct
 		write_metadata writer cf.cf_meta;
 		write_field_kind writer cf.cf_kind;
 		let expr_chunk = match cf.cf_expr with
+			| _ when writer.minimal ->
+				Chunk.write_u8 writer.chunk 0;
+				None
 			| None ->
 				Chunk.write_u8 writer.chunk 0;
 				None
 			| Some e when not write_expr_immediately ->
 				Chunk.write_u8 writer.chunk 2;
 				let fctx,close = start_texpr writer e.epos in
-				let old = writer.in_expr in
-				writer.in_expr <- true;
 				write_texpr writer fctx e;
 				Chunk.write_option writer.chunk cf.cf_expr_unoptimized (write_texpr writer fctx);
-				writer.in_expr <- old;
 				let expr_chunk = close() in
 				Some expr_chunk
 			| Some e ->
 				Chunk.write_u8 writer.chunk 1;
 				let fctx,close = start_texpr writer e.epos in
-				let old = writer.in_expr in
-				writer.in_expr <- true;
 				write_texpr writer fctx e;
 				Chunk.write_option writer.chunk cf.cf_expr_unoptimized (write_texpr writer fctx);
-				writer.in_expr <- old;
 				let expr_pre_chunk,expr_chunk = close() in
 				Chunk.export_data expr_pre_chunk writer.chunk;
 				Chunk.export_data expr_chunk writer.chunk;
@@ -2263,19 +2260,6 @@ module HxbWriter = struct
 			end;
 		end;
 
-		(* Note: this is only a start, and is still including a lot of dependencies *)
-		(* that are not actually needed for signature only. *)
-		let sig_deps = ref PMap.empty in
-		List.iter (fun mdep ->
-			let dep = {md_sign = mdep.m_extra.m_sign; md_path = mdep.m_path; md_kind = mdep.m_extra.m_kind; md_origin = MDepFromTyping} in
-			sig_deps := PMap.add mdep.m_id dep !sig_deps;
-		) writer.sig_deps;
-		PMap.iter (fun id mdep -> match mdep.md_kind, mdep.md_origin with
-			| (MCode | MExtern), MDepFromMacro when mdep.md_sign = m.m_extra.m_sign -> sig_deps := PMap.add id mdep !sig_deps;
-			| _ -> ()
-		) m.m_extra.m_deps;
-		m.m_extra.m_sig_deps <- Some !sig_deps;
-
 		start_chunk writer EOT;
 		start_chunk writer EOF;
 		start_chunk writer EOM;
@@ -2298,6 +2282,16 @@ module HxbWriter = struct
 		l
 end
 
+let get_dependencies writer =
+	let deps = ref PMap.empty in
+
+	List.iter (fun mdep ->
+		let dep = {md_sign = mdep.m_extra.m_sign; md_path = mdep.m_path; md_kind = mdep.m_extra.m_kind; md_origin = MDepFromTyping} in
+		deps := PMap.add mdep.m_id dep !deps;
+	) writer.deps;
+
+	!deps
+
 let create config string_pool warn anon_id =
 	let cp,has_own_string_pool = match string_pool with
 		| None ->
@@ -2313,8 +2307,8 @@ let create config string_pool warn anon_id =
 		chunks = DynArray.create ();
 		cp = cp;
 		has_own_string_pool;
-		sig_deps = [];
-		in_expr = false;
+		deps = [];
+		minimal = false;
 		docs = StringPool.create ();
 		chunk = Obj.magic ();
 		classes = Pool.create ();

+ 2 - 2
src/compiler/server.ml

@@ -452,7 +452,7 @@ class hxb_reader_api_server
 				t_hxb();
 				r
 			in
-			let m,chunks = f_next mc.mc_chunks EOT in
+			let m,chunks = f_next (if full_restore then mc.mc_chunks else mc.mc_min_chunks) EOT in
 
 			(* We try to avoid reading expressions as much as possible, so we only do this for
 				 our current display file if we're in display mode. *)
@@ -605,7 +605,7 @@ and type_module sctx com delay mpath p =
 						t_hxb();
 						r
 					in
-					let m,chunks = f_next mc.mc_chunks EOT in
+					let m,chunks = f_next (if full_restore then mc.mc_chunks else mc.mc_min_chunks) EOT in
 					(* We try to avoid reading expressions as much as possible, so we only do this for
 					   our current display file if we're in display mode. *)
 					if restore_level <> Minimal then ignore(f_next chunks EOM)