Change StringLiteral to less frequently allocate a new string. (#3314)

Building on #3311, which started moving the result string into a
`unique_ptr`, instead have `StringLiteral` use a `BumpPtrAllocator` to
manage memory. But also, detect when a string is really trivial during
`Lex` and, if so, return `contents_` directly.
This commit is contained in:
Jon Ross-Perkins
2023-10-24 19:15:49 +00:00
committed by GitHub
parent 1d6298290f
commit b2cfd5a8a8
7 changed files with 149 additions and 76 deletions
+1 -5
View File
@@ -652,12 +652,8 @@ class [[clang::internal_linkage]] TokenizedBuffer::Lexer {
}
if (literal->is_terminated()) {
// TODO: Refactor to reduce copies.
// https://github.com/carbon-language/carbon-lang/pull/3311#discussion_r1366048360
buffer_.computed_strings_.push_back(
std::make_unique<std::string>(literal->ComputeValue(emitter_)));
auto string_id = buffer_.value_stores_->strings().Add(
*buffer_.computed_strings_.back());
literal->ComputeValue(buffer_.allocator_, emitter_));
auto token = buffer_.AddToken({.kind = TokenKind::StringLiteral,
.token_line = string_line,
.column = string_column,