From 698c708bf6c15ed63918220e47582d1d937c6e5f Mon Sep 17 00:00:00 2001 From: Thomas Krijnen Date: Mon, 3 Oct 2022 14:04:42 +0200 Subject: [PATCH] Reduce heap allocations during parse by reusing vector for top level instances --- src/ifcparse/IfcFile.h | 2 ++ src/ifcparse/IfcParse.cpp | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/ifcparse/IfcFile.h b/src/ifcparse/IfcFile.h index f915059168..0742c2efaa 100644 --- a/src/ifcparse/IfcFile.h +++ b/src/ifcparse/IfcFile.h @@ -130,6 +130,8 @@ private: const IfcParse::schema_definition* schema_; const IfcParse::declaration* ifcroot_type_; + std::vector internal_attribute_vector_; + entity_by_id_t byid; entities_by_type_t bytype; entities_by_type_t bytype_excl; diff --git a/src/ifcparse/IfcParse.cpp b/src/ifcparse/IfcParse.cpp index 0f60c9d3f2..888cd60b04 100644 --- a/src/ifcparse/IfcParse.cpp +++ b/src/ifcparse/IfcParse.cpp @@ -695,7 +695,15 @@ size_t IfcParse::IfcFile::load(unsigned entity_instance_name, const IfcParse::en std::vector* vector = 0; vector_or_array filler(attributes, num_attributes); if (attributes == 0) { - vector = new std::vector(); + if (num_attributes != 0) { + // If num_attributes is zero we know this is a top-level entity instance (or header entity) being parsed. + // There can only be parsed one of these at a time, so we can reuse the vector we have defined at the file + // scope. + vector = &internal_attribute_vector_; + vector->clear(); + } else { + vector = new std::vector; + } filler = vector_or_array(vector); } @@ -743,7 +751,9 @@ size_t IfcParse::IfcFile::load(unsigned entity_instance_name, const IfcParse::en } } - delete vector; + if (vector != &internal_attribute_vector_) { + delete vector; + } return return_value; } @@ -1467,6 +1477,9 @@ void IfcFile::initialize_(IfcParse::IfcSpfStream* s) { // number parsing. See comment above on line 41. init_locale(); + // prevent heap allocations during parse + internal_attribute_vector_.reserve(64); + parsing_complete_ = false; MaxId = 0; tokens = 0;