From aade842de4f66ba3a9d965c2c4e856cf3b79ae83 Mon Sep 17 00:00:00 2001 From: Alan Garny Date: Mon, 28 Sep 2026 19:42:23 +1300 Subject: [PATCH 1/9] Cache the MathML DTD per thread. --- src/xmldoc.cpp | 47 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/src/xmldoc.cpp b/src/xmldoc.cpp index 24709be463..059513b846 100644 --- a/src/xmldoc.cpp +++ b/src/xmldoc.cpp @@ -104,27 +104,54 @@ std::string decompressMathMLDTD() return std::string(mathmlDTD.begin(), mathmlDTD.end()); } -void XmlDoc::parseMathML(const std::string &input) +/** + * @brief The parsed MathML DTD. + * + * Parsing the MathML DTD is expensive (it is about 390 KB long), so we parse it only once rather than every time that + * we validate some MathML. However, libxml2 builds (and caches) the content model of an element declaration the first + * time that it validates an element against it, i.e. validating against a DTD modifies it, so we have one parsed MathML + * DTD per thread. + */ +class MathmlDtd { - // Decompress the MathML DTD. - int sizeMathmlDTDUncompressed = MATHML_DTD_LEN; +public: + MathmlDtd() + { + auto mathmlDTD = decompressMathMLDTD(); + xmlParserInputBufferPtr buf = xmlParserInputBufferCreateMem(mathmlDTD.c_str(), static_cast(mathmlDTD.size()), XML_CHAR_ENCODING_ASCII); + + mDtd = xmlIOParseDTD(nullptr, buf, XML_CHAR_ENCODING_ASCII); + } + + ~MathmlDtd() + { + xmlFreeDtd(mDtd); + } - static std::string mathMLDTD; + MathmlDtd(const MathmlDtd &) = delete; + MathmlDtd &operator=(const MathmlDtd &) = delete; - if (mathMLDTD.empty()) { - mathMLDTD = decompressMathMLDTD(); + xmlDtdPtr dtd() const + { + return mDtd; } +private: + xmlDtdPtr mDtd = nullptr; +}; + +void XmlDoc::parseMathML(const std::string &input) +{ xmlInitParser(); + + thread_local MathmlDtd mathmlDtd; + xmlParserCtxtPtr context = xmlNewParserCtxt(); context->_private = reinterpret_cast(this); xmlSetStructuredErrorFunc(context, structuredErrorCallback); mPimpl->mXmlDocPtr = xmlCtxtReadDoc(context, reinterpret_cast(input.c_str()), "/", nullptr, 0); - xmlParserInputBufferPtr buf = xmlParserInputBufferCreateMem(reinterpret_cast(mathMLDTD.c_str()), sizeMathmlDTDUncompressed, XML_CHAR_ENCODING_ASCII); - xmlDtdPtr dtd = xmlIOParseDTD(nullptr, buf, XML_CHAR_ENCODING_ASCII); - xmlValidateDtd(&(context->vctxt), mPimpl->mXmlDocPtr, dtd); + xmlValidateDtd(&(context->vctxt), mPimpl->mXmlDocPtr, mathmlDtd.dtd()); - xmlFreeDtd(dtd); xmlFreeParserCtxt(context); xmlSetStructuredErrorFunc(nullptr, nullptr); xmlCleanupParser(); From 5e9c4cf5a2046718090ba7cdcba0df2b6f300a1c Mon Sep 17 00:00:00 2001 From: Alan Garny Date: Mon, 28 Sep 2026 22:28:49 +1300 Subject: [PATCH 2/9] Stop calling xmlCleanupParser() in XmlDoc. Remove `xmlCleanupParser()` from `XmlDoc::parse` and `XmlDoc::parseMathML` because it is a process-wide libxml2 teardown API that should only run when libxml2 is no longer needed. --- src/xmldoc.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/xmldoc.cpp b/src/xmldoc.cpp index 059513b846..42688b0f0c 100644 --- a/src/xmldoc.cpp +++ b/src/xmldoc.cpp @@ -88,7 +88,6 @@ void XmlDoc::parse(const std::string &input) mPimpl->mXmlDocPtr = xmlCtxtReadDoc(context, reinterpret_cast(input.c_str()), "/", nullptr, 0); xmlFreeParserCtxt(context); xmlSetStructuredErrorFunc(nullptr, nullptr); - xmlCleanupParser(); } std::string decompressMathMLDTD() @@ -111,6 +110,9 @@ std::string decompressMathMLDTD() * we validate some MathML. However, libxml2 builds (and caches) the content model of an element declaration the first * time that it validates an element against it, i.e. validating against a DTD modifies it, so we have one parsed MathML * DTD per thread. + * + * Note: this is why we never call xmlCleanupParser(). It is a process-wide operation that must only be called once + * libxml2 is no longer used (typically when an application exits) while a cached DTD may outlive any given call. */ class MathmlDtd { @@ -154,7 +156,6 @@ void XmlDoc::parseMathML(const std::string &input) xmlFreeParserCtxt(context); xmlSetStructuredErrorFunc(nullptr, nullptr); - xmlCleanupParser(); } std::string XmlDoc::prettyPrint() const From e75ba4d3a1ebe247e7a7f6ea39ba85d62bda9e93 Mon Sep 17 00:00:00 2001 From: Alan Garny Date: Mon, 28 Sep 2026 22:43:48 +1300 Subject: [PATCH 3/9] Avoid libxml parser cleanup. This change clarifies the MathML DTD cache lifetime and removes explicit xmlCleanupParser() calls from the libxml parser tests. --- src/xmldoc.cpp | 7 +++++-- tests/parser/libxml_user.cpp | 2 -- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/xmldoc.cpp b/src/xmldoc.cpp index 42688b0f0c..c2b4b3a7aa 100644 --- a/src/xmldoc.cpp +++ b/src/xmldoc.cpp @@ -111,8 +111,11 @@ std::string decompressMathMLDTD() * time that it validates an element against it, i.e. validating against a DTD modifies it, so we have one parsed MathML * DTD per thread. * - * Note: this is why we never call xmlCleanupParser(). It is a process-wide operation that must only be called once - * libxml2 is no longer used (typically when an application exits) while a cached DTD may outlive any given call. + * Note: this is why we never call xmlCleanupParser(). It is a process-wide operation after which no libxml2 call may be + * made (from any thread), i.e. it should only be called right before a process exits, while a cached DTD + * outlives any given call. Since libxml2 2.9.11, cleanup is performed automatically anyway. Also, the DTD is + * detached from its (dictionary-less) document, so it only consists of heap memory, meaning that freeing it + * when a thread exits does not rely on any global state that xmlCleanupParser() might have freed. */ class MathmlDtd { diff --git a/tests/parser/libxml_user.cpp b/tests/parser/libxml_user.cpp index ba84240c49..40e7c9fcb9 100644 --- a/tests/parser/libxml_user.cpp +++ b/tests/parser/libxml_user.cpp @@ -53,7 +53,6 @@ TEST(Parser, parseValidXmlDirectlyUsingLibxml) EXPECT_NE(nullptr, doc); xmlFreeDoc(doc); xmlSetStructuredErrorFunc(nullptr, nullptr); - xmlCleanupParser(); } TEST(Parser, parseInvalidXmlDirectlyUsingLibxml) @@ -74,7 +73,6 @@ TEST(Parser, parseInvalidXmlDirectlyUsingLibxml) xmlDocPtr doc = xmlCtxtReadDoc(context, reinterpret_cast(e.c_str()), "/", nullptr, 0); xmlFreeParserCtxt(context); xmlSetStructuredErrorFunc(nullptr, nullptr); - xmlCleanupParser(); EXPECT_EQ(nullptr, doc); } From 8782f9c5d4ad55afa7b62ca72a710692e75ab102 Mon Sep 17 00:00:00 2001 From: Alan Garny Date: Mon, 28 Sep 2026 22:58:29 +1300 Subject: [PATCH 4/9] Validator: added a test to validate MathML concurrently. --- tests/validator/validator.cpp | 54 +++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tests/validator/validator.cpp b/tests/validator/validator.cpp index b3527f98db..62753e718a 100644 --- a/tests/validator/validator.cpp +++ b/tests/validator/validator.cpp @@ -18,6 +18,8 @@ limitations under the License. #include +#include + #include "test_utils.h" /* @@ -4006,3 +4008,55 @@ TEST(Validator, invalidMathMLElementsChildrenOrSiblings) EXPECT_EQ_ISSUES(expectedIssues, validator); } + +TEST(Validator, validateMathmlConcurrently) +{ + // Validate a model that yields some W3C MathML DTD errors from several threads at the same time. Each thread uses + // (and, upon exiting, frees) its own parsed MathML DTD, so each thread should get the same issues as the main + // thread, which validates the model once all the other threads have exited. + + static const size_t THREAD_COUNT = 8; + static const size_t VALIDATION_COUNT = 5; + + auto validate = [](const std::string &modelContents) { + auto parser = libcellml::Parser::create(); + auto model = parser->parseModel(modelContents); + auto validator = libcellml::Validator::create(); + + validator->validateModel(model); + + std::vector issues; + + for (size_t i = 0; i < validator->issueCount(); ++i) { + issues.push_back(validator->issue(i)->description()); + } + + return issues; + }; + + auto modelContents = fileContents("invalidmathmlelementschildrenorsiblings.cellml"); + std::vector>> issues(THREAD_COUNT, std::vector>(VALIDATION_COUNT)); + std::vector threads; + + for (size_t i = 0; i < THREAD_COUNT; ++i) { + threads.emplace_back([&, i]() { + for (size_t j = 0; j < VALIDATION_COUNT; ++j) { + issues[i][j] = validate(modelContents); + } + }); + } + + for (auto &thread : threads) { + thread.join(); + } + + auto expectedIssues = validate(modelContents); + + EXPECT_FALSE(expectedIssues.empty()); + + for (size_t i = 0; i < THREAD_COUNT; ++i) { + for (size_t j = 0; j < VALIDATION_COUNT; ++j) { + EXPECT_EQ(expectedIssues, issues[i][j]); + } + } +} From 315d7503a59027a64954f6cb7f64e18fb711fc26 Mon Sep 17 00:00:00 2001 From: Alan Garny Date: Mon, 28 Sep 2026 23:08:55 +1300 Subject: [PATCH 5/9] Initialise libxml2 exactly once. --- src/xmldoc.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/xmldoc.cpp b/src/xmldoc.cpp index c2b4b3a7aa..b36dba51a4 100644 --- a/src/xmldoc.cpp +++ b/src/xmldoc.cpp @@ -19,6 +19,7 @@ limitations under the License. #include #include #include +#include #include #include #include @@ -52,6 +53,20 @@ void structuredErrorCallback(void *userData, XML_ERROR_CALLBACK_ARGUMENT_TYPE er doc->addXmlError(errorString); } +/** + * @brief Initialise libxml2. + * + * Initialise libxml2 exactly once, no matter how many threads call this function and when. Before libxml2 2.14, + * xmlInitParser() checks whether libxml2 is already initialised without any synchronisation, which is a data race + * when it gets called from different threads at the same time. + */ +void initialiseLibxml2() +{ + static std::once_flag flag; + + std::call_once(flag, xmlInitParser); +} + /** * @brief The XmlDoc::XmlDocImpl struct. * @@ -81,7 +96,7 @@ XmlDoc::~XmlDoc() void XmlDoc::parse(const std::string &input) { - xmlInitParser(); + initialiseLibxml2(); xmlParserCtxtPtr context = xmlNewParserCtxt(); context->_private = reinterpret_cast(this); xmlSetStructuredErrorFunc(context, structuredErrorCallback); @@ -147,7 +162,7 @@ class MathmlDtd void XmlDoc::parseMathML(const std::string &input) { - xmlInitParser(); + initialiseLibxml2(); thread_local MathmlDtd mathmlDtd; From 22201a15cb8c7b884d977d4c574b599412a74c0e Mon Sep 17 00:00:00 2001 From: Alan Garny Date: Mon, 28 Sep 2026 23:21:05 +1300 Subject: [PATCH 6/9] Use atomic updates for coverage profiles. --- cmake/environmentchecks.cmake | 8 ++++---- src/CMakeLists.txt | 11 +++++++---- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/cmake/environmentchecks.cmake b/cmake/environmentchecks.cmake index 21ba618b5a..7e2ae982e9 100644 --- a/cmake/environmentchecks.cmake +++ b/cmake/environmentchecks.cmake @@ -77,11 +77,11 @@ else () set(_ORIGINAL_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) - set(CMAKE_REQUIRED_FLAGS -fprofile-instr-generate) - check_cxx_compiler_flag("-fprofile-instr-generate -fcoverage-mapping" LLVM_COVERAGE_COMPILER_FLAGS_OK) + set(CMAKE_REQUIRED_FLAGS "-fprofile-instr-generate -fprofile-update=atomic") + check_cxx_compiler_flag("-fprofile-instr-generate -fcoverage-mapping -fprofile-update=atomic" LLVM_COVERAGE_COMPILER_FLAGS_OK) - set(CMAKE_REQUIRED_FLAGS "-fprofile-arcs -ftest-coverage") - check_cxx_compiler_flag("-fprofile-arcs -ftest-coverage" GCC_COVERAGE_COMPILER_FLAGS_OK) + set(CMAKE_REQUIRED_FLAGS "-fprofile-arcs -ftest-coverage -fprofile-update=atomic") + check_cxx_compiler_flag("-fprofile-arcs -ftest-coverage -fprofile-update=atomic" GCC_COVERAGE_COMPILER_FLAGS_OK) set(CMAKE_REQUIRED_FLAGS ${_ORIGINAL_CMAKE_REQUIRED_FLAGS}) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 488d2a773a..dc8691a3d7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -281,8 +281,10 @@ function(append_target_property _TARGET _PROPERTY _VALUE) endfunction() if(LIBCELLML_COVERAGE) - append_target_property(cellml COMPILE_FLAGS "-fprofile-arcs -ftest-coverage") - append_target_property(cellml LINK_FLAGS "-fprofile-arcs -ftest-coverage") + # Note: we use atomic profile updates since some of our tests use libCellML from several threads at the same time, + # which would otherwise result in some profile counter updates being lost, and therefore in some flaky coverage. + append_target_property(cellml COMPILE_FLAGS "-fprofile-arcs -ftest-coverage -fprofile-update=atomic") + append_target_property(cellml LINK_FLAGS "-fprofile-arcs -ftest-coverage -fprofile-update=atomic") # Share some paths with interested parties (tests) set(GCOV_ANALYSIS_PATH "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/cellml.dir" PARENT_SCOPE) @@ -299,8 +301,9 @@ if(LIBCELLML_COVERAGE) endif() if(LIBCELLML_LLVM_COVERAGE) - append_target_property(cellml COMPILE_FLAGS "-fprofile-instr-generate -fcoverage-mapping") - append_target_property(cellml LINK_FLAGS "-fprofile-instr-generate") + # Note: we use atomic profile updates (see above). + append_target_property(cellml COMPILE_FLAGS "-fprofile-instr-generate -fcoverage-mapping -fprofile-update=atomic") + append_target_property(cellml LINK_FLAGS "-fprofile-instr-generate -fprofile-update=atomic") endif() install(TARGETS cellml EXPORT libcellml-targets From 580758c46d6061f9d278d59e6743cbebf422b54c Mon Sep 17 00:00:00 2001 From: Alan Garny Date: Mon, 28 Sep 2026 23:29:53 +1300 Subject: [PATCH 7/9] Avoid std::call_once() in libxml2 initialisation. --- src/xmldoc.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/xmldoc.cpp b/src/xmldoc.cpp index b36dba51a4..311377ba39 100644 --- a/src/xmldoc.cpp +++ b/src/xmldoc.cpp @@ -19,7 +19,6 @@ limitations under the License. #include #include #include -#include #include #include #include @@ -59,12 +58,19 @@ void structuredErrorCallback(void *userData, XML_ERROR_CALLBACK_ARGUMENT_TYPE er * Initialise libxml2 exactly once, no matter how many threads call this function and when. Before libxml2 2.14, * xmlInitParser() checks whether libxml2 is already initialised without any synchronisation, which is a data race * when it gets called from different threads at the same time. + * + * Note: we rely on the initialisation of a function-local static variable being thread-safe (since C++11) rather than + * on std::call_once(), which may require linking against a thread library (e.g., pthreads with glibc < 2.34). */ void initialiseLibxml2() { - static std::once_flag flag; + static const bool initialised = []() { + xmlInitParser(); + + return true; + }(); - std::call_once(flag, xmlInitParser); + (void)initialised; } /** From 184c637256c8b66acdbcde4730f09c0f71f8014a Mon Sep 17 00:00:00 2001 From: Alan Garny Date: Mon, 28 Sep 2026 23:39:16 +1300 Subject: [PATCH 8/9] Use atomic coverage checks and synchronise our test threads. --- cmake/environmentchecks.cmake | 12 ++++++------ tests/validator/validator.cpp | 10 ++++++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/cmake/environmentchecks.cmake b/cmake/environmentchecks.cmake index 7e2ae982e9..fd496e076b 100644 --- a/cmake/environmentchecks.cmake +++ b/cmake/environmentchecks.cmake @@ -78,10 +78,10 @@ else () set(_ORIGINAL_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) set(CMAKE_REQUIRED_FLAGS "-fprofile-instr-generate -fprofile-update=atomic") - check_cxx_compiler_flag("-fprofile-instr-generate -fcoverage-mapping -fprofile-update=atomic" LLVM_COVERAGE_COMPILER_FLAGS_OK) + check_cxx_compiler_flag("-fprofile-instr-generate -fcoverage-mapping -fprofile-update=atomic" LLVM_ATOMIC_COVERAGE_COMPILER_FLAGS_OK) set(CMAKE_REQUIRED_FLAGS "-fprofile-arcs -ftest-coverage -fprofile-update=atomic") - check_cxx_compiler_flag("-fprofile-arcs -ftest-coverage -fprofile-update=atomic" GCC_COVERAGE_COMPILER_FLAGS_OK) + check_cxx_compiler_flag("-fprofile-arcs -ftest-coverage -fprofile-update=atomic" GCC_ATOMIC_COVERAGE_COMPILER_FLAGS_OK) set(CMAKE_REQUIRED_FLAGS ${_ORIGINAL_CMAKE_REQUIRED_FLAGS}) @@ -96,12 +96,12 @@ else () CLANG_TIDY_EXE CLANG_FORMAT_EXE FIND_EXE - GCC_COVERAGE_COMPILER_FLAGS_OK + GCC_ATOMIC_COVERAGE_COMPILER_FLAGS_OK GCOV_EXE GIT_EXE INSTALL_NAME_TOOL_EXE + LLVM_ATOMIC_COVERAGE_COMPILER_FLAGS_OK LLVM_COV_EXE - LLVM_COVERAGE_COMPILER_FLAGS_OK LLVM_PROFDATA_EXE SWIG_EXECUTABLE VALGRIND_EXE @@ -144,7 +144,7 @@ if(CLANG_TIDY_EXE) set(CLANG_TIDY_AVAILABLE TRUE CACHE INTERNAL "Executable required to perform static analysis is available.") endif() -if(FIND_EXE AND GCOV_EXE AND Python_Interpreter_FOUND AND GCC_COVERAGE_COMPILER_FLAGS_OK) +if(FIND_EXE AND GCOV_EXE AND Python_Interpreter_FOUND AND GCC_ATOMIC_COVERAGE_COMPILER_FLAGS_OK) set(COVERAGE_TESTING_AVAILABLE TRUE CACHE INTERNAL "Executables required to run the coverage testing are available.") endif() @@ -160,7 +160,7 @@ if(VALGRIND_EXE AND Python_Interpreter_FOUND) set(VALGRIND_TESTING_AVAILABLE TRUE CACHE INTERNAL "Executable required to run valgrind testing is available.") endif() -if(LLVM_PROFDATA_EXE AND LLVM_COV_EXE AND FIND_EXE AND LLVM_COVERAGE_COMPILER_FLAGS_OK) +if(LLVM_PROFDATA_EXE AND LLVM_COV_EXE AND FIND_EXE AND LLVM_ATOMIC_COVERAGE_COMPILER_FLAGS_OK) set(LLVM_COVERAGE_TESTING_AVAILABLE TRUE CACHE INTERNAL "Executables required to run the llvm coverage testing are available.") endif() diff --git a/tests/validator/validator.cpp b/tests/validator/validator.cpp index 62753e718a..522fdaecab 100644 --- a/tests/validator/validator.cpp +++ b/tests/validator/validator.cpp @@ -18,6 +18,7 @@ limitations under the License. #include +#include #include #include "test_utils.h" @@ -4037,9 +4038,18 @@ TEST(Validator, validateMathmlConcurrently) auto modelContents = fileContents("invalidmathmlelementschildrenorsiblings.cellml"); std::vector>> issues(THREAD_COUNT, std::vector>(VALIDATION_COUNT)); std::vector threads; + std::atomic readyThreadCount(0); for (size_t i = 0; i < THREAD_COUNT; ++i) { threads.emplace_back([&, i]() { + // Wait for all the threads to be ready, so that they all validate the model at the same time. + + ++readyThreadCount; + + while (readyThreadCount < THREAD_COUNT) { + std::this_thread::yield(); + } + for (size_t j = 0; j < VALIDATION_COUNT; ++j) { issues[i][j] = validate(modelContents); } From 55904b2742456a97fc6706e87fa24ae3a18420a6 Mon Sep 17 00:00:00 2001 From: Alan Garny Date: Thu, 3 Dec 2026 15:11:11 +1300 Subject: [PATCH 9/9] Revised/removed some comments. --- src/CMakeLists.txt | 4 +--- src/xmldoc.cpp | 13 +++++-------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index dc8691a3d7..a9cb0065a6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -281,8 +281,7 @@ function(append_target_property _TARGET _PROPERTY _VALUE) endfunction() if(LIBCELLML_COVERAGE) - # Note: we use atomic profile updates since some of our tests use libCellML from several threads at the same time, - # which would otherwise result in some profile counter updates being lost, and therefore in some flaky coverage. + # Note: we use atomic profile updates for threaded tests coverage counting. append_target_property(cellml COMPILE_FLAGS "-fprofile-arcs -ftest-coverage -fprofile-update=atomic") append_target_property(cellml LINK_FLAGS "-fprofile-arcs -ftest-coverage -fprofile-update=atomic") @@ -301,7 +300,6 @@ if(LIBCELLML_COVERAGE) endif() if(LIBCELLML_LLVM_COVERAGE) - # Note: we use atomic profile updates (see above). append_target_property(cellml COMPILE_FLAGS "-fprofile-instr-generate -fcoverage-mapping -fprofile-update=atomic") append_target_property(cellml LINK_FLAGS "-fprofile-instr-generate -fprofile-update=atomic") endif() diff --git a/src/xmldoc.cpp b/src/xmldoc.cpp index 6312f1a85b..f7dfa834f3 100644 --- a/src/xmldoc.cpp +++ b/src/xmldoc.cpp @@ -58,9 +58,6 @@ void structuredErrorCallback(void *userData, XML_ERROR_CALLBACK_ARGUMENT_TYPE er * Initialise libxml2 exactly once, no matter how many threads call this function and when. Before libxml2 2.14, * xmlInitParser() checks whether libxml2 is already initialised without any synchronisation, which is a data race * when it gets called from different threads at the same time. - * - * Note: we rely on the initialisation of a function-local static variable being thread-safe (since C++11) rather than - * on std::call_once(), which may require linking against a thread library (e.g., pthreads with glibc < 2.34). */ void initialiseLibxml2() { @@ -133,11 +130,11 @@ std::string decompressMathMLDTD() * time that it validates an element against it, i.e. validating against a DTD modifies it, so we have one parsed MathML * DTD per thread. * - * Note: this is why we never call xmlCleanupParser(). It is a process-wide operation after which no libxml2 call may be - * made (from any thread), i.e. it should only be called right before a process exits, while a cached DTD - * outlives any given call. Since libxml2 2.9.11, cleanup is performed automatically anyway. Also, the DTD is - * detached from its (dictionary-less) document, so it only consists of heap memory, meaning that freeing it - * when a thread exits does not rely on any global state that xmlCleanupParser() might have freed. + * Note: a thread's parsed MathML DTD is only freed when that thread exits, not when it has finished validating some + * MathML. A parsed MathML DTD takes about 4 MB of memory, so an application that validates MathML from a large + * pool of long-lived threads will hold about 4 MB per thread for as long as the pool exists. If this ever becomes + * a problem, we could, for instance, keep a fixed number of parsed MathML DTDs and lend each of them to one + * thread at a time. */ class MathmlDtd {