Skip to content

Commit

Permalink
Apply a short-vector optimization to EvalString.
Browse files Browse the repository at this point in the history
This very often holds only a single RAW token, so we do not
need to allocate elements on an std::vector for it in the
common case.

For a no-op build of Chromium (Linux, Zen 2),
this reduces time spent from 5.48 to 5.14 seconds.

Note that this opens up for a potential optimization where
EvalString::Evaluate() could just return a StringPiece, without
making a std::string out of it (which requires allocation; this is
about 5% of remaining runtime). However, this would also require
that CanonicalizePath() somehow learned to work with StringPiece
(presumably allocating a new StringPiece if and only if changes
were needed).
  • Loading branch information
Steinar H. Gunderson committed Nov 5, 2024
1 parent d85cfed commit 40efd00
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 20 deletions.
57 changes: 39 additions & 18 deletions src/eval_env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ string BindingEnv::LookupWithFallback(const string& var,
}

string EvalString::Evaluate(Env* env) const {
if (parsed_.empty()) {
return single_token_;
}

string result;
for (TokenList::const_iterator i = parsed_.begin(); i != parsed_.end(); ++i) {
if (i->second == RAW)
Expand All @@ -110,40 +114,57 @@ string EvalString::Evaluate(Env* env) const {
}

void EvalString::AddText(StringPiece text) {
// Add it to the end of an existing RAW token if possible.
if (!parsed_.empty() && parsed_.back().second == RAW) {
parsed_.back().first.append(text.str_, text.len_);
if (parsed_.empty()) {
single_token_.append(text.begin(), text.end());
} else if (!parsed_.empty() && parsed_.back().second == RAW) {
parsed_.back().first.append(text.begin(), text.end());
} else {
parsed_.push_back(make_pair(text.AsString(), RAW));
parsed_.push_back(std::make_pair(text.AsString(), RAW));
}
}

void EvalString::AddSpecial(StringPiece text) {
parsed_.push_back(make_pair(text.AsString(), SPECIAL));
if (parsed_.empty() && !single_token_.empty()) {
// Going from one to two tokens, so we can no longer apply
// our single_token_ optimization and need to push everything
// onto the vector.
parsed_.push_back(std::make_pair(std::move(single_token_), RAW));
}
parsed_.push_back(std::make_pair(text.AsString(), SPECIAL));
}

string EvalString::Serialize() const {
string result;
for (TokenList::const_iterator i = parsed_.begin();
i != parsed_.end(); ++i) {
if (parsed_.empty() && !single_token_.empty()) {
result.append("[");
if (i->second == SPECIAL)
result.append("$");
result.append(i->first);
result.append(single_token_);
result.append("]");
} else {
for (const auto& pair : parsed_) {
result.append("[");
if (pair.second == SPECIAL)
result.append("$");
result.append(pair.first.begin(), pair.first.end());
result.append("]");
}
}
return result;
}

string EvalString::Unparse() const {
string result;
for (TokenList::const_iterator i = parsed_.begin();
i != parsed_.end(); ++i) {
bool special = (i->second == SPECIAL);
if (special)
result.append("${");
result.append(i->first);
if (special)
result.append("}");
if (parsed_.empty() && !single_token_.empty()) {
result.append(single_token_.begin(), single_token_.end());
} else {
for (TokenList::const_iterator i = parsed_.begin();
i != parsed_.end(); ++i) {
bool special = (i->second == SPECIAL);
if (special)
result.append("${");
result.append(i->first.begin(), i->first.end());
if (special)
result.append("}");
}
}
return result;
}
10 changes: 8 additions & 2 deletions src/eval_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ struct EvalString {
/// @return The string with variables not expanded.
std::string Unparse() const;

void Clear() { parsed_.clear(); }
bool empty() const { return parsed_.empty(); }
void Clear() { parsed_.clear(); single_token_.clear(); }
bool empty() const { return parsed_.empty() && single_token_.empty(); }

void AddText(StringPiece text);
void AddSpecial(StringPiece text);
Expand All @@ -53,6 +53,12 @@ struct EvalString {
enum TokenType { RAW, SPECIAL };
typedef std::vector<std::pair<std::string, TokenType> > TokenList;
TokenList parsed_;

// If we hold only a single RAW token, then we keep it here instead of
// pushing it on TokenList. This saves a bunch of allocations for
// what is a common case. If parsed_ is nonempty, then this value
// must be ignored.
std::string single_token_;
};

/// An invocable build command and associated metadata (description, etc.).
Expand Down
4 changes: 4 additions & 0 deletions src/string_piece.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ struct StringPiece {
return len_;
}

size_t empty() const {
return len_ == 0;
}

const char* str_;
size_t len_;
};
Expand Down

0 comments on commit 40efd00

Please sign in to comment.