Improve performance of writing XML

As with the JSON writer, the old code was made to be simple and
for each char just decided whether it needs escaping, or should be
written as-is. The new code instead looks for characters that need
escaping and batches writes of characters that do not.

This provides 4-8x speedup (length dependent) for writing strings
that do not need escaping, and keeps roughly the same performance
for those that do need escaping.
This commit is contained in:
Martin Hořeňovský
2025-08-22 17:03:31 +02:00
parent fb2e4fbe41
commit f4e05a67bb
2 changed files with 84 additions and 48 deletions

View File

@@ -7,13 +7,16 @@
// SPDX-License-Identifier: BSL-1.0
#include <catch2/catch_test_macros.hpp>
#include <catch2/internal/catch_xmlwriter.hpp>
#include <catch2/benchmark/catch_benchmark.hpp>
#include <catch2/generators/catch_generators.hpp>
#include <catch2/internal/catch_reusable_string_stream.hpp>
#include <catch2/internal/catch_xmlwriter.hpp>
#include <catch2/matchers/catch_matchers_string.hpp>
#include <sstream>
namespace {
static std::string encode( std::string const& str, Catch::XmlEncode::ForWhat forWhat = Catch::XmlEncode::ForTextNodes ) {
Catch::ReusableStringStream oss;
oss << Catch::XmlEncode( str, forWhat );
@@ -181,3 +184,20 @@ TEST_CASE("XmlWriter escapes attributes properly", "[XML][XmlWriter][approvals]"
REQUIRE_THAT(stream.str(),
ContainsSubstring(R"(some-attribute="Special chars need escaping: &lt; > ' &quot; &amp;")"));
}
TEST_CASE( "XmlWriter benchmarks", "[XML][XmlWriter][!benchmark]" ) {
const auto input_length = GENERATE( as<size_t>{}, 10, 100, 10'000 );
std::string test_input( input_length, 'a' );
BENCHMARK_ADVANCED( "write string, no-escaping, len=" +
std::to_string( input_length ) ) {
return encode( test_input );
};
std::string escape_input( input_length, '\b' );
BENCHMARK_ADVANCED( "write string, all-escaped, len=" +
std::to_string( input_length ) ) {
return encode( escape_input );
};
}
} // namespace