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

Side by Side Diff: third_party/WebKit/Source/wtf/text/AtomicString.cpp

Issue 1844223002: Literal AtomicString construction can rely on strlen optimization. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2013 Apple Inc. All rights reserv ed. 2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2013 Apple Inc. All rights reserv ed.
3 * Copyright (C) 2010 Patrick Gansterer <paroga@paroga.com> 3 * Copyright (C) 2010 Patrick Gansterer <paroga@paroga.com>
4 * Copyright (C) 2012 Google Inc. All rights reserved. 4 * Copyright (C) 2012 Google Inc. All rights reserved.
5 * 5 *
6 * This library is free software; you can redistribute it and/or 6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public 7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either 8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version. 9 * version 2 of the License, or (at your option) any later version.
10 * 10 *
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 template<typename T, typename HashTranslator> 118 template<typename T, typename HashTranslator>
119 static inline PassRefPtr<StringImpl> addToStringTable(const T& value) 119 static inline PassRefPtr<StringImpl> addToStringTable(const T& value)
120 { 120 {
121 HashSet<StringImpl*>::AddResult addResult = atomicStrings().addWithTranslato r<HashTranslator>(value); 121 HashSet<StringImpl*>::AddResult addResult = atomicStrings().addWithTranslato r<HashTranslator>(value);
122 122
123 // If the string is newly-translated, then we need to adopt it. 123 // If the string is newly-translated, then we need to adopt it.
124 // The boolean in the pair tells us if that is so. 124 // The boolean in the pair tells us if that is so.
125 return addResult.isNewEntry ? adoptRef(*addResult.storedValue) : *addResult. storedValue; 125 return addResult.isNewEntry ? adoptRef(*addResult.storedValue) : *addResult. storedValue;
126 } 126 }
127 127
128 PassRefPtr<StringImpl> AtomicString::add(const LChar* c)
129 {
130 if (!c)
131 return nullptr;
132 if (!*c)
133 return StringImpl::empty();
134
135 return add(c, strlen(reinterpret_cast<const char*>(c)));
136 }
137
138 template<typename CharacterType> 128 template<typename CharacterType>
139 struct HashTranslatorCharBuffer { 129 struct HashTranslatorCharBuffer {
140 const CharacterType* s; 130 const CharacterType* s;
141 unsigned length; 131 unsigned length;
142 }; 132 };
143 133
144 typedef HashTranslatorCharBuffer<UChar> UCharBuffer; 134 typedef HashTranslatorCharBuffer<UChar> UCharBuffer;
145 struct UCharBufferTranslator { 135 struct UCharBufferTranslator {
146 static unsigned hash(const UCharBuffer& buf) 136 static unsigned hash(const UCharBuffer& buf)
147 { 137 {
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 } 350 }
361 351
362 static void translate(StringImpl*& location, const LCharBuffer& buf, unsigne d hash) 352 static void translate(StringImpl*& location, const LCharBuffer& buf, unsigne d hash)
363 { 353 {
364 location = StringImpl::create(buf.s, buf.length).leakRef(); 354 location = StringImpl::create(buf.s, buf.length).leakRef();
365 location->setHash(hash); 355 location->setHash(hash);
366 location->setIsAtomic(true); 356 location->setIsAtomic(true);
367 } 357 }
368 }; 358 };
369 359
370 typedef HashTranslatorCharBuffer<char> CharBuffer;
371 struct CharBufferFromLiteralDataTranslator {
372 static unsigned hash(const CharBuffer& buf)
373 {
374 return StringHasher::computeHashAndMaskTop8Bits(reinterpret_cast<const L Char*>(buf.s), buf.length);
375 }
376
377 static bool equal(StringImpl* const& str, const CharBuffer& buf)
378 {
379 return WTF::equal(str, buf.s, buf.length);
380 }
381
382 static void translate(StringImpl*& location, const CharBuffer& buf, unsigned hash)
383 {
384 location = StringImpl::create(buf.s, buf.length).leakRef();
385 location->setHash(hash);
386 location->setIsAtomic(true);
387 }
388 };
389
390 PassRefPtr<StringImpl> AtomicString::add(const LChar* s, unsigned length) 360 PassRefPtr<StringImpl> AtomicString::add(const LChar* s, unsigned length)
391 { 361 {
392 if (!s) 362 if (!s)
393 return nullptr; 363 return nullptr;
394 364
395 if (!length) 365 if (!length)
396 return StringImpl::empty(); 366 return StringImpl::empty();
397 367
398 LCharBuffer buffer = { s, length }; 368 LCharBuffer buffer = { s, length };
399 return addToStringTable<LCharBuffer, LCharBufferTranslator>(buffer); 369 return addToStringTable<LCharBuffer, LCharBufferTranslator>(buffer);
400 } 370 }
401 371
402 PassRefPtr<StringImpl> AtomicString::addFromLiteralData(const char* characters, unsigned length)
403 {
404 ASSERT(characters);
405 ASSERT(length);
406
407 CharBuffer buffer = { characters, length };
408 return addToStringTable<CharBuffer, CharBufferFromLiteralDataTranslator>(buf fer);
409 }
410
411 PassRefPtr<StringImpl> AtomicString::addSlowCase(StringImpl* string) 372 PassRefPtr<StringImpl> AtomicString::addSlowCase(StringImpl* string)
412 { 373 {
413 return atomicStringTable().addStringImpl(string); 374 return atomicStringTable().addStringImpl(string);
414 } 375 }
415 376
416 template<typename CharacterType> 377 template<typename CharacterType>
417 static inline HashSet<StringImpl*>::iterator findString(const StringImpl* string Impl) 378 static inline HashSet<StringImpl*>::iterator findString(const StringImpl* string Impl)
418 { 379 {
419 HashAndCharacters<CharacterType> buffer = { stringImpl->existingHash(), stri ngImpl->getCharacters<CharacterType>(), stringImpl->length() }; 380 HashAndCharacters<CharacterType> buffer = { stringImpl->existingHash(), stri ngImpl->getCharacters<CharacterType>(), stringImpl->length() };
420 return atomicStrings().find<HashAndCharactersTranslator<CharacterType>>(buff er); 381 return atomicStrings().find<HashAndCharactersTranslator<CharacterType>>(buff er);
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
536 } 497 }
537 498
538 #ifndef NDEBUG 499 #ifndef NDEBUG
539 void AtomicString::show() const 500 void AtomicString::show() const
540 { 501 {
541 m_string.show(); 502 m_string.show();
542 } 503 }
543 #endif 504 #endif
544 505
545 } // namespace WTF 506 } // namespace WTF
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698