From fe4e163478d9d90ad5e2b64c9cdd8b1239766d6e Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 16:42:00 +0000 Subject: [PATCH] Mark compile cache entries as used when they are reused The compile cache's size limit removes the least recently used files, judged by their access and modification times. Windows doesn't update access times by default, so objects that were reused in every build still looked as old as when they were compiled, and the limit removed them while unused ones stayed. Each build then recompiled objects that the previous one had removed. Update the modification time of a cache entry when it is reused, by rewriting its first byte unchanged. For a precompiled header, only the header is marked: on msvc, the time of its obj decides which cached objects are current. The size limit treats all files of a precompiled header directory as used when the newest of them was. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Va4FXE5HQQRYqakoJ8nj3b --- tools/hxcpp/CompileCache.hx | 45 +++++++++++++++++++++++++++++++------ tools/hxcpp/Compiler.hx | 6 +++++ 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/tools/hxcpp/CompileCache.hx b/tools/hxcpp/CompileCache.hx index a1041a886..97e20c48f 100644 --- a/tools/hxcpp/CompileCache.hx +++ b/tools/hxcpp/CompileCache.hx @@ -93,6 +93,33 @@ class CompileCache return dir + "/" + hash.substr(2) + inExt; } + // When a cache file was last used, for removing the least recently used files. + static function getUseTime(inFile:String) : Float + { + var info = FileSystem.stat(inFile); + return info.atime==null ? info.mtime.getTime() : Math.max(info.atime.getTime(), info.mtime.getTime()); + } + + // Marks a cache file as used now, so that clear() keeps it over files that weren't used for + // longer. Not every file system updates the access time when a file is read (Windows doesn't by + // default), so this updates the modification time by rewriting the first byte unchanged. + public static function markUsed(inFile:String) + { + try + { + var input = sys.io.File.read(inFile, true); + var first = input.readByte(); + input.close(); + var output = sys.io.File.update(inFile, true); + output.writeByte(first); + output.close(); + } + catch(e:Dynamic) + { + Log.v('Could not mark $inFile as used: $e'); + } + } + public static function clear(inDays:Int,inMB:Int,inLogInfo:Bool,inProject:String) { try @@ -128,6 +155,15 @@ class CompileCache } var dirFiles = FileSystem.readDirectory(path); var allDeleted = true; + // The files of a precompiled header are used together, but only the header itself + // is marked as used (see markUsed), so they all count as used when it was. + var pchTime:Null = null; + if (dir.substr(0,3)=="pch") + { + pchTime = 0; + for(file in dirFiles) + pchTime = Math.max(pchTime, getUseTime(path + "/" + file)); + } for(file in dirFiles) { total++; @@ -135,19 +171,14 @@ class CompileCache var doDelete = true; if (inDays>0) { - var info = FileSystem.stat(filename); - var atime = info.atime; - var time = atime==null ? info.mtime.getTime() : - Math.max(info.atime.getTime(),info.mtime.getTime() ); + var time = pchTime!=null ? pchTime : getUseTime(filename); if (time>=tooOld) doDelete = false; } else if (inMB>0) { var info = FileSystem.stat(filename); - var atime = info.atime; - var time = atime==null ? info.mtime.getTime() : - Math.max(info.atime.getTime(),info.mtime.getTime() ); + var time = pchTime!=null ? pchTime : getUseTime(filename); fileInfo.push( {filename:filename, time:time, size:info.size } ); sizeKB += info.size/1024; doDelete = false; diff --git a/tools/hxcpp/Compiler.hx b/tools/hxcpp/Compiler.hx index 85619f55a..a680a3e90 100644 --- a/tools/hxcpp/Compiler.hx +++ b/tools/hxcpp/Compiler.hx @@ -401,6 +401,7 @@ class Compiler //Log.info(""," copy cache for " + obj_name ); if (!useCacheInPlace) sys.io.File.copy(cacheName, obj_name); + CompileCache.markUsed(cacheName); found = true; } } @@ -651,7 +652,12 @@ class Compiler // No obj needed for gcc var obj = mPCH=="msvc" ? PathManager.combine(dir, file + mExt) : null; if (FileSystem.exists(pch_name) && (obj==null || FileSystem.exists(obj)) ) + { + // Not the obj: on msvc, its time decides which cached objects are current. + if (inGroup.isCached()) + CompileCache.markUsed(pch_name); return obj; + } } args = args.concat( mPCHFlags );