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

Side by Side Diff: src/parser.cc

Issue 765473006: Make TemplateLiteral hashing algorithm more memory efficient (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Change assertions in StringHasher extensions, add comment about hash key usage Created 6 years 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 | « src/objects-inl.h ('k') | 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 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/v8.h" 5 #include "src/v8.h"
6 6
7 #include "src/api.h" 7 #include "src/api.h"
8 #include "src/ast.h" 8 #include "src/ast.h"
9 #include "src/bailout-reason.h" 9 #include "src/bailout-reason.h"
10 #include "src/base/platform/platform.h" 10 #include "src/base/platform/platform.h"
(...skipping 5277 matching lines...) Expand 10 before | Expand all | Expand 10 after
5288 // with end-of-line sequences normalized to U+000A LINE FEEDs, and without 5288 // with end-of-line sequences normalized to U+000A LINE FEEDs, and without
5289 // leading or trailing template delimiters. 5289 // leading or trailing template delimiters.
5290 // 5290 //
5291 5291
5292 DCHECK(total); 5292 DCHECK(total);
5293 5293
5294 Handle<String> source(String::cast(script()->source())); 5294 Handle<String> source(String::cast(script()->source()));
5295 5295
5296 raw_strings = new (zone()) ZoneList<Expression*>(total, zone()); 5296 raw_strings = new (zone()) ZoneList<Expression*>(total, zone());
5297 5297
5298 int num_hash_chars = (total - 1) * 3; 5298 uint32_t running_hash = 0;
5299 for (int index = 0; index < total; ++index) {
5300 // Allow about length * 4 to handle most UTF8 sequences.
5301 num_hash_chars += lengths->at(index) * 4;
5302 }
5303
5304 Vector<uint8_t> hash_string = Vector<uint8_t>::New(num_hash_chars);
5305 num_hash_chars = 0;
5306 5299
5307 for (int index = 0; index < total; ++index) { 5300 for (int index = 0; index < total; ++index) {
5308 int span_start = cooked_strings->at(index)->position() + 1; 5301 int span_start = cooked_strings->at(index)->position() + 1;
5309 int span_end = lengths->at(index) - 1; 5302 int span_end = lengths->at(index) - 1;
5310 int length; 5303 int length;
5311 int to_index = 0; 5304 int to_index = 0;
5312 5305
5313 if (index) { 5306 if (index) {
5314 hash_string[num_hash_chars++] = '$'; 5307 running_hash = StringHasher::ComputeRunningHashOneByte(
5315 hash_string[num_hash_chars++] = '{'; 5308 running_hash, "${}", 3);
5316 hash_string[num_hash_chars++] = '}';
5317 } 5309 }
5318 5310
5319 SmartArrayPointer<char> raw_chars = 5311 SmartArrayPointer<char> raw_chars =
5320 source->ToCString(ALLOW_NULLS, FAST_STRING_TRAVERSAL, span_start, 5312 source->ToCString(ALLOW_NULLS, FAST_STRING_TRAVERSAL, span_start,
5321 span_end, &length); 5313 span_end, &length);
5322 5314
5323 // Normalize raw line-feeds. [U+000D U+000A] (CRLF) and [U+000D] (CR) must 5315 // Normalize raw line-feeds. [U+000D U+000A] (CRLF) and [U+000D] (CR) must
5324 // be translated into U+000A (LF). 5316 // be translated into U+000A (LF).
5325 for (int from_index = 0; from_index < length; ++from_index) { 5317 for (int from_index = 0; from_index < length; ++from_index) {
5326 char ch = raw_chars[from_index]; 5318 char ch = raw_chars[from_index];
5327 if (ch == '\r') { 5319 if (ch == '\r') {
5328 ch = '\n'; 5320 ch = '\n';
5329 if (from_index + 1 < length && raw_chars[from_index + 1] == '\n') { 5321 if (from_index + 1 < length && raw_chars[from_index + 1] == '\n') {
5330 ++from_index; 5322 ++from_index;
5331 } 5323 }
5332 } 5324 }
5333 hash_string[num_hash_chars++] = ch;
5334 raw_chars[to_index++] = ch; 5325 raw_chars[to_index++] = ch;
5335 } 5326 }
5336 5327
5337 Access<UnicodeCache::Utf8Decoder> 5328 Access<UnicodeCache::Utf8Decoder>
5338 decoder(isolate()->unicode_cache()->utf8_decoder()); 5329 decoder(isolate()->unicode_cache()->utf8_decoder());
5339 decoder->Reset(raw_chars.get(), to_index); 5330 decoder->Reset(raw_chars.get(), to_index);
5340 int utf16_length = decoder->Utf16Length(); 5331 int utf16_length = decoder->Utf16Length();
5341 Literal* raw_lit = NULL; 5332 Literal* raw_lit = NULL;
5342 if (utf16_length > 0) { 5333 if (utf16_length > 0) {
5343 uc16* utf16_buffer = zone()->NewArray<uc16>(utf16_length); 5334 uc16* utf16_buffer = zone()->NewArray<uc16>(utf16_length);
5344 to_index = decoder->WriteUtf16(utf16_buffer, utf16_length); 5335 to_index = decoder->WriteUtf16(utf16_buffer, utf16_length);
5336 running_hash = StringHasher::ComputeRunningHash(
5337 running_hash, utf16_buffer, to_index);
5345 const uint16_t* data = reinterpret_cast<const uint16_t*>(utf16_buffer); 5338 const uint16_t* data = reinterpret_cast<const uint16_t*>(utf16_buffer);
5346 const AstRawString* raw_str = ast_value_factory()->GetTwoByteString( 5339 const AstRawString* raw_str = ast_value_factory()->GetTwoByteString(
5347 Vector<const uint16_t>(data, to_index)); 5340 Vector<const uint16_t>(data, to_index));
5348 raw_lit = factory()->NewStringLiteral(raw_str, span_start - 1); 5341 raw_lit = factory()->NewStringLiteral(raw_str, span_start - 1);
5349 } else { 5342 } else {
5350 raw_lit = factory()->NewStringLiteral( 5343 raw_lit = factory()->NewStringLiteral(
5351 ast_value_factory()->empty_string(), span_start - 1); 5344 ast_value_factory()->empty_string(), span_start - 1);
5352 } 5345 }
5353 DCHECK_NOT_NULL(raw_lit); 5346 DCHECK_NOT_NULL(raw_lit);
5354 raw_strings->Add(raw_lit, zone()); 5347 raw_strings->Add(raw_lit, zone());
5355 } 5348 }
5356 5349
5357 hash_string.Truncate(num_hash_chars); 5350 // Hash key is used exclusively by template call site caching. There are no
5358 int utf16_length; 5351 // real security implications for unseeded hashes, and no issues with changing
5359 *hash = StringHasher::ComputeUtf8Hash(Vector<const char>::cast(hash_string), 5352 // the hashing algorithm to improve performance or entropy.
5360 num_hash_chars, &utf16_length); 5353 *hash = running_hash;
5361 hash_string.Dispose();
5362 5354
5363 return raw_strings; 5355 return raw_strings;
5364 } 5356 }
5365 } } // namespace v8::internal 5357 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/objects-inl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698