浏览代码

[hxb] Add define to opt out of hxb cache (#11668)

* [hxb] add -D disable-hxb-cache to opt out of hxb cache

* [ci] run server tests with and without hxb cache

* [tests] adjust test for in-memory vs hxb cache

* Add -D disable-hxb-cache to defines that don't change signature
Rudy Ges 1 年之前
父节点
当前提交
f5934bd573

+ 5 - 0
src-json/define.json

@@ -66,6 +66,11 @@
 		"define": "debug",
 		"doc": "Activated when compiling with -debug."
 	},
+	{
+		"name": "DisableHxbCache",
+		"define": "disable-hxb-cache",
+		"doc": "Use in-memory cache instead of hxb powered cache."
+	},
 	{
 		"name": "DisableUnicodeStrings",
 		"define": "disable-unicode-strings",

+ 4 - 1
src/compiler/compilationCache.ml

@@ -81,7 +81,7 @@ class context_cache (index : int) (sign : Digest.t) = object(self)
 		try (Hashtbl.find modules path).m_extra
 		with Not_found -> (self#get_hxb_module path).mc_extra
 
-	method cache_module config warn anon_identification path m =
+	method cache_hxb_module config warn anon_identification path m =
 		match m.m_extra.m_kind with
 		| MImport ->
 			Hashtbl.add modules m.m_path m
@@ -96,6 +96,9 @@ class context_cache (index : int) (sign : Digest.t) = object(self)
 				mc_extra = { m.m_extra with m_cache_state = MSGood }
 			}
 
+	method cache_module_in_memory path m =
+		Hashtbl.replace modules path m
+
 	method clear_temp_cache =
 		Hashtbl.clear tmp_binary_cache
 

+ 27 - 15
src/context/commonCache.ml

@@ -84,27 +84,39 @@ let get_cache_sign com = match com.Common.cache with
 let rec cache_context cs com =
 	let cc = get_cache com in
 	let sign = Define.get_signature com.defines in
-	let anon_identification = new Tanon_identification.tanon_identification in
-	let config = match com.hxb_writer_config with
-		| None ->
-			HxbWriterConfig.create_target_config ()
-		| Some config ->
-			if com.is_macro_context then config.macro_config else config.target_config
-	in
-	let cache_module m =
-		(* If we have a signature mismatch, look-up cache for module. Physical equality check is fine as a heueristic. *)
-		let cc = if m.m_extra.m_sign = sign then cc else cs#get_context m.m_extra.m_sign in
-		let warn w s p = com.warning w com.warning_options s p in
-		cc#cache_module config warn anon_identification m.m_path m;
+
+	let cache_module =
+		if Define.defined com.defines DisableHxbCache then
+			let cache_module m =
+				(* If we have a signature mismatch, look-up cache for module. Physical equality check is fine as a heuristic. *)
+				let cc = if m.m_extra.m_sign = sign then cc else cs#get_context m.m_extra.m_sign in
+				cc#cache_module_in_memory m.m_path m;
+			in
+			cache_module
+		else
+			let anon_identification = new Tanon_identification.tanon_identification in
+			let warn w s p = com.warning w com.warning_options s p in
+			let config = match com.hxb_writer_config with
+				| None ->
+					HxbWriterConfig.create_target_config ()
+				| Some config ->
+					if com.is_macro_context then config.macro_config else config.target_config
+			in
+			let cache_module m =
+				(* If we have a signature mismatch, look-up cache for module. Physical equality check is fine as a heuristic. *)
+				let cc = if m.m_extra.m_sign = sign then cc else cs#get_context m.m_extra.m_sign in
+				cc#cache_hxb_module config warn anon_identification m.m_path m;
+			in
+			cache_module
 	in
+
 	List.iter cache_module com.modules;
 	begin match com.get_macros() with
 		| None -> ()
 		| Some com -> cache_context cs com
 	end;
-	if Define.raw_defined com.defines "hxb.stats" then begin
-		HxbReader.dump_stats (platform_name com.platform) com.hxb_reader_stats;
-	end
+	if Define.raw_defined com.defines "hxb.stats" then
+		HxbReader.dump_stats (platform_name com.platform) com.hxb_reader_stats
 
 let maybe_add_context_sign cs com desc =
 	let sign = Define.get_signature com.defines in

+ 1 - 1
src/core/define.ml

@@ -152,7 +152,7 @@ let get_signature def =
 			   Parser.parse_macro_ident as well (issue #5682).
 			   Note that we should removed flags like use_rtti_doc here.
 			*)
-			| "display" | "use_rtti_doc" | "macro_times" | "display_details" | "no_copt" | "display_stdin" | "hxb.stats" | "fail_fast"
+			| "display" | "use_rtti_doc" | "macro_times" | "display_details" | "no_copt" | "display_stdin" | "disable-hxb-cache" | "hxb.stats" | "fail_fast"
 			| "message.reporting" | "message.log_file" | "message.log_format" | "message.no_color"
 			| "dump" | "dump_dependencies" | "dump_ignore_var_ids" -> acc
 			| _ -> (k ^ "=" ^ v) :: acc

+ 2 - 0
tests/runci/targets/Js.hx

@@ -126,6 +126,8 @@ class Js {
 		changeDirectory(serverDir);
 		runCommand("haxe", ["build.hxml"]);
 		runCommand("node", ["test.js"]);
+		runCommand("haxe", ["build.hxml", "-D", "disable-hxb-cache"]);
+		runCommand("node", ["test.js"]);
 
 		changeDirectory(sysDir);
 		installNpmPackages(["deasync"]);

+ 1 - 0
tests/server/src/TestCase.hx

@@ -100,6 +100,7 @@ class TestCase implements ITest implements ITestCase {
 	}
 
 	function runHaxe(args:Array<String>, done:() -> Void) {
+		#if disable-hxb-cache args = ["-D", "disable-hxb-cache"].concat(args); #end
 		messages = [];
 		errorMessages = [];
 		server.rawRequest(args, null, function(result) {

+ 12 - 3
tests/server/src/cases/ServerTests.hx

@@ -112,16 +112,23 @@ class ServerTests extends TestCase {
 		assertSuccess();
 	}
 
-	function testDisplayModuleRecache() {
+	@:variant("InMemory", true)
+	@:variant("Hxb", false)
+	function testDisplayModuleRecache(inMemory:Bool) {
 		vfs.putContent("HelloWorld.hx", getTemplate("HelloWorld.hx"));
 		var args = ["--main", "HelloWorld", "--interp"];
+		if (inMemory) args = args.concat(["-D", "disable-hxb-cache"]);
+		else args = args.concat(["--undefine", "disable-hxb-cache"]);
+
 		runHaxe(args);
 		runHaxe(args);
 		assertReuse("HelloWorld");
 
 		var args2 = ["--main", "HelloWorld", "--interp", "--display", "HelloWorld.hx@64@type"];
-		runHaxe(args2);
+		if (inMemory) args2 = args2.concat(["-D", "disable-hxb-cache"]);
+		else args2 = args2.concat(["--undefine", "disable-hxb-cache"]);
 
+		runHaxe(args2);
 		runHaxe(args);
 		assertReuse("HelloWorld");
 
@@ -130,7 +137,9 @@ class ServerTests extends TestCase {
 		runHaxe(args2);
 
 		runHaxe(args);
-		assertSkipping("HelloWorld", Tainted("server/invalidate"));
+
+		if (inMemory) assertSkipping("HelloWorld", Tainted("check_display_file"));
+		else assertSkipping("HelloWorld", Tainted("server/invalidate"));
 	}
 
 	function testMutuallyDependent() {