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

Side by Side Diff: runtime/vm/object.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/object.h ('k') | runtime/vm/object_test.cc » ('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 6840 matching lines...) Expand 10 before | Expand all | Expand 10 after
6851 6851
6852 6852
6853 void RedirectionData::PrintJSONImpl(JSONStream* stream, bool ref) const { 6853 void RedirectionData::PrintJSONImpl(JSONStream* stream, bool ref) const {
6854 Object::PrintJSONImpl(stream, ref); 6854 Object::PrintJSONImpl(stream, ref);
6855 } 6855 }
6856 6856
6857 6857
6858 RawString* Field::GetterName(const String& field_name) { 6858 RawString* Field::GetterName(const String& field_name) {
6859 CompilerStats::make_accessor_name++; 6859 CompilerStats::make_accessor_name++;
6860 // TODO(koda): Avoid most of these allocations by adding prefix-based lookup 6860 // TODO(koda): Avoid most of these allocations by adding prefix-based lookup
6861 // to Symbols. 6861 // to Class::Lookup*.
6862 return String::Concat(Symbols::GetterPrefix(), field_name); 6862 return String::Concat(Symbols::GetterPrefix(), field_name);
6863 } 6863 }
6864 6864
6865 6865
6866 RawString* Field::GetterSymbol(const String& field_name) { 6866 RawString* Field::GetterSymbol(const String& field_name) {
6867 const String& str = String::Handle(Field::GetterName(field_name)); 6867 return Symbols::FromConcat(Symbols::GetterPrefix(), field_name);
6868 return Symbols::New(str);
6869 } 6868 }
6870 6869
6871 6870
6872 RawString* Field::SetterName(const String& field_name) { 6871 RawString* Field::SetterName(const String& field_name) {
6873 CompilerStats::make_accessor_name++; 6872 CompilerStats::make_accessor_name++;
6874 // TODO(koda): Avoid most of these allocations by adding prefix-based lookup 6873 // TODO(koda): Avoid most of these allocations by adding prefix-based lookup
6875 // to Symbols. 6874 // to Class::Lookup*.
6876 return String::Concat(Symbols::SetterPrefix(), field_name); 6875 return String::Concat(Symbols::SetterPrefix(), field_name);
6877 } 6876 }
6878 6877
6879 6878
6880 RawString* Field::SetterSymbol(const String& field_name) { 6879 RawString* Field::SetterSymbol(const String& field_name) {
6881 const String& str = String::Handle(Field::SetterName(field_name)); 6880 return Symbols::FromConcat(Symbols::SetterPrefix(), field_name);
6882 return Symbols::New(str);
6883 } 6881 }
6884 6882
6885 6883
6886 RawString* Field::NameFromGetter(const String& getter_name) { 6884 RawString* Field::NameFromGetter(const String& getter_name) {
6887 CompilerStats::make_field_name++; 6885 CompilerStats::make_field_name++;
6888 return String::SubString(getter_name, strlen(kGetterPrefix)); 6886 return String::SubString(getter_name, strlen(kGetterPrefix));
6889 } 6887 }
6890 6888
6891 6889
6892 RawString* Field::NameFromSetter(const String& setter_name) { 6890 RawString* Field::NameFromSetter(const String& setter_name) {
(...skipping 9186 matching lines...) Expand 10 before | Expand all | Expand 10 after
16079 } 16077 }
16080 16078
16081 16079
16082 // Synchronize with implementation in compiler (intrinsifier). 16080 // Synchronize with implementation in compiler (intrinsifier).
16083 class StringHasher : ValueObject { 16081 class StringHasher : ValueObject {
16084 public: 16082 public:
16085 StringHasher() : hash_(0) {} 16083 StringHasher() : hash_(0) {}
16086 void Add(int32_t ch) { 16084 void Add(int32_t ch) {
16087 hash_ = CombineHashes(hash_, ch); 16085 hash_ = CombineHashes(hash_, ch);
16088 } 16086 }
16087 void Add(const String& str, intptr_t begin_index, intptr_t len);
16088
16089 // Return a non-zero hash of at most 'bits' bits. 16089 // Return a non-zero hash of at most 'bits' bits.
16090 intptr_t Finalize(int bits) { 16090 intptr_t Finalize(int bits) {
16091 ASSERT(1 <= bits && bits <= (kBitsPerWord - 1)); 16091 ASSERT(1 <= bits && bits <= (kBitsPerWord - 1));
16092 hash_ = FinalizeHash(hash_); 16092 hash_ = FinalizeHash(hash_);
16093 hash_ = hash_ & ((static_cast<intptr_t>(1) << bits) - 1); 16093 hash_ = hash_ & ((static_cast<intptr_t>(1) << bits) - 1);
16094 ASSERT(hash_ <= static_cast<uint32_t>(kMaxInt32)); 16094 ASSERT(hash_ <= static_cast<uint32_t>(kMaxInt32));
16095 return hash_ == 0 ? 1 : hash_; 16095 return hash_ == 0 ? 1 : hash_;
16096 } 16096 }
16097 private: 16097 private:
16098 uint32_t hash_; 16098 uint32_t hash_;
16099 }; 16099 };
16100 16100
16101 16101
16102 intptr_t String::Hash(const String& str, intptr_t begin_index, intptr_t len) { 16102 void StringHasher::Add(const String& str, intptr_t begin_index, intptr_t len) {
16103 ASSERT(begin_index >= 0); 16103 ASSERT(begin_index >= 0);
16104 ASSERT(len >= 0); 16104 ASSERT(len >= 0);
16105 ASSERT((begin_index + len) <= str.Length()); 16105 ASSERT((begin_index + len) <= str.Length());
16106 StringHasher hasher;
16107 if (str.IsOneByteString()) { 16106 if (str.IsOneByteString()) {
16108 for (intptr_t i = 0; i < len; i++) { 16107 for (intptr_t i = 0; i < len; i++) {
16109 hasher.Add(*OneByteString::CharAddr(str, i + begin_index)); 16108 Add(*OneByteString::CharAddr(str, i + begin_index));
16110 } 16109 }
16111 } else { 16110 } else {
16112 CodePointIterator it(str, begin_index, len); 16111 String::CodePointIterator it(str, begin_index, len);
16113 while (it.Next()) { 16112 while (it.Next()) {
16114 hasher.Add(it.Current()); 16113 Add(it.Current());
16115 } 16114 }
16116 } 16115 }
16116 }
16117
16118
16119 intptr_t String::Hash(const String& str, intptr_t begin_index, intptr_t len) {
16120 StringHasher hasher;
16121 hasher.Add(str, begin_index, len);
16117 return hasher.Finalize(String::kHashBits); 16122 return hasher.Finalize(String::kHashBits);
16118 } 16123 }
16119 16124
16120 16125
16126 intptr_t String::HashConcat(const String& str1, const String& str2) {
16127 intptr_t len1 = str1.Length();
16128 // Since String::Hash works at the code point (rune) level, a surrogate pair
16129 // that crosses the boundary between str1 and str2 must be composed.
16130 if (str1.IsTwoByteString() && Utf16::IsLeadSurrogate(str1.CharAt(len1 - 1))) {
16131 const String& temp = String::Handle(String::Concat(str1, str2));
16132 return temp.Hash();
16133 } else {
16134 StringHasher hasher;
16135 hasher.Add(str1, 0, len1);
16136 hasher.Add(str2, 0, str2.Length());
16137 return hasher.Finalize(String::kHashBits);
16138 }
16139 }
16140
16141
16121 template<typename T> 16142 template<typename T>
16122 static intptr_t HashImpl(const T* characters, intptr_t len) { 16143 static intptr_t HashImpl(const T* characters, intptr_t len) {
16123 ASSERT(len >= 0); 16144 ASSERT(len >= 0);
16124 StringHasher hasher; 16145 StringHasher hasher;
16125 for (intptr_t i = 0; i < len; i++) { 16146 for (intptr_t i = 0; i < len; i++) {
16126 hasher.Add(characters[i]); 16147 hasher.Add(characters[i]);
16127 } 16148 }
16128 return hasher.Finalize(String::kHashBits); 16149 return hasher.Finalize(String::kHashBits);
16129 } 16150 }
16130 16151
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
16287 } 16308 }
16288 // Advance both streams forward. 16309 // Advance both streams forward.
16289 ++i; 16310 ++i;
16290 has_more = it.Next(); 16311 has_more = it.Next();
16291 } 16312 }
16292 // Strings are only true iff we reached the end in both streams. 16313 // Strings are only true iff we reached the end in both streams.
16293 return (i == len) && !has_more; 16314 return (i == len) && !has_more;
16294 } 16315 }
16295 16316
16296 16317
16318 bool String::EqualsConcat(const String& str1, const String& str2) const {
16319 return (Length() == str1.Length() + str2.Length()) &&
16320 str1.Equals(*this, 0, str1.Length()) &&
16321 str2.Equals(*this, str1.Length(), str2.Length());
16322 }
16323
16324
16297 intptr_t String::CompareTo(const String& other) const { 16325 intptr_t String::CompareTo(const String& other) const {
16298 const intptr_t this_len = this->Length(); 16326 const intptr_t this_len = this->Length();
16299 const intptr_t other_len = other.IsNull() ? 0 : other.Length(); 16327 const intptr_t other_len = other.IsNull() ? 0 : other.Length();
16300 const intptr_t len = (this_len < other_len) ? this_len : other_len; 16328 const intptr_t len = (this_len < other_len) ? this_len : other_len;
16301 for (intptr_t i = 0; i < len; i++) { 16329 for (intptr_t i = 0; i < len; i++) {
16302 int32_t this_code_point = this->CharAt(i); 16330 int32_t this_code_point = this->CharAt(i);
16303 int32_t other_code_point = other.CharAt(i); 16331 int32_t other_code_point = other.CharAt(i);
16304 if (this_code_point < other_code_point) { 16332 if (this_code_point < other_code_point) {
16305 return -1; 16333 return -1;
16306 } 16334 }
(...skipping 3067 matching lines...) Expand 10 before | Expand all | Expand 10 after
19374 return tag_label.ToCString(); 19402 return tag_label.ToCString();
19375 } 19403 }
19376 19404
19377 19405
19378 void UserTag::PrintJSONImpl(JSONStream* stream, bool ref) const { 19406 void UserTag::PrintJSONImpl(JSONStream* stream, bool ref) const {
19379 Instance::PrintJSONImpl(stream, ref); 19407 Instance::PrintJSONImpl(stream, ref);
19380 } 19408 }
19381 19409
19382 19410
19383 } // namespace dart 19411 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/object.h ('k') | runtime/vm/object_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698