@@ -17,6 +17,7 @@ use log::{debug, info, trace, warn};
1717use rayon:: prelude:: * ;
1818use std:: cmp:: Ordering ;
1919use std:: collections:: BinaryHeap ;
20+ use std:: io:: Read ;
2021use std:: path:: Path ;
2122use std:: path:: PathBuf ;
2223use std:: process:: Command ;
@@ -34,6 +35,14 @@ fn compiler_output_to_string(bytes: &[u8]) -> String {
3435 String :: from_utf8_lossy ( bytes) . to_string ( )
3536}
3637
38+ // The compiler prefixes each .cmj with the 16-byte digest of its serialized
39+ // contents. Reading that header avoids hashing the whole file again.
40+ fn read_cmj_digest ( path : & Path ) -> Option < [ u8 ; 16 ] > {
41+ let mut digest = [ 0 ; 16 ] ;
42+ std:: fs:: File :: open ( path) . ok ( ) ?. read_exact ( & mut digest) . ok ( ) ?;
43+ Some ( digest)
44+ }
45+
3746/// Execute js-post-build command for a compiled JavaScript file.
3847/// The command runs in the directory containing the rescript.json that defines it.
3948/// The absolute path to the JS file is passed as an argument.
@@ -261,7 +270,7 @@ fn compile_one(
261270 & source_file. implementation . path ,
262271 ".cmj" ,
263272 ) ;
264- let cmj_digest = helpers :: compute_file_hash ( Path :: new ( & cmj_path) ) ;
273+ let cmj_digest = read_cmj_digest ( Path :: new ( & cmj_path) ) ;
265274
266275 let interface_result = source_file. interface . as_ref ( ) . map ( |iface| {
267276 compile_file (
@@ -282,7 +291,7 @@ fn compile_one(
282291 warn_error_override,
283292 ) ;
284293 let cmi_digest_after = helpers:: compute_file_hash ( Path :: new ( & cmi_path) ) ;
285- let cmj_digest_after = helpers :: compute_file_hash ( Path :: new ( & cmj_path) ) ;
294+ let cmj_digest_after = read_cmj_digest ( Path :: new ( & cmj_path) ) ;
286295
287296 // If the cmi is byte-for-byte unchanged, downstream modules can
288297 // short-circuit — we check both interface and implementation
@@ -1412,6 +1421,19 @@ mod tests {
14121421 assert ! ( decoded. starts_with( "Warning " ) ) ;
14131422 }
14141423
1424+ #[ test]
1425+ fn read_cmj_digest_reads_only_the_header ( ) {
1426+ let temp_dir = TempDir :: new ( ) . expect ( "temp dir should be created" ) ;
1427+ let cmj_path = temp_dir. path ( ) . join ( "Example.cmj" ) ;
1428+ let digest = [ 42 ; 16 ] ;
1429+ fs:: write ( & cmj_path, [ digest. as_slice ( ) , b"serialized contents" ] . concat ( ) )
1430+ . expect ( "cmj should be written" ) ;
1431+
1432+ assert_eq ! ( read_cmj_digest( & cmj_path) , Some ( digest) ) ;
1433+ fs:: write ( & cmj_path, b"short" ) . expect ( "cmj should be rewritten" ) ;
1434+ assert_eq ! ( read_cmj_digest( & cmj_path) , None ) ;
1435+ }
1436+
14151437 fn test_project_context ( root : & Path ) -> ProjectContext {
14161438 let config = config:: tests:: create_config ( config:: tests:: CreateConfigArgs {
14171439 name : "test-root" . to_string ( ) ,
0 commit comments