diff --git a/src/brep/brep_boolean.cpp b/src/brep/brep_boolean.cpp index d417a599e..b754979ba 100644 --- a/src/brep/brep_boolean.cpp +++ b/src/brep/brep_boolean.cpp @@ -41,13 +41,32 @@ enum ClassResult { IN = -1, ON = 0, OUT = 1 }; // ── Mesh caching ───────────────────────────────────────── /// Cache to_mesh() results keyed by BrepModel pointer. /// Avoids re-tessellating the same body for every face classification. -static std::unordered_map mesh_cache; +// std::map keeps element addresses stable across insertions (unordered_map +// would rehash and move cached meshes, invalidating in-flight references +// under the #pragma omp parallel for below). +static std::map mesh_cache; +static std::mutex mesh_cache_mutex; +// Serializes construction: prevents N OpenMP threads from all building the +// same mesh when the cache is cold ("thundering herd"). +static std::mutex mesh_build_mutex; /// Get the tessellated mesh for a body, reusing cached result. static const vde::mesh::HalfedgeMesh& get_mesh(const BrepModel& body) { - auto it = mesh_cache.find(&body); - if (it != mesh_cache.end()) return it->second; - auto [inserted_it, _] = mesh_cache.emplace(&body, body.to_mesh(0.05)); + { + std::lock_guard lock(mesh_cache_mutex); + auto it = mesh_cache.find(&body); + if (it != mesh_cache.end()) return it->second; + } + std::lock_guard build_lock(mesh_build_mutex); + { + // Double-check: another thread may have completed while we waited. + std::lock_guard lock(mesh_cache_mutex); + auto it = mesh_cache.find(&body); + if (it != mesh_cache.end()) return it->second; + } + auto mesh = body.to_mesh(0.05); + std::lock_guard lock(mesh_cache_mutex); + auto [inserted_it, _] = mesh_cache.emplace(&body, std::move(mesh)); return inserted_it->second; } diff --git a/src/brep/ssi_boolean.cpp b/src/brep/ssi_boolean.cpp index acbb614d6..9d80fe34a 100644 --- a/src/brep/ssi_boolean.cpp +++ b/src/brep/ssi_boolean.cpp @@ -64,8 +64,16 @@ namespace { // Mesh caching (shared with brep_boolean.cpp pattern) // ═══════════════════════════════════════════════════════════ -static std::unordered_map mesh_cache; +// std::map (node-based) keeps element addresses stable across insertions, so +// the references returned by get_mesh()/get_mesh_grid() never dangle when +// other threads insert additional bodies (unordered_map would rehash and move +// the values, invalidating in-flight references during OpenMP execution). +static std::map mesh_cache; static std::mutex mesh_cache_mutex; +// Serializes actual mesh/grid construction: without it, N OpenMP threads that +// all miss the cache build the same 40k-triangle mesh N times ("thundering +// herd", ~7s × N wasted CPU even though wall-clock overlap hides it). +static std::mutex mesh_build_mutex; // ── Diagnostic counters (only active under VDE_SSI_DIAG) ─────────────── static thread_local long long tls_mesh_builds = 0; // to_mesh() calls @@ -81,6 +89,14 @@ static const vde::mesh::HalfedgeMesh& get_mesh(const BrepModel& body) { auto it = mesh_cache.find(&body); if (it != mesh_cache.end()) return it->second; } + // Slow path: serialize construction so concurrent misses build once. + std::lock_guard build_lock(mesh_build_mutex); + { + // Double-check: another thread may have completed while we waited. + std::lock_guard lock(mesh_cache_mutex); + auto it = mesh_cache.find(&body); + if (it != mesh_cache.end()) return it->second; + } auto mesh = body.to_mesh(0.05); tls_mesh_builds++; std::lock_guard lock(mesh_cache_mutex); @@ -102,7 +118,7 @@ struct TriangleGrid { std::vector> cells; // triangle index per cell }; -static std::unordered_map grid_cache; +static std::map grid_cache; static const TriangleGrid& get_mesh_grid(const vde::mesh::HalfedgeMesh& mesh) { { @@ -110,6 +126,13 @@ static const TriangleGrid& get_mesh_grid(const vde::mesh::HalfedgeMesh& mesh) { auto it = grid_cache.find(&mesh); if (it != grid_cache.end()) return it->second; } + // Serialize grid construction (same thundering-herd concern as get_mesh). + std::lock_guard build_lock(mesh_build_mutex); + { + std::lock_guard lock(mesh_cache_mutex); + auto it = grid_cache.find(&mesh); + if (it != grid_cache.end()) return it->second; + } TriangleGrid g; g.mesh = &mesh;