1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950(* Claude Code
*
* Copyright (C) 2026 Yoann Padioleau
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public License
* (LGPL) as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*)(* See Mesh_cache.mli. *)(* [used]: whether the mesh was used since the last sweep *)type'meshentry={mesh:'mesh;mutableused:bool}typestats={live:int;built:int;freed:int}type'mesht={meshes:(int,'meshentry)Hashtbl.t;(* counted during the current frame *)mutablebuilt:int;(* of the last frame, i.e. at the last sweep *)mutablelast:stats;}letcreate():'mesht={meshes=Hashtbl.create64;built=0;last={live=0;built=0;freed=0}}letfind_or_build(cache:'mesht)(id:int)(build:unit->'mesh):'mesh=matchHashtbl.find_optcache.meshesidwith|Somee->e.used<-true;e.mesh|None->letmesh=build()inHashtbl.replacecache.meshesid{mesh;used=true};cache.built<-cache.built+1;meshletsweep(cache:'mesht)~(free:'mesh->unit):unit=(* not removing from the table while iterating over it: undefined *)letunused=Hashtbl.fold(funideids->ife.usedthenidselseid::ids)cache.meshes[]inList.iter(funid->free(Hashtbl.findcache.meshesid).mesh;Hashtbl.removecache.meshesid)unused;Hashtbl.iter(fun_e->e.used<-false)cache.meshes;cache.last<-{live=Hashtbl.lengthcache.meshes;built=cache.built;freed=List.lengthunused};cache.built<-0letstats(cache:'mesht):stats=cache.last