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

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

Issue 304253002: Hash tables templates, wrapping Array. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 5 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/hash_table_test.cc ('k') | runtime/vm/vm_sources.gypi » ('j') | 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/object.h" 5 #include "vm/object.h"
6 6
7 #include "include/dart_api.h" 7 #include "include/dart_api.h"
8 #include "platform/assert.h" 8 #include "platform/assert.h"
9 #include "vm/assembler.h" 9 #include "vm/assembler.h"
10 #include "vm/cpu.h" 10 #include "vm/cpu.h"
(...skipping 11 matching lines...) Expand all
22 #include "vm/dart_entry.h" 22 #include "vm/dart_entry.h"
23 #include "vm/datastream.h" 23 #include "vm/datastream.h"
24 #include "vm/debugger.h" 24 #include "vm/debugger.h"
25 #include "vm/deopt_instructions.h" 25 #include "vm/deopt_instructions.h"
26 #include "vm/disassembler.h" 26 #include "vm/disassembler.h"
27 #include "vm/double_conversion.h" 27 #include "vm/double_conversion.h"
28 #include "vm/exceptions.h" 28 #include "vm/exceptions.h"
29 #include "vm/flow_graph_builder.h" 29 #include "vm/flow_graph_builder.h"
30 #include "vm/flow_graph_compiler.h" 30 #include "vm/flow_graph_compiler.h"
31 #include "vm/growable_array.h" 31 #include "vm/growable_array.h"
32 #include "vm/hash_table.h"
32 #include "vm/heap.h" 33 #include "vm/heap.h"
33 #include "vm/intermediate_language.h" 34 #include "vm/intermediate_language.h"
34 #include "vm/intrinsifier.h" 35 #include "vm/intrinsifier.h"
35 #include "vm/object_id_ring.h" 36 #include "vm/object_id_ring.h"
36 #include "vm/object_store.h" 37 #include "vm/object_store.h"
37 #include "vm/parser.h" 38 #include "vm/parser.h"
38 #include "vm/report.h" 39 #include "vm/report.h"
39 #include "vm/reusable_handles.h" 40 #include "vm/reusable_handles.h"
40 #include "vm/runtime_entry.h" 41 #include "vm/runtime_entry.h"
41 #include "vm/scopes.h" 42 #include "vm/scopes.h"
(...skipping 8608 matching lines...) Expand 10 before | Expand all | Expand 10 after
8650 obj = LookupLocalObject(accessor_name); 8651 obj = LookupLocalObject(accessor_name);
8651 if (obj.IsNull()) { 8652 if (obj.IsNull()) {
8652 obj = LookupImportedObject(name); 8653 obj = LookupImportedObject(name);
8653 } 8654 }
8654 } 8655 }
8655 AddToResolvedNamesCache(name, obj); 8656 AddToResolvedNamesCache(name, obj);
8656 return obj.raw(); 8657 return obj.raw();
8657 } 8658 }
8658 8659
8659 8660
8660 static intptr_t ResolvedNameCacheSize(const Array& cache) { 8661 class StringEqualsTraits {
8661 return (cache.Length() - 1) / 2; 8662 public:
8662 } 8663 static bool IsMatch(const Object& a, const Object& b) {
8664 return String::Cast(a).Equals(String::Cast(b));
8665 }
8666 static uword Hash(const Object& obj) {
8667 return String::Cast(obj).Hash();
8668 }
8669 };
8670 typedef UnorderedHashMap<StringEqualsTraits> ResolvedNamesMap;
8663 8671
8664 8672
8665 // Returns true if the name is found in the cache, false no cache hit. 8673 // Returns true if the name is found in the cache, false no cache hit.
8666 // obj is set to the cached entry. It may be null, indicating that the 8674 // obj is set to the cached entry. It may be null, indicating that the
8667 // name does not resolve to anything in this library. 8675 // name does not resolve to anything in this library.
8668 bool Library::LookupResolvedNamesCache(const String& name, 8676 bool Library::LookupResolvedNamesCache(const String& name,
8669 Object* obj) const { 8677 Object* obj) const {
8670 const Array& cache = Array::Handle(resolved_names()); 8678 ResolvedNamesMap cache(Array::Handle(resolved_names()));
8671 const intptr_t cache_size = ResolvedNameCacheSize(cache); 8679 bool present = false;
8672 intptr_t index = name.Hash() % cache_size; 8680 *obj = cache.GetOrNull(name, &present);
8673 String& entry_name = String::Handle(); 8681 RawArray* array = cache.Release();
8674 entry_name ^= cache.At(index); 8682 ASSERT(array == resolved_names());
8675 while (!entry_name.IsNull()) { 8683 return present;
8676 if (entry_name.Equals(name)) {
8677 CompilerStats::num_lib_cache_hit++;
8678 *obj = cache.At(index + cache_size);
8679 return true;
8680 }
8681 index = (index + 1) % cache_size;
8682 entry_name ^= cache.At(index);
8683 }
8684 *obj = Object::null();
8685 return false;
8686 }
8687
8688
8689 void Library::GrowResolvedNamesCache() const {
8690 const Array& old_cache = Array::Handle(resolved_names());
8691 const intptr_t old_cache_size = ResolvedNameCacheSize(old_cache);
8692
8693 // Create empty new cache and add entries from the old cache.
8694 const intptr_t new_cache_size = old_cache_size * 3 / 2;
8695 InitResolvedNamesCache(new_cache_size);
8696 String& name = String::Handle();
8697 Object& entry = Object::Handle();
8698 for (intptr_t i = 0; i < old_cache_size; i++) {
8699 name ^= old_cache.At(i);
8700 if (!name.IsNull()) {
8701 entry = old_cache.At(i + old_cache_size);
8702 AddToResolvedNamesCache(name, entry);
8703 }
8704 }
8705 } 8684 }
8706 8685
8707 8686
8708 // Add a name to the resolved name cache. This name resolves to the 8687 // Add a name to the resolved name cache. This name resolves to the
8709 // given object in this library scope. obj may be null, which means 8688 // given object in this library scope. obj may be null, which means
8710 // the name does not resolve to anything in this library scope. 8689 // the name does not resolve to anything in this library scope.
8711 void Library::AddToResolvedNamesCache(const String& name, 8690 void Library::AddToResolvedNamesCache(const String& name,
8712 const Object& obj) const { 8691 const Object& obj) const {
8713 if (!FLAG_use_lib_cache) { 8692 if (!FLAG_use_lib_cache) {
8714 return; 8693 return;
8715 } 8694 }
8716 ASSERT(!Field::IsGetterName(name) && !Field::IsSetterName(name)); 8695 ResolvedNamesMap cache(Array::Handle(resolved_names()));
8717 const Array& cache = Array::Handle(resolved_names()); 8696 cache.UpdateOrInsert(name, obj);
8718 // let N = cache.Length(); 8697 StorePointer(&raw_ptr()->resolved_names_, cache.Release());
8719 // The entry cache[N-1] is used as a counter
8720 // The array entries [0..N-2] are used as cache entries.
8721 // cache[i] contains the name of the entry
8722 // cache[i+(N-1)/2] contains the resolved entity, or NULL if that name
8723 // is not present in the library.
8724 const intptr_t counter_index = cache.Length() - 1;
8725 intptr_t cache_size = ResolvedNameCacheSize(cache);
8726 intptr_t index = name.Hash() % cache_size;
8727 String& entry_name = String::Handle();
8728 entry_name ^= cache.At(index);
8729 // An empty spot will be found because we keep the hash set at most 75% full.
8730 while (!entry_name.IsNull()) {
8731 index = (index + 1) % cache_size;
8732 entry_name ^= cache.At(index);
8733 }
8734 // Insert the object at the empty slot.
8735 cache.SetAt(index, name);
8736 ASSERT(cache.At(index + cache_size) == Object::null());
8737 cache.SetAt(index + cache_size, obj);
8738
8739 // One more element added.
8740 intptr_t num_used = Smi::Value(Smi::RawCast(cache.At(counter_index))) + 1;
8741 cache.SetAt(counter_index, Smi::Handle(Smi::New(num_used)));
8742 CompilerStats::num_names_cached++;
8743
8744 // Rehash if symbol_table is 75% full.
8745 if (num_used > ((cache_size / 4) * 3)) {
8746 CompilerStats::num_names_cached -= num_used;
8747 GrowResolvedNamesCache();
8748 }
8749 } 8698 }
8750 8699
8751 8700
8752 void Library::InvalidateResolvedName(const String& name) const { 8701 void Library::InvalidateResolvedName(const String& name) const {
8753 Object& entry = Object::Handle(); 8702 Object& entry = Object::Handle();
8754 if (LookupResolvedNamesCache(name, &entry)) { 8703 if (LookupResolvedNamesCache(name, &entry)) {
8704 // TODO(koda): Support deleted sentinel in snapshots and remove only 'name'.
8755 InvalidateResolvedNamesCache(); 8705 InvalidateResolvedNamesCache();
8756 } 8706 }
8757 } 8707 }
8758 8708
8759 8709
8760 void Library::InvalidateResolvedNamesCache() const { 8710 void Library::InvalidateResolvedNamesCache() const {
8761 const intptr_t kInvalidatedCacheSize = 16; 8711 const intptr_t kInvalidatedCacheSize = 16;
8762 InitResolvedNamesCache(kInvalidatedCacheSize); 8712 InitResolvedNamesCache(kInvalidatedCacheSize);
8763 } 8713 }
8764 8714
(...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after
9261 9211
9262 static RawArray* NewDictionary(intptr_t initial_size) { 9212 static RawArray* NewDictionary(intptr_t initial_size) {
9263 const Array& dict = Array::Handle(Array::New(initial_size + 1, Heap::kOld)); 9213 const Array& dict = Array::Handle(Array::New(initial_size + 1, Heap::kOld));
9264 // The last element of the dictionary specifies the number of in use slots. 9214 // The last element of the dictionary specifies the number of in use slots.
9265 dict.SetAt(initial_size, Smi::Handle(Smi::New(0))); 9215 dict.SetAt(initial_size, Smi::Handle(Smi::New(0)));
9266 return dict.raw(); 9216 return dict.raw();
9267 } 9217 }
9268 9218
9269 9219
9270 void Library::InitResolvedNamesCache(intptr_t size) const { 9220 void Library::InitResolvedNamesCache(intptr_t size) const {
9271 // Need space for 'size' names and 'size' resolved object entries. 9221 const Array& cache = Array::Handle(HashTables::New<ResolvedNamesMap>(size));
9272 StorePointer(&raw_ptr()->resolved_names_, NewDictionary(2 * size)); 9222 StorePointer(&raw_ptr()->resolved_names_, cache.raw());
9273 } 9223 }
9274 9224
9275 9225
9276 void Library::InitClassDictionary() const { 9226 void Library::InitClassDictionary() const {
9277 // TODO(iposva): Find reasonable initial size. 9227 // TODO(iposva): Find reasonable initial size.
9278 const int kInitialElementCount = 16; 9228 const int kInitialElementCount = 16;
9279 StorePointer(&raw_ptr()->dictionary_, NewDictionary(kInitialElementCount)); 9229 StorePointer(&raw_ptr()->dictionary_, NewDictionary(kInitialElementCount));
9280 } 9230 }
9281 9231
9282 9232
(...skipping 9801 matching lines...) Expand 10 before | Expand all | Expand 10 after
19084 return tag_label.ToCString(); 19034 return tag_label.ToCString();
19085 } 19035 }
19086 19036
19087 19037
19088 void UserTag::PrintJSONImpl(JSONStream* stream, bool ref) const { 19038 void UserTag::PrintJSONImpl(JSONStream* stream, bool ref) const {
19089 Instance::PrintJSONImpl(stream, ref); 19039 Instance::PrintJSONImpl(stream, ref);
19090 } 19040 }
19091 19041
19092 19042
19093 } // namespace dart 19043 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/hash_table_test.cc ('k') | runtime/vm/vm_sources.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698