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

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: 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
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 21 matching lines...) Expand all
32 32
33 namespace WTF { 33 namespace WTF {
34 34
35 using namespace Unicode; 35 using namespace Unicode;
36 36
37 static_assert(sizeof(AtomicString) == sizeof(String), "AtomicString and String m ust be same size"); 37 static_assert(sizeof(AtomicString) == sizeof(String), "AtomicString and String m ust be same size");
38 38
39 class AtomicStringTable { 39 class AtomicStringTable {
40 WTF_MAKE_NONCOPYABLE(AtomicStringTable); 40 WTF_MAKE_NONCOPYABLE(AtomicStringTable);
41 public: 41 public:
42 static AtomicStringTable* create(WTFThreadData& data) 42 AtomicStringTable() = default;
43 {
44 data.m_atomicStringTable = new AtomicStringTable;
45 data.m_atomicStringTableDestructor = AtomicStringTable::destroy;
46 data.m_atomicStringTable->addStaticStrings();
47 return data.m_atomicStringTable;
48 }
49
50 StringImpl* addStringImpl(StringImpl* string) 43 StringImpl* addStringImpl(StringImpl* string)
51 { 44 {
52 if (!string->length()) 45 if (!string->length())
53 return StringImpl::empty(); 46 return StringImpl::empty();
54 47
55 StringImpl* result = *m_table.add(string).storedValue; 48 StringImpl* result = *m_table.add(string).storedValue;
56 49
57 if (!result->isAtomic()) 50 if (!result->isAtomic())
58 result->setIsAtomic(true); 51 result->setIsAtomic(true);
59 52
60 ASSERT(!string->isStatic() || result->isStatic()); 53 ASSERT(!string->isStatic() || result->isStatic());
61 return result; 54 return result;
62 } 55 }
63 56
64 HashSet<StringImpl*>& table() 57 HashSet<StringImpl*>& table()
65 { 58 {
66 return m_table; 59 return m_table;
67 } 60 }
68 61
69 private: 62 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 }
80 }
81
82 static void destroy(AtomicStringTable* table)
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; 63 HashSet<StringImpl*> m_table;
96 }; 64 };
97 65
98 static inline AtomicStringTable& atomicStringTable() 66 AtomicStringTable* AtomicString::createAtomicStringTable()
99 { 67 {
100 // Once possible we should make this non-lazy (constructed in WTFThreadData' s constructor). 68 AtomicStringTable* table = new AtomicStringTable;
69 const StaticStringsTable& staticStrings = StringImpl::allStaticStrings();
70 for (auto it = staticStrings.begin(); it != staticStrings.end(); ++it) {
71 table->addStringImpl(it->value);
72 }
73 return table;
74 }
75
76 void AtomicString::destroyAtomicStringTable(AtomicStringTable* table)
77 {
78 for (StringImpl* string : table->table()) {
79 if (!string->isStatic()) {
80 ASSERT(string->isAtomic());
81 string->setIsAtomic(false);
82 }
83 }
84 delete table;
85 }
86
87 static inline AtomicStringTable* atomicStringTable()
88 {
101 WTFThreadData& data = wtfThreadData(); 89 WTFThreadData& data = wtfThreadData();
102 AtomicStringTable* table = data.atomicStringTable(); 90 AtomicStringTable* table = data.atomicStringTable();
103 if (UNLIKELY(!table)) 91 ASSERT(table);
104 table = AtomicStringTable::create(data); 92 return table;
105 return *table;
106 } 93 }
107 94
108 static inline HashSet<StringImpl*>& atomicStrings() 95 static inline HashSet<StringImpl*>& atomicStrings()
109 { 96 {
110 return atomicStringTable().table(); 97 return atomicStringTable()->table();
111 } 98 }
112 99
113 template<typename T, typename HashTranslator> 100 template<typename T, typename HashTranslator>
114 static inline PassRefPtr<StringImpl> addToStringTable(const T& value) 101 static inline PassRefPtr<StringImpl> addToStringTable(const T& value)
115 { 102 {
116 HashSet<StringImpl*>::AddResult addResult = atomicStrings().add<HashTranslat or>(value); 103 HashSet<StringImpl*>::AddResult addResult = atomicStrings().add<HashTranslat or>(value);
117 104
118 // If the string is newly-translated, then we need to adopt it. 105 // If the string is newly-translated, then we need to adopt it.
119 // The boolean in the pair tells us if that is so. 106 // The boolean in the pair tells us if that is so.
120 return addResult.isNewEntry ? adoptRef(*addResult.storedValue) : *addResult. storedValue; 107 return addResult.isNewEntry ? adoptRef(*addResult.storedValue) : *addResult. storedValue;
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
398 { 385 {
399 ASSERT(characters); 386 ASSERT(characters);
400 ASSERT(length); 387 ASSERT(length);
401 388
402 CharBuffer buffer = { characters, length }; 389 CharBuffer buffer = { characters, length };
403 return addToStringTable<CharBuffer, CharBufferFromLiteralDataTranslator>(buf fer); 390 return addToStringTable<CharBuffer, CharBufferFromLiteralDataTranslator>(buf fer);
404 } 391 }
405 392
406 PassRefPtr<StringImpl> AtomicString::addSlowCase(StringImpl* string) 393 PassRefPtr<StringImpl> AtomicString::addSlowCase(StringImpl* string)
407 { 394 {
408 return atomicStringTable().addStringImpl(string); 395 return atomicStringTable()->addStringImpl(string);
409 } 396 }
410 397
411 template<typename CharacterType> 398 template<typename CharacterType>
412 static inline HashSet<StringImpl*>::iterator findString(const StringImpl* string Impl) 399 static inline HashSet<StringImpl*>::iterator findString(const StringImpl* string Impl)
413 { 400 {
414 HashAndCharacters<CharacterType> buffer = { stringImpl->existingHash(), stri ngImpl->getCharacters<CharacterType>(), stringImpl->length() }; 401 HashAndCharacters<CharacterType> buffer = { stringImpl->existingHash(), stri ngImpl->getCharacters<CharacterType>(), stringImpl->length() };
415 return atomicStrings().find<HashAndCharactersTranslator<CharacterType>>(buff er); 402 return atomicStrings().find<HashAndCharactersTranslator<CharacterType>>(buff er);
416 } 403 }
417 404
418 StringImpl* AtomicString::find(const StringImpl* stringImpl) 405 StringImpl* AtomicString::find(const StringImpl* stringImpl)
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
507 } 494 }
508 495
509 #ifndef NDEBUG 496 #ifndef NDEBUG
510 void AtomicString::show() const 497 void AtomicString::show() const
511 { 498 {
512 m_string.show(); 499 m_string.show();
513 } 500 }
514 #endif 501 #endif
515 502
516 } // namespace WTF 503 } // namespace WTF
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698