Skip to content

Commit 7159019

Browse files
authored
Merge pull request #1292 from Geode-solutions/fix/tracing-shared
fix(BRepRayTracing): use shared_mutex
2 parents f78bab3 + b38dfdc commit 7159019

1 file changed

Lines changed: 22 additions & 16 deletions

File tree

src/geode/model/helpers/ray_tracing.cpp

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
#include <geode/model/helpers/ray_tracing.hpp>
2525

26-
#include <mutex>
26+
#include <absl/base/call_once.h>
2727

2828
#include <geode/basic/pimpl_impl.hpp>
2929

@@ -237,7 +237,15 @@ namespace geode
237237
class BRepRayTracing::Impl
238238
{
239239
public:
240-
explicit Impl( const BRep& brep ) : brep_( brep ) {}
240+
explicit Impl( const BRep& brep ) : brep_( brep )
241+
{
242+
aabb_trees_.reserve( brep.nb_surfaces() );
243+
for( const auto& surface : brep.surfaces() )
244+
{
245+
aabb_trees_.emplace(
246+
surface.id(), std::make_unique< CachedTree >() );
247+
}
248+
}
241249

242250
BoundarySurfaceIntersections find_intersections_with_boundaries(
243251
const InfiniteLine3D& infinite_line, const Block3D& block )
@@ -298,26 +306,24 @@ namespace geode
298306
}
299307

300308
private:
309+
struct CachedTree
310+
{
311+
absl::once_flag built;
312+
AABBTree3D tree;
313+
};
314+
301315
const AABBTree3D& surface_aabb( const Surface3D& surface )
302316
{
303-
std::lock_guard lock{ mutex_ };
304-
{
305-
const auto it = aabb_trees_.find( surface.id() );
306-
if( it != aabb_trees_.end() )
307-
{
308-
return *it->second;
309-
}
310-
}
311-
const auto [it, inserted] = aabb_trees_.emplace(
312-
surface.id(), std::make_unique< AABBTree3D >(
313-
create_aabb_tree( surface.mesh() ) ) );
314-
return *it->second;
317+
auto& entry = *aabb_trees_.at( surface.id() );
318+
absl::call_once( entry.built, [&surface, &entry] {
319+
entry.tree = create_aabb_tree( surface.mesh() );
320+
} );
321+
return entry.tree;
315322
}
316323

317324
private:
318325
const BRep& brep_;
319-
absl::flat_hash_map< uuid, std::unique_ptr< AABBTree3D > > aabb_trees_;
320-
std::mutex mutex_;
326+
absl::flat_hash_map< uuid, std::unique_ptr< CachedTree > > aabb_trees_;
321327
};
322328

323329
BRepRayTracing::BRepRayTracing( const BRep& brep ) : impl_{ brep } {}

0 commit comments

Comments
 (0)