From 8ecc1269d7095508f3a0a5d5387097636333ca4d Mon Sep 17 00:00:00 2001 From: Thomas Krijnen Date: Fri, 18 Mar 2016 11:21:04 +0100 Subject: [PATCH] Compile-time options to manipulate caching behaviour --- src/ifcgeom/IfcGeom.h | 24 +++++++++++++++++++++++- src/ifcgeom/IfcGeomIterator.h | 3 +++ src/ifcgeom/IfcGeomRenderStyles.cpp | 6 +++--- src/ifcgeom/IfcGeomRenderStyles.h | 12 +++--------- src/ifcgeom/IfcRegister.cpp | 4 ++++ 5 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/ifcgeom/IfcGeom.h b/src/ifcgeom/IfcGeom.h index c593fbab95..7fff6d189c 100644 --- a/src/ifcgeom/IfcGeom.h +++ b/src/ifcgeom/IfcGeom.h @@ -46,22 +46,35 @@ #include "../ifcgeom/IfcRepresentationShapeItem.h" #include "../ifcgeom/IfcGeomShapeType.h" +#define NO_CACHE + +#ifdef NO_CACHE + +#define IN_CACHE(T,E,t,e) +#define CACHE(T,E,e) + +#else + #define IN_CACHE(T,E,t,e) std::map::const_iterator it = cache.T.find(E->entity->id());\ if ( it != cache.T.end() ) { e = it->second; return true; } #define CACHE(T,E,e) cache.T[E->entity->id()] = e; +#endif + namespace IfcGeom { class Cache { public: #include "IfcRegisterCreateCache.h" - std::map Style; std::map Shape; }; class Kernel { private: +#ifndef NO_CACHE Cache cache; +#endif + std::map style_cache; public: // Tolerances and settings for various geometrical operations: enum GeomValue { @@ -182,6 +195,15 @@ public: return std::make_pair(0,0); } + void purge_cache() { + // Rather hack-ish, but a stopgap solution to keep memory under control + // for large files. SurfaceStyles need to be kept at all costs, as they + // are read later on when serializing Collada files. +#ifndef NO_CACHE + cache = Cache(); +#endif + } + #include "IfcRegisterGeomHeader.h" }; diff --git a/src/ifcgeom/IfcGeomIterator.h b/src/ifcgeom/IfcGeomIterator.h index 30bf6fe9cb..ecef8225c8 100644 --- a/src/ifcgeom/IfcGeomIterator.h +++ b/src/ifcgeom/IfcGeomIterator.h @@ -283,6 +283,9 @@ namespace IfcGeom { private: // Move to the next IfcRepresentation void _nextShape() { + if (done % 100 == 99) { + // kernel.purge_cache(); + } ifcproducts.reset(); ++ representation_iterator; ++ done; diff --git a/src/ifcgeom/IfcGeomRenderStyles.cpp b/src/ifcgeom/IfcGeomRenderStyles.cpp index 2d9da9a042..a65c608d53 100644 --- a/src/ifcgeom/IfcGeomRenderStyles.cpp +++ b/src/ifcgeom/IfcGeomRenderStyles.cpp @@ -56,8 +56,8 @@ const IfcGeom::SurfaceStyle* IfcGeom::Kernel::get_style(const IfcSchema::IfcRepr return 0; } int surface_style_id = shading_styles.first->entity->id(); - std::map::const_iterator it = cache.Style.find(surface_style_id); - if (it != cache.Style.end()) { + std::map::const_iterator it = style_cache.find(surface_style_id); + if (it != style_cache.end()) { return &(it->second); } SurfaceStyle surface_style; @@ -104,7 +104,7 @@ const IfcGeom::SurfaceStyle* IfcGeom::Kernel::get_style(const IfcSchema::IfcRepr surface_style.Transparency().reset(d); } } - return &(cache.Style[surface_style_id] = surface_style); + return &(style_cache[surface_style_id] = surface_style); } static std::map default_materials; diff --git a/src/ifcgeom/IfcGeomRenderStyles.h b/src/ifcgeom/IfcGeomRenderStyles.h index d84b85d737..a970f15a4e 100644 --- a/src/ifcgeom/IfcGeomRenderStyles.h +++ b/src/ifcgeom/IfcGeomRenderStyles.h @@ -44,7 +44,7 @@ namespace IfcGeom { double& B() { return data[2]; } }; private: - boost::optional name; + std::string name; boost::optional id; boost::optional diffuse, specular; boost::optional transparency; @@ -73,16 +73,10 @@ namespace IfcGeom { // pointer addresses of the styles, as they are always referenced // from out of a global map of some sort. bool operator==(const SurfaceStyle& other) { - if (name && other.name) { - return *name == *other.name; - } else if (id && other.id) { - return *id == *other.id; - } else { - return false; - } + return name == other.name; } - const std::string& Name() const { return *name; } + const std::string& Name() const { return name; } const boost::optional& Diffuse() const { return diffuse; } const boost::optional& Specular() const { return specular; } diff --git a/src/ifcgeom/IfcRegister.cpp b/src/ifcgeom/IfcRegister.cpp index 2b7d836c63..ba119c12bc 100644 --- a/src/ifcgeom/IfcRegister.cpp +++ b/src/ifcgeom/IfcRegister.cpp @@ -49,8 +49,10 @@ bool IfcGeom::Kernel::convert_shape(const IfcBaseClass* l, TopoDS_Shape& r) { bool processed = false; bool ignored = false; +#ifndef NO_CACHE std::map::const_iterator it = cache.Shape.find(id); if ( it != cache.Shape.end() ) { r = it->second; return true; } +#endif const bool include_curves = getValue(GV_DIMENSIONALITY) != +1; const bool include_solids_and_surfaces = getValue(GV_DIMENSIONALITY) != -1; @@ -85,7 +87,9 @@ bool IfcGeom::Kernel::convert_shape(const IfcBaseClass* l, TopoDS_Shape& r) { if ( processed && success ) { const double precision = getValue(GV_PRECISION); apply_tolerance(r, precision); +#ifndef NO_CACHE cache.Shape[id] = r; +#endif } else if (!ignored) { const char* const msg = processed ? "Failed to convert:"