Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(301)

Side by Side Diff: runtime/vm/parser.cc

Issue 1327003003: Cache interpolated constant strings (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Add a comment Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/parser.h" 5 #include "vm/parser.h"
6 6
7 #include "lib/invocation_mirror.h" 7 #include "lib/invocation_mirror.h"
8 #include "platform/utils.h" 8 #include "platform/utils.h"
9 #include "vm/ast_transformer.h" 9 #include "vm/ast_transformer.h"
10 #include "vm/bootstrap.h" 10 #include "vm/bootstrap.h"
(...skipping 13571 matching lines...) Expand 10 before | Expand all | Expand 10 after
13582 Token::Kind l1_token = LookaheadToken(1); 13582 Token::Kind l1_token = LookaheadToken(1);
13583 if ((l1_token != Token::kSTRING) && 13583 if ((l1_token != Token::kSTRING) &&
13584 (l1_token != Token::kINTERPOL_VAR) && 13584 (l1_token != Token::kINTERPOL_VAR) &&
13585 (l1_token != Token::kINTERPOL_START)) { 13585 (l1_token != Token::kINTERPOL_START)) {
13586 // Common case: no interpolation. 13586 // Common case: no interpolation.
13587 primary = new(Z) LiteralNode(literal_start, *CurrentLiteral()); 13587 primary = new(Z) LiteralNode(literal_start, *CurrentLiteral());
13588 ConsumeToken(); 13588 ConsumeToken();
13589 return primary; 13589 return primary;
13590 } 13590 }
13591 // String interpolation needed. 13591 // String interpolation needed.
13592
13593 // First, check whether we've cached a compile-time constant for this
13594 // string interpolation.
13595 Instance& cached_string = Instance::Handle(Z);
13596 if (GetCachedConstant(literal_start, &cached_string)) {
13597 SkipStringLiteral();
13598 return new(Z) LiteralNode(literal_start,
13599 Instance::ZoneHandle(Z, cached_string.raw()));
13600 }
13601
13592 bool is_compiletime_const = true; 13602 bool is_compiletime_const = true;
13593 bool has_interpolation = false; 13603 bool has_interpolation = false;
13594 GrowableArray<AstNode*> values_list; 13604 GrowableArray<AstNode*> values_list;
13595 while (CurrentToken() == Token::kSTRING) { 13605 while (CurrentToken() == Token::kSTRING) {
13596 if (CurrentLiteral()->Length() > 0) { 13606 if (CurrentLiteral()->Length() > 0) {
13597 // Only add non-empty string sections to the values list 13607 // Only add non-empty string sections to the values list
13598 // that will be concatenated. 13608 // that will be concatenated.
13599 values_list.Add(new(Z) LiteralNode(TokenPos(), *CurrentLiteral())); 13609 values_list.Add(new(Z) LiteralNode(TokenPos(), *CurrentLiteral()));
13600 } 13610 }
13601 ConsumeToken(); 13611 ConsumeToken();
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
13634 EvaluateConstExpr(expr_pos, expr)); 13644 EvaluateConstExpr(expr_pos, expr));
13635 } else { 13645 } else {
13636 is_compiletime_const = false; 13646 is_compiletime_const = false;
13637 } 13647 }
13638 } 13648 }
13639 values_list.Add(expr); 13649 values_list.Add(expr);
13640 } 13650 }
13641 } 13651 }
13642 if (is_compiletime_const) { 13652 if (is_compiletime_const) {
13643 if (has_interpolation) { 13653 if (has_interpolation) {
13644 primary = new(Z) LiteralNode(literal_start, Interpolate(values_list)); 13654 const String& interpolated_string = Interpolate(values_list);
13655 primary = new(Z) LiteralNode(literal_start, interpolated_string);
13656 CacheConstantValue(literal_start, interpolated_string);
13645 } else { 13657 } else {
13646 GrowableHandlePtrArray<const String> pieces(Z, values_list.length()); 13658 GrowableHandlePtrArray<const String> pieces(Z, values_list.length());
13647 for (int i = 0; i < values_list.length(); i++) { 13659 for (int i = 0; i < values_list.length(); i++) {
13648 const Instance& part = values_list[i]->AsLiteralNode()->literal(); 13660 const Instance& part = values_list[i]->AsLiteralNode()->literal();
13649 ASSERT(part.IsString()); 13661 ASSERT(part.IsString());
13650 pieces.Add(String::Cast(part)); 13662 pieces.Add(String::Cast(part));
13651 } 13663 }
13652 const String& lit = String::ZoneHandle(Z, Symbols::FromConcatAll(pieces)); 13664 const String& lit = String::ZoneHandle(Z, Symbols::FromConcatAll(pieces));
13653 primary = new(Z) LiteralNode(literal_start, lit); 13665 primary = new(Z) LiteralNode(literal_start, lit);
13666 // Caching of constant not necessary because the symbol lookup will
13667 // find the value next time.
13654 } 13668 }
13655 } else { 13669 } else {
13656 ArrayNode* values = new(Z) ArrayNode( 13670 ArrayNode* values = new(Z) ArrayNode(
13657 TokenPos(), 13671 TokenPos(),
13658 Type::ZoneHandle(Z, Type::ArrayType()), 13672 Type::ZoneHandle(Z, Type::ArrayType()),
13659 values_list); 13673 values_list);
13660 primary = new(Z) StringInterpolateNode(TokenPos(), values); 13674 primary = new(Z) StringInterpolateNode(TokenPos(), values);
13661 } 13675 }
13662 return primary; 13676 return primary;
13663 } 13677 }
(...skipping 596 matching lines...) Expand 10 before | Expand all | Expand 10 after
14260 void Parser::SkipQualIdent() { 14274 void Parser::SkipQualIdent() {
14261 ASSERT(IsIdentifier()); 14275 ASSERT(IsIdentifier());
14262 ConsumeToken(); 14276 ConsumeToken();
14263 if (CurrentToken() == Token::kPERIOD) { 14277 if (CurrentToken() == Token::kPERIOD) {
14264 ConsumeToken(); // Consume the kPERIOD token. 14278 ConsumeToken(); // Consume the kPERIOD token.
14265 ExpectIdentifier("identifier expected after '.'"); 14279 ExpectIdentifier("identifier expected after '.'");
14266 } 14280 }
14267 } 14281 }
14268 14282
14269 } // namespace dart 14283 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698