From 175d57c1266a37f01fae7ae64370eb75e2e12566 Mon Sep 17 00:00:00 2001 From: Dan Short Date: Fri, 31 Jul 2020 13:25:46 +0100 Subject: [PATCH] Load the serialized representation from file once The previous implementation had each sampling run reading from the serialized file. This introduced a large I/O overhead and the performance was much slower than the equivalent run with the unserialized source. --- .../serialized_custom_source/serialized_source_ring.cpp | 4 +++- src/source.cpp | 8 +++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/examples/serialized_custom_source/serialized_source_ring.cpp b/examples/serialized_custom_source/serialized_source_ring.cpp index 560a99345..7e19bc579 100644 --- a/examples/serialized_custom_source/serialized_source_ring.cpp +++ b/examples/serialized_custom_source/serialized_source_ring.cpp @@ -23,9 +23,11 @@ class SerialisedSource { // The deserialisation routine populates the constructor from well defined elements // in the input XML document. + // Note that the source will have already been read from file, so what will be passed + // in here is a string-like serialized value (not the path to the serialized value). static SerialisedSource from_xml(char* serialised_source) { pugi::xml_document doc; - doc.load_file(serialised_source); + doc.load_string(serialised_source); pugi::xml_node root_node = doc.root().child("Source"); double radius = root_node.child("Radius").text().as_double(); double energy = root_node.child("Energy").text().as_double(); diff --git a/src/source.cpp b/src/source.cpp index 86dac6b3f..d7bcf726b 100644 --- a/src/source.cpp +++ b/src/source.cpp @@ -99,7 +99,13 @@ SourceDistribution::SourceDistribution(pugi::xml_node node) } if (check_for_node(node, "serialization")) { - serialization = get_node_value(node, "serialization", false, true); + // If the source is serialized then make sure we only load it from file once, otherwise there will + // be a significant I/O overhead. + pugi::xml_document doc; + doc.load_file(get_node_value(node, "serialization", false, true).c_str()); + std::stringstream ss; + doc.print(ss); + serialization = ss.str(); } } else {