Faster saving and hashing
- XML text is printed straight from the node tree by a Printer that matches tinyxml2's XMLPrinter byte for byte; no tinyxml2 document is built to save. - Numbers format with std::to_chars (whole numbers as integers) instead of snprintf, with the same text as "%.8g". - Frame attribute and track child vectors are reserved up front. - Repeated attributes in a read file collapse as tinyxml2 wrote them. Corpus (1,251 files, Release): save 4.2 s -> 0.77 s, extended save 0.61 s -> 0.12 s, hash 0.28 s -> 0.13 s; every output is byte-identical. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c4b21fd90c
commit
210232eb83
4 files changed
+142
-22
No files matched your search
@@ -36,6 +36,7 @@ namespace anm2ed::imgui::wizard
|
|||||||
{"Design/Programming", font::BOLD},
|
{"Design/Programming", font::BOLD},
|
||||||
{"Shweet"},
|
{"Shweet"},
|
||||||
{"OpenAI Codex"},
|
{"OpenAI Codex"},
|
||||||
|
{"Claude Code"},
|
||||||
{""},
|
{""},
|
||||||
{"Additional Help", font::BOLD},
|
{"Additional Help", font::BOLD},
|
||||||
{"im-tem"},
|
{"im-tem"},
|
||||||
|
|||||||
+125
-22
@@ -1,6 +1,7 @@
|
|||||||
#include "xml.hpp"
|
#include "xml.hpp"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <charconv>
|
||||||
#include <format>
|
#include <format>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
@@ -45,6 +46,14 @@ namespace anm2ed::model
|
|||||||
constexpr const char* CREATED_ON_FORMAT = "%m/%d/%Y %I:%M:%S %p";
|
constexpr const char* CREATED_ON_FORMAT = "%m/%d/%Y %I:%M:%S %p";
|
||||||
constexpr const char* SHADER_NAME_DEFAULT = "New Shader";
|
constexpr const char* SHADER_NAME_DEFAULT = "New Shader";
|
||||||
constexpr std::size_t VALUE_BUFFER_SIZE = 64;
|
constexpr std::size_t VALUE_BUFFER_SIZE = 64;
|
||||||
|
constexpr int FLOAT_DIGITS = 8;
|
||||||
|
constexpr int DOUBLE_DIGITS = 17;
|
||||||
|
constexpr double FLOAT_WHOLE_MAX = 1e8;
|
||||||
|
constexpr std::size_t FRAME_ATTRIBUTES_MAX = 27;
|
||||||
|
constexpr std::string_view INDENT = " ";
|
||||||
|
constexpr std::pair<char, std::string_view> ENTITIES[] = {
|
||||||
|
{'&', "&"}, {'<', "<"}, {'>', ">"}, {'"', """}, {'\'', "'"}};
|
||||||
|
constexpr int TEXT_ENTITY_COUNT = 3;
|
||||||
constexpr std::uint64_t HASH_OFFSET = 14695981039346656037ull;
|
constexpr std::uint64_t HASH_OFFSET = 14695981039346656037ull;
|
||||||
constexpr std::uint64_t HASH_PRIME = 1099511628211ull;
|
constexpr std::uint64_t HASH_PRIME = 1099511628211ull;
|
||||||
constexpr Flags CLIPBOARD_FLAGS = SERIALIZE_EDITOR_DEFAULT;
|
constexpr Flags CLIPBOARD_FLAGS = SERIALIZE_EDITOR_DEFAULT;
|
||||||
@@ -150,11 +159,22 @@ namespace anm2ed::model
|
|||||||
if (is_child_kept(parent, child)) callback(child, type_get(child), position++);
|
if (is_child_kept(parent, child)) callback(child, type_get(child), position++);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A repeated attribute keeps its first position and last value, as tinyxml2 writes it.
|
||||||
|
void attribute_read(std::vector<std::pair<std::string, std::string>>& attributes, const XMLAttribute* attribute)
|
||||||
|
{
|
||||||
|
auto existing = std::ranges::find(attributes, std::string_view(attribute->Name()),
|
||||||
|
[](const auto& pair) { return std::string_view(pair.first); });
|
||||||
|
if (existing != attributes.end())
|
||||||
|
existing->second = attribute->Value();
|
||||||
|
else
|
||||||
|
attributes.emplace_back(attribute->Name(), attribute->Value());
|
||||||
|
}
|
||||||
|
|
||||||
XmlNode node_read(const XMLElement* element)
|
XmlNode node_read(const XMLElement* element)
|
||||||
{
|
{
|
||||||
XmlNode node{.tag = element->Name()};
|
XmlNode node{.tag = element->Name()};
|
||||||
for (auto attribute = element->FirstAttribute(); attribute; attribute = attribute->Next())
|
for (auto attribute = element->FirstAttribute(); attribute; attribute = attribute->Next())
|
||||||
node.attributes.emplace_back(attribute->Name(), attribute->Value());
|
attribute_read(node.attributes, attribute);
|
||||||
if (auto text = element->GetText()) node.text = text;
|
if (auto text = element->GetText()) node.text = text;
|
||||||
children_each(element,
|
children_each(element,
|
||||||
[&](const XMLElement* child, ElementType, int) { node.children.push_back(node_read(child)); });
|
[&](const XMLElement* child, ElementType, int) { node.children.push_back(node_read(child)); });
|
||||||
@@ -166,7 +186,7 @@ namespace anm2ed::model
|
|||||||
Extras extras{};
|
Extras extras{};
|
||||||
for (auto attribute = element->FirstAttribute(); attribute; attribute = attribute->Next())
|
for (auto attribute = element->FirstAttribute(); attribute; attribute = attribute->Next())
|
||||||
if (!std::ranges::contains(KNOWN_ATTRIBUTES, std::string_view(attribute->Name())))
|
if (!std::ranges::contains(KNOWN_ATTRIBUTES, std::string_view(attribute->Name())))
|
||||||
extras.attributes.emplace_back(attribute->Name(), attribute->Value());
|
attribute_read(extras.attributes, attribute);
|
||||||
if (auto text = element->GetText()) extras.text = text;
|
if (auto text = element->GetText()) extras.text = text;
|
||||||
return extras;
|
return extras;
|
||||||
}
|
}
|
||||||
@@ -776,15 +796,31 @@ namespace anm2ed::model
|
|||||||
return it == ids.end() ? -1 : it->second;
|
return it == ids.end() ? -1 : it->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool is_float_whole(double value)
|
||||||
|
{
|
||||||
|
return std::abs(value) < FLOAT_WHOLE_MAX && value == std::trunc(value) && !(value == 0 && std::signbit(value));
|
||||||
|
}
|
||||||
|
|
||||||
template <class T> void attribute_add(XmlNode& node, const char* name, const T& value)
|
template <class T> void attribute_add(XmlNode& node, const char* name, const T& value)
|
||||||
{
|
{
|
||||||
if constexpr (std::is_convertible_v<T, std::string_view>)
|
if constexpr (std::is_convertible_v<T, std::string_view>)
|
||||||
node.attributes.emplace_back(name, std::string(std::string_view(value)));
|
node.attributes.emplace_back(name, std::string(std::string_view(value)));
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
// The same text tinyxml2's XMLUtil::ToStr prints ("%d", "%.8g", "true"/"false"), without printf.
|
||||||
char buffer[VALUE_BUFFER_SIZE]{};
|
char buffer[VALUE_BUFFER_SIZE]{};
|
||||||
XMLUtil::ToStr(value, buffer, sizeof(buffer));
|
auto end = buffer;
|
||||||
node.attributes.emplace_back(name, buffer);
|
if constexpr (std::is_same_v<T, bool>)
|
||||||
|
end = std::ranges::copy(std::string_view(value ? "true" : "false"), buffer).out;
|
||||||
|
else if constexpr (std::is_floating_point_v<T>)
|
||||||
|
// Whole numbers (most values) print as integers, which "%.8g" also does below 1e8 (but keeps "-0").
|
||||||
|
end = is_float_whole(value) ? std::to_chars(buffer, buffer + sizeof(buffer), (long long)value).ptr
|
||||||
|
: std::to_chars(buffer, buffer + sizeof(buffer), value, std::chars_format::general,
|
||||||
|
std::is_same_v<T, float> ? FLOAT_DIGITS : DOUBLE_DIGITS)
|
||||||
|
.ptr;
|
||||||
|
else
|
||||||
|
end = std::to_chars(buffer, buffer + sizeof(buffer), (long long)value).ptr;
|
||||||
|
node.attributes.emplace_back(name, std::string(buffer, end));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -858,6 +894,7 @@ namespace anm2ed::model
|
|||||||
}
|
}
|
||||||
|
|
||||||
XmlNode node{.tag = tag_get(ElementType::FRAME)};
|
XmlNode node{.tag = tag_get(ElementType::FRAME)};
|
||||||
|
node.attributes.reserve(FRAME_ATTRIBUTES_MAX + frame.extras.attributes.size());
|
||||||
if (type == ItemType::LAYER)
|
if (type == ItemType::LAYER)
|
||||||
{
|
{
|
||||||
auto isRegion = writer.is(SERIALIZE_REGIONS) && frame.regionId != -1;
|
auto isRegion = writer.is(SERIALIZE_REGIONS) && frame.regionId != -1;
|
||||||
@@ -909,6 +946,7 @@ namespace anm2ed::model
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto layerTrack = track.type == ItemType::LAYER && !isBackup ? &track : nullptr;
|
auto layerTrack = track.type == ItemType::LAYER && !isBackup ? &track : nullptr;
|
||||||
|
node.children.reserve(track.frames.size());
|
||||||
for (int i = 0; i < (int)track.frames.size(); ++i)
|
for (int i = 0; i < (int)track.frames.size(); ++i)
|
||||||
{
|
{
|
||||||
auto frame = frame_normalize(writer, track.frames[i], layerTrack);
|
auto frame = frame_normalize(writer, track.frames[i], layerTrack);
|
||||||
@@ -1321,24 +1359,90 @@ namespace anm2ed::model
|
|||||||
return game;
|
return game;
|
||||||
}
|
}
|
||||||
|
|
||||||
XMLElement* element_write(XMLDocument& document, const XmlNode& node)
|
// Prints a node tree exactly as tinyxml2's XMLPrinter would (4-space indents, entities escaped, text keeping its
|
||||||
|
// element's children inline), without building a tinyxml2 document.
|
||||||
|
struct Printer
|
||||||
{
|
{
|
||||||
auto element = document.NewElement(node.tag.c_str());
|
std::string text{};
|
||||||
for (const auto& [name, value] : node.attributes)
|
int depth{};
|
||||||
element->SetAttribute(name.c_str(), value.c_str());
|
int textDepth{-1};
|
||||||
if (!node.text.empty()) element->SetText(node.text.c_str());
|
bool isFirst{true};
|
||||||
for (const auto& child : node.children)
|
|
||||||
element->InsertEndChild(element_write(document, child));
|
// Attributes escape all five entities; text only the first three.
|
||||||
return element;
|
void escape(std::string_view value, bool isAttribute)
|
||||||
}
|
{
|
||||||
|
for (auto character : value)
|
||||||
|
{
|
||||||
|
auto entity = std::ranges::find(ENTITIES, character, &std::pair<char, std::string_view>::first);
|
||||||
|
auto isEscaped = entity != std::end(ENTITIES) && (isAttribute || entity - ENTITIES < TEXT_ENTITY_COUNT);
|
||||||
|
if (isEscaped)
|
||||||
|
text += entity->second;
|
||||||
|
else
|
||||||
|
text += character;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void indent()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < depth; ++i)
|
||||||
|
text += INDENT;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print(const XmlNode& node)
|
||||||
|
{
|
||||||
|
if (!isFirst && textDepth < 0) text += '\n';
|
||||||
|
if (isFirst || textDepth < 0) indent();
|
||||||
|
isFirst = false;
|
||||||
|
|
||||||
|
text += '<';
|
||||||
|
text += node.tag;
|
||||||
|
++depth;
|
||||||
|
for (const auto& [name, value] : node.attributes)
|
||||||
|
{
|
||||||
|
text += ' ';
|
||||||
|
text += name;
|
||||||
|
text += "=\"";
|
||||||
|
escape(value, true);
|
||||||
|
text += '"';
|
||||||
|
}
|
||||||
|
auto isOpen = true;
|
||||||
|
if (!node.text.empty())
|
||||||
|
{
|
||||||
|
textDepth = depth - 1;
|
||||||
|
text += '>';
|
||||||
|
isOpen = false;
|
||||||
|
escape(node.text, false);
|
||||||
|
}
|
||||||
|
for (const auto& child : node.children)
|
||||||
|
{
|
||||||
|
if (std::exchange(isOpen, false)) text += '>';
|
||||||
|
print(child);
|
||||||
|
}
|
||||||
|
|
||||||
|
--depth;
|
||||||
|
if (isOpen)
|
||||||
|
text += "/>";
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (textDepth < 0)
|
||||||
|
{
|
||||||
|
text += '\n';
|
||||||
|
indent();
|
||||||
|
}
|
||||||
|
text += "</";
|
||||||
|
text += node.tag;
|
||||||
|
text += '>';
|
||||||
|
}
|
||||||
|
if (textDepth == depth) textDepth = -1;
|
||||||
|
if (depth == 0) text += '\n';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
std::string node_to_string(const XmlNode& node)
|
std::string node_to_string(const XmlNode& node)
|
||||||
{
|
{
|
||||||
XMLDocument document{};
|
Printer printer{};
|
||||||
document.InsertEndChild(element_write(document, node));
|
printer.print(node);
|
||||||
XMLPrinter printer{};
|
return std::move(printer.text);
|
||||||
document.Print(&printer);
|
|
||||||
return printer.CStr();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void node_hash(std::uint64_t& hash, const XmlNode& node)
|
void node_hash(std::uint64_t& hash, const XmlNode& node)
|
||||||
@@ -1403,16 +1507,15 @@ namespace anm2ed::model
|
|||||||
|
|
||||||
bool model_save(const Model& model, const std::filesystem::path& path, std::string* errorString, Options options)
|
bool model_save(const Model& model, const std::filesystem::path& path, std::string* errorString, Options options)
|
||||||
{
|
{
|
||||||
XMLDocument document{};
|
auto text = model_to_string(model, options);
|
||||||
document.InsertFirstChild(element_write(document, document_write(model, options)));
|
|
||||||
File file(path, "wb");
|
File file(path, "wb");
|
||||||
if (!file)
|
if (!file)
|
||||||
{
|
{
|
||||||
if (errorString) *errorString = "File permissions.";
|
if (errorString) *errorString = "File permissions.";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (document.SaveFile(file.get()) == XML_SUCCESS) return true;
|
if (std::fwrite(text.data(), 1, text.size(), file.get()) == text.size()) return true;
|
||||||
if (errorString) *errorString = document.ErrorStr();
|
if (errorString) *errorString = "Could not write the file.";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,3 +11,19 @@ TEST_CASE("per-fixture invariants")
|
|||||||
invariants_check(model_load(file_load(fixture)));
|
invariants_check(model_load(file_load(fixture)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("saved text is what tinyxml2 prints for the same document")
|
||||||
|
{
|
||||||
|
auto model = model_load(file_load(CORPUS_DIR / "11_unknown.anm2"));
|
||||||
|
model.extras.children.emplace_back(
|
||||||
|
0, model::XmlNode{.tag = "Note",
|
||||||
|
.attributes = {{"Quote", "it's \"x\" & <y>"}},
|
||||||
|
.text = "a & b > c 'd' \"e\"",
|
||||||
|
.children = {{.tag = "Inner", .children = {{.tag = "Leaf", .attributes = {{"A", "1"}}}}}}});
|
||||||
|
auto text = model::model_to_string(model);
|
||||||
|
tinyxml2::XMLDocument document{};
|
||||||
|
REQUIRE(document.Parse(text.c_str()) == tinyxml2::XML_SUCCESS);
|
||||||
|
tinyxml2::XMLPrinter printer{};
|
||||||
|
document.Print(&printer);
|
||||||
|
CHECK(text == printer.CStr());
|
||||||
|
}
|
||||||
Reference in new issue
Block a user