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

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

Issue 1370113006: AtomicStringTable should not be initialized lazily Base URL: https://chromium.googlesource.com/chromium/src.git@AtomitStringTable
Patch Set: Export AtomicStringTable Created 5 years, 2 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 | « third_party/WebKit/Source/wtf/text/AtomicString.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 /* 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 *
11 * This library is distributed in the hope that it will be useful, 11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details. 14 * Library General Public License for more details.
15 * 15 *
16 * You should have received a copy of the GNU Library General Public License 16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to 17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA. 19 * Boston, MA 02110-1301, USA.
20 * 20 *
21 */ 21 */
22 22
23 #include "config.h" 23 #include "config.h"
24 #include "AtomicString.h" 24 #include "AtomicString.h"
25 25
26 #include "StringHash.h" 26 #include "StringHash.h"
27 #include "wtf/HashSet.h"
28 #include "wtf/WTFThreadData.h" 27 #include "wtf/WTFThreadData.h"
29 #include "wtf/dtoa.h" 28 #include "wtf/dtoa.h"
30 #include "wtf/text/IntegerToStringConversion.h" 29 #include "wtf/text/IntegerToStringConversion.h"
31 #include "wtf/text/UTF8.h" 30 #include "wtf/text/UTF8.h"
32 31
33 namespace WTF { 32 namespace WTF {
34 33
35 using namespace Unicode; 34 using namespace Unicode;
36 35
37 static_assert(sizeof(AtomicString) == sizeof(String), "AtomicString and String m ust be same size"); 36 static_assert(sizeof(AtomicString) == sizeof(String), "AtomicString and String m ust be same size");
38 37
39 class AtomicStringTable { 38 StringImpl* AtomicStringTable::addStringImpl(StringImpl* string)
40 WTF_MAKE_NONCOPYABLE(AtomicStringTable); 39 {
41 public: 40 if (!string->length())
42 static AtomicStringTable* create(WTFThreadData& data) 41 return StringImpl::empty();
43 { 42
44 data.m_atomicStringTable = new AtomicStringTable; 43 StringImpl* result = *m_table.add(string).storedValue;
45 data.m_atomicStringTableDestructor = AtomicStringTable::destroy; 44
46 data.m_atomicStringTable->addStaticStrings(); 45 if (!result->isAtomic())
47 return data.m_atomicStringTable; 46 result->setIsAtomic(true);
47
48 ASSERT(!string->isStatic() || result->isStatic());
49 return result;
50 }
51
52 AtomicStringTable* AtomicStringTable::create()
53 {
54 AtomicStringTable* table = new AtomicStringTable;
55 const StaticStringsTable& staticStrings = StringImpl::allStaticStrings();
56 for (auto it = staticStrings.begin(); it != staticStrings.end(); ++it) {
57 table->addStringImpl(it->value);
48 } 58 }
59 return table;
60 }
49 61
50 StringImpl* addStringImpl(StringImpl* string) 62 void AtomicStringTable::destroy(AtomicStringTable* table)
51 { 63 {
52 if (!string->length()) 64 for (StringImpl* string : table->table()) {
53 return StringImpl::empty(); 65 if (!string->isStatic()) {
54 66 ASSERT(string->isAtomic());
55 StringImpl* result = *m_table.add(string).storedValue; 67 string->setIsAtomic(false);
56
57 if (!result->isAtomic())
58 result->setIsAtomic(true);
59
60 ASSERT(!string->isStatic() || result->isStatic());
61 return result;
62 }
63
64 HashSet<StringImpl*>& table()
65 {
66 return m_table;
67 }
68
69 private:
70 AtomicStringTable() { }
71
72 void addStaticStrings()
73 {
74 const StaticStringsTable& staticStrings = StringImpl::allStaticStrings() ;
75
76 StaticStringsTable::const_iterator it = staticStrings.begin();
77 for (; it != staticStrings.end(); ++it) {
78 addStringImpl(it->value);
79 } 68 }
80 } 69 }
70 delete table;
71 }
81 72
82 static void destroy(AtomicStringTable* table) 73 static inline AtomicStringTable* atomicStringTable()
83 {
84 HashSet<StringImpl*>::iterator end = table->m_table.end();
85 for (HashSet<StringImpl*>::iterator iter = table->m_table.begin(); iter != end; ++iter) {
86 StringImpl* string = *iter;
87 if (!string->isStatic()) {
88 ASSERT(string->isAtomic());
89 string->setIsAtomic(false);
90 }
91 }
92 delete table;
93 }
94
95 HashSet<StringImpl*> m_table;
96 };
97
98 static inline AtomicStringTable& atomicStringTable()
99 { 74 {
100 // Once possible we should make this non-lazy (constructed in WTFThreadData' s constructor).
101 WTFThreadData& data = wtfThreadData(); 75 WTFThreadData& data = wtfThreadData();
102 AtomicStringTable* table = data.atomicStringTable(); 76 AtomicStringTable* table = data.atomicStringTable();
103 if (UNLIKELY(!table)) 77 ASSERT(table);
104 table = AtomicStringTable::create(data); 78 return table;
105 return *table;
106 } 79 }
107 80
108 static inline HashSet<StringImpl*>& atomicStrings() 81 static inline HashSet<StringImpl*>& atomicStrings()
109 { 82 {
110 return atomicStringTable().table(); 83 return atomicStringTable()->table();
111 } 84 }
112 85
113 template<typename T, typename HashTranslator> 86 template<typename T, typename HashTranslator>
114 static inline PassRefPtr<StringImpl> addToStringTable(const T& value) 87 static inline PassRefPtr<StringImpl> addToStringTable(const T& value)
115 { 88 {
116 HashSet<StringImpl*>::AddResult addResult = atomicStrings().add<HashTranslat or>(value); 89 HashSet<StringImpl*>::AddResult addResult = atomicStrings().add<HashTranslat or>(value);
117 90
118 // If the string is newly-translated, then we need to adopt it. 91 // If the string is newly-translated, then we need to adopt it.
119 // The boolean in the pair tells us if that is so. 92 // The boolean in the pair tells us if that is so.
120 return addResult.isNewEntry ? adoptRef(*addResult.storedValue) : *addResult. storedValue; 93 return addResult.isNewEntry ? adoptRef(*addResult.storedValue) : *addResult. storedValue;
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
398 { 371 {
399 ASSERT(characters); 372 ASSERT(characters);
400 ASSERT(length); 373 ASSERT(length);
401 374
402 CharBuffer buffer = { characters, length }; 375 CharBuffer buffer = { characters, length };
403 return addToStringTable<CharBuffer, CharBufferFromLiteralDataTranslator>(buf fer); 376 return addToStringTable<CharBuffer, CharBufferFromLiteralDataTranslator>(buf fer);
404 } 377 }
405 378
406 PassRefPtr<StringImpl> AtomicString::addSlowCase(StringImpl* string) 379 PassRefPtr<StringImpl> AtomicString::addSlowCase(StringImpl* string)
407 { 380 {
408 return atomicStringTable().addStringImpl(string); 381 return atomicStringTable()->addStringImpl(string);
409 } 382 }
410 383
411 template<typename CharacterType> 384 template<typename CharacterType>
412 static inline HashSet<StringImpl*>::iterator findString(const StringImpl* string Impl) 385 static inline HashSet<StringImpl*>::iterator findString(const StringImpl* string Impl)
413 { 386 {
414 HashAndCharacters<CharacterType> buffer = { stringImpl->existingHash(), stri ngImpl->getCharacters<CharacterType>(), stringImpl->length() }; 387 HashAndCharacters<CharacterType> buffer = { stringImpl->existingHash(), stri ngImpl->getCharacters<CharacterType>(), stringImpl->length() };
415 return atomicStrings().find<HashAndCharactersTranslator<CharacterType>>(buff er); 388 return atomicStrings().find<HashAndCharactersTranslator<CharacterType>>(buff er);
416 } 389 }
417 390
418 StringImpl* AtomicString::find(const StringImpl* stringImpl) 391 StringImpl* AtomicString::find(const StringImpl* stringImpl)
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
507 } 480 }
508 481
509 #ifndef NDEBUG 482 #ifndef NDEBUG
510 void AtomicString::show() const 483 void AtomicString::show() const
511 { 484 {
512 m_string.show(); 485 m_string.show();
513 } 486 }
514 #endif 487 #endif
515 488
516 } // namespace WTF 489 } // namespace WTF
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/text/AtomicString.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698