Skip to content

Commit

Permalink
S-Expression: Minimize lag when parsing level names
Browse files Browse the repository at this point in the history
`sexp::Parser` now supports specifying a custom `depth` property for omitting (not parsing) structures deeper than `depth` in the tree, starting from 0. This optimizes parsing level names from level files a ton, since level files tend to be very big and used to take a while to parse collectively.
  • Loading branch information
Vankata453 committed Nov 17, 2024
1 parent 00d2ff9 commit f74b4ec
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/supertux/level_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ LevelParser::get_level_name(const std::string& filename)
try
{
register_translation_directory(filename);
auto doc = ReaderDocument::from_file(filename);
auto doc = ReaderDocument::from_file(filename, 1);
auto root = doc.get_root();

if (root.get_name() != "supertux-level") {
Expand Down
15 changes: 11 additions & 4 deletions src/util/reader_document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,21 @@
#include "util/log.hpp"

ReaderDocument
ReaderDocument::from_stream(std::istream& stream, const std::string& filename)
ReaderDocument::from_string(const std::string& string, const std::string& filename, int depth)
{
sexp::Value sx = sexp::Parser::from_stream(stream, sexp::Parser::USE_ARRAYS);
std::istringstream stream(string);
return from_stream(stream, filename, depth);
}

ReaderDocument
ReaderDocument::from_stream(std::istream& stream, const std::string& filename, int depth)
{
sexp::Value sx = sexp::Parser::from_stream(stream, sexp::Parser::USE_ARRAYS, depth);
return ReaderDocument(filename, std::move(sx));
}

ReaderDocument
ReaderDocument::from_file(const std::string& filename)
ReaderDocument::from_file(const std::string& filename, int depth)
{
log_debug << "ReaderDocument::parse: " << filename << std::endl;

Expand All @@ -41,7 +48,7 @@ ReaderDocument::from_file(const std::string& filename)
msg << "Parser problem: Couldn't open file '" << filename << "'.";
throw std::runtime_error(msg.str());
} else {
return from_stream(in, filename);
return from_stream(in, filename, depth);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/util/reader_document.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
class ReaderDocument final
{
public:
static ReaderDocument from_stream(std::istream& stream, const std::string& filename = "<stream>");
static ReaderDocument from_file(const std::string& filename);
static ReaderDocument from_string(const std::string& string, const std::string& filename = "<string>", int depth = -1);
static ReaderDocument from_stream(std::istream& stream, const std::string& filename = "<stream>", int depth = -1);
static ReaderDocument from_file(const std::string& filename, int depth = -1);

public:
ReaderDocument(const std::string& filename, sexp::Value sx);
Expand Down
6 changes: 1 addition & 5 deletions src/util/reader_mapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,7 @@ ReaderMapping::get(const char* key, std::string& value, const std::optional<cons
if (item[1].is_string()) {
value = item[1].as_string();
return true;
} else if (item[1].is_array() &&
item[1].as_array().size() == 2 &&
item[1].as_array()[0].is_symbol() &&
item[1].as_array()[0].as_string() == "_" &&
item[1].as_array()[1].is_string()) {
} else if (item[1].is_translatable_string()) {
if (s_translations_enabled) {
value = _(item[1].as_array()[1].as_string());
} else {
Expand Down

0 comments on commit f74b4ec

Please sign in to comment.