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

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

Issue 442483002: Avoid redundant allocation of accessor names. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 4 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 | Annotate | Revision Log
« no previous file with comments | « runtime/vm/symbols.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 (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/symbols.h" 5 #include "vm/symbols.h"
6 6
7 #include "vm/handles.h" 7 #include "vm/handles.h"
8 #include "vm/handles_impl.h" 8 #include "vm/handles_impl.h"
9 #include "vm/hash_table.h" 9 #include "vm/hash_table.h"
10 #include "vm/isolate.h" 10 #include "vm/isolate.h"
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 } else { 163 } else {
164 String& result = String::Handle( 164 String& result = String::Handle(
165 String::SubString(str_, begin_index_, len_, Heap::kOld)); 165 String::SubString(str_, begin_index_, len_, Heap::kOld));
166 result.SetCanonical(); 166 result.SetCanonical();
167 result.SetHash(hash_); 167 result.SetHash(hash_);
168 return result.raw(); 168 return result.raw();
169 } 169 }
170 } 170 }
171 171
172 172
173 class ConcatString {
174 public:
175 ConcatString(const String& str1, const String& str2)
176 : str1_(str1), str2_(str2), hash_(String::HashConcat(str1, str2)) {}
177 RawString* ToSymbol() const;
178 bool Equals(const String& other) const {
179 return other.EqualsConcat(str1_, str2_);
180 }
181 intptr_t Hash() const { return hash_; }
182 private:
183 const String& str1_;
184 const String& str2_;
185 intptr_t hash_;
186 };
187
188
189 RawString* ConcatString::ToSymbol() const {
190 String& result = String::Handle(String::Concat(str1_, str2_, Heap::kOld));
191 result.SetCanonical();
192 result.SetHash(hash_);
193 return result.raw();
194 }
195
196
173 class SymbolTraits { 197 class SymbolTraits {
174 public: 198 public:
175 static bool IsMatch(const Object& a, const Object& b) { 199 static bool IsMatch(const Object& a, const Object& b) {
176 return String::Cast(a).Equals(String::Cast(b)); 200 return String::Cast(a).Equals(String::Cast(b));
177 } 201 }
178 template<typename CharType> 202 template<typename CharType>
179 static bool IsMatch(const CharArray<CharType>& array, const Object& obj) { 203 static bool IsMatch(const CharArray<CharType>& array, const Object& obj) {
180 return array.Equals(String::Cast(obj)); 204 return array.Equals(String::Cast(obj));
181 } 205 }
182 static bool IsMatch(const StringSlice& slice, const Object& obj) { 206 static bool IsMatch(const StringSlice& slice, const Object& obj) {
183 return slice.Equals(String::Cast(obj)); 207 return slice.Equals(String::Cast(obj));
184 } 208 }
209 static bool IsMatch(const ConcatString& concat, const Object& obj) {
210 return concat.Equals(String::Cast(obj));
211 }
185 static uword Hash(const Object& key) { 212 static uword Hash(const Object& key) {
186 return String::Cast(key).Hash(); 213 return String::Cast(key).Hash();
187 } 214 }
188 template<typename CharType> 215 template<typename CharType>
189 static uword Hash(const CharArray<CharType>& array) { 216 static uword Hash(const CharArray<CharType>& array) {
190 return array.Hash(); 217 return array.Hash();
191 } 218 }
192 static uword Hash(const StringSlice& slice) { 219 static uword Hash(const StringSlice& slice) {
193 return slice.Hash(); 220 return slice.Hash();
194 } 221 }
222 static uword Hash(const ConcatString& concat) {
223 return concat.Hash();
224 }
195 template<typename CharType> 225 template<typename CharType>
196 static RawObject* NewKey(const CharArray<CharType>& array) { 226 static RawObject* NewKey(const CharArray<CharType>& array) {
197 return array.ToSymbol(); 227 return array.ToSymbol();
198 } 228 }
199 static RawObject* NewKey(const StringSlice& slice) { 229 static RawObject* NewKey(const StringSlice& slice) {
200 return slice.ToSymbol(); 230 return slice.ToSymbol();
201 } 231 }
232 static RawObject* NewKey(const ConcatString& concat) {
233 return concat.ToSymbol();
234 }
202 }; 235 };
203 typedef UnorderedHashSet<SymbolTraits> SymbolTable; 236 typedef UnorderedHashSet<SymbolTraits> SymbolTable;
204 237
205 238
206 void Symbols::SetupSymbolTable(Isolate* isolate) { 239 void Symbols::SetupSymbolTable(Isolate* isolate) {
207 ASSERT(isolate != NULL); 240 ASSERT(isolate != NULL);
208 241
209 // Setup the symbol table used within the String class. 242 // Setup the symbol table used within the String class.
210 const intptr_t initial_size = (isolate == Dart::vm_isolate()) ? 243 const intptr_t initial_size = (isolate == Dart::vm_isolate()) ?
211 kInitialVMIsolateSymtabSize : kInitialSymtabSize; 244 kInitialVMIsolateSymtabSize : kInitialSymtabSize;
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 RawString* Symbols::FromUTF16(const uint16_t* utf16_array, intptr_t len) { 304 RawString* Symbols::FromUTF16(const uint16_t* utf16_array, intptr_t len) {
272 return NewSymbol(UTF16Array(utf16_array, len)); 305 return NewSymbol(UTF16Array(utf16_array, len));
273 } 306 }
274 307
275 308
276 RawString* Symbols::FromUTF32(const int32_t* utf32_array, intptr_t len) { 309 RawString* Symbols::FromUTF32(const int32_t* utf32_array, intptr_t len) {
277 return NewSymbol(UTF32Array(utf32_array, len)); 310 return NewSymbol(UTF32Array(utf32_array, len));
278 } 311 }
279 312
280 313
281 // StringType can be StringSlice, Latin1Array, UTF16Array or UTF32Array. 314 RawString* Symbols::FromConcat(const String& str1, const String& str2) {
315 return NewSymbol(ConcatString(str1, str2));
316 }
317
318
319 // StringType can be StringSlice, ConcatString, or {Latin1,UTF16,UTF32}Array.
282 template<typename StringType> 320 template<typename StringType>
283 RawString* Symbols::NewSymbol(const StringType& str) { 321 RawString* Symbols::NewSymbol(const StringType& str) {
284 Isolate* isolate = Isolate::Current(); 322 Isolate* isolate = Isolate::Current();
285 String& symbol = String::Handle(isolate); 323 String& symbol = String::Handle(isolate);
286 { 324 {
287 Isolate* vm_isolate = Dart::vm_isolate(); 325 Isolate* vm_isolate = Dart::vm_isolate();
288 SymbolTable table(isolate, vm_isolate->object_store()->symbol_table()); 326 SymbolTable table(isolate, vm_isolate->object_store()->symbol_table());
289 symbol ^= table.GetOrNull(str); 327 symbol ^= table.GetOrNull(str);
290 table.Release(); 328 table.Release();
291 } 329 }
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 RawObject* Symbols::GetVMSymbol(intptr_t object_id) { 389 RawObject* Symbols::GetVMSymbol(intptr_t object_id) {
352 ASSERT(IsVMSymbolId(object_id)); 390 ASSERT(IsVMSymbolId(object_id));
353 intptr_t i = (object_id - kMaxPredefinedObjectIds); 391 intptr_t i = (object_id - kMaxPredefinedObjectIds);
354 if ((i > kIllegal) && (i < Symbols::kMaxPredefinedId)) { 392 if ((i > kIllegal) && (i < Symbols::kMaxPredefinedId)) {
355 return symbol_handles_[i]->raw(); 393 return symbol_handles_[i]->raw();
356 } 394 }
357 return Object::null(); 395 return Object::null();
358 } 396 }
359 397
360 } // namespace dart 398 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/symbols.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698