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

Side by Side Diff: src/objects.cc

Issue 140953002: Refactor string internalization. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Add virtual for real Created 6 years, 11 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 | « src/objects.h ('k') | src/objects-inl.h » ('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 2013 the V8 project authors. All rights reserved. 1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 13982 matching lines...) Expand 10 before | Expand all | Expand 10 after
13993 13993
13994 static uint32_t RegExpHash(String* string, Smi* flags) { 13994 static uint32_t RegExpHash(String* string, Smi* flags) {
13995 return string->Hash() + flags->value(); 13995 return string->Hash() + flags->value();
13996 } 13996 }
13997 13997
13998 String* string_; 13998 String* string_;
13999 Smi* flags_; 13999 Smi* flags_;
14000 }; 14000 };
14001 14001
14002 14002
14003 // Utf8StringKey carries a vector of chars as key. 14003 MaybeObject* OneByteStringKey::AsObject(Heap* heap) {
14004 class Utf8StringKey : public HashTableKey { 14004 if (hash_field_ == 0) Hash();
14005 public: 14005 return heap->AllocateOneByteInternalizedString(string_, hash_field_);
14006 explicit Utf8StringKey(Vector<const char> string, uint32_t seed) 14006 }
14007 : string_(string), hash_field_(0), seed_(seed) { }
14008
14009 bool IsMatch(Object* string) {
14010 return String::cast(string)->IsUtf8EqualTo(string_);
14011 }
14012
14013 uint32_t Hash() {
14014 if (hash_field_ != 0) return hash_field_ >> String::kHashShift;
14015 hash_field_ = StringHasher::ComputeUtf8Hash(string_, seed_, &chars_);
14016 uint32_t result = hash_field_ >> String::kHashShift;
14017 ASSERT(result != 0); // Ensure that the hash value of 0 is never computed.
14018 return result;
14019 }
14020
14021 uint32_t HashForObject(Object* other) {
14022 return String::cast(other)->Hash();
14023 }
14024
14025 MaybeObject* AsObject(Heap* heap) {
14026 if (hash_field_ == 0) Hash();
14027 return heap->AllocateInternalizedStringFromUtf8(string_,
14028 chars_,
14029 hash_field_);
14030 }
14031
14032 Vector<const char> string_;
14033 uint32_t hash_field_;
14034 int chars_; // Caches the number of characters when computing the hash code.
14035 uint32_t seed_;
14036 };
14037 14007
14038 14008
14039 template <typename Char> 14009 MaybeObject* SubStringOneByteStringKey::AsObject(Heap* heap) {
14040 class SequentialStringKey : public HashTableKey { 14010 if (hash_field_ == 0) Hash();
14041 public: 14011 Vector<const uint8_t> chars(string_->GetChars() + from_, length_);
14042 explicit SequentialStringKey(Vector<const Char> string, uint32_t seed) 14012 return heap->AllocateOneByteInternalizedString(chars, hash_field_);
14043 : string_(string), hash_field_(0), seed_(seed) { } 14013 }
14044
14045 uint32_t Hash() {
14046 hash_field_ = StringHasher::HashSequentialString<Char>(string_.start(),
14047 string_.length(),
14048 seed_);
14049
14050 uint32_t result = hash_field_ >> String::kHashShift;
14051 ASSERT(result != 0); // Ensure that the hash value of 0 is never computed.
14052 return result;
14053 }
14054 14014
14055 14015
14056 uint32_t HashForObject(Object* other) { 14016 MaybeObject* TwoByteStringKey::AsObject(Heap* heap) {
14057 return String::cast(other)->Hash(); 14017 if (hash_field_ == 0) Hash();
14058 } 14018 return heap->AllocateTwoByteInternalizedString(string_, hash_field_);
14059 14019 }
14060 Vector<const Char> string_;
14061 uint32_t hash_field_;
14062 uint32_t seed_;
14063 };
14064
14065
14066
14067 class OneByteStringKey : public SequentialStringKey<uint8_t> {
14068 public:
14069 OneByteStringKey(Vector<const uint8_t> str, uint32_t seed)
14070 : SequentialStringKey<uint8_t>(str, seed) { }
14071
14072 bool IsMatch(Object* string) {
14073 return String::cast(string)->IsOneByteEqualTo(string_);
14074 }
14075
14076 MaybeObject* AsObject(Heap* heap) {
14077 if (hash_field_ == 0) Hash();
14078 return heap->AllocateOneByteInternalizedString(string_, hash_field_);
14079 }
14080 };
14081
14082
14083 class SubStringOneByteStringKey : public HashTableKey {
14084 public:
14085 explicit SubStringOneByteStringKey(Handle<SeqOneByteString> string,
14086 int from,
14087 int length)
14088 : string_(string), from_(from), length_(length) { }
14089
14090 uint32_t Hash() {
14091 ASSERT(length_ >= 0);
14092 ASSERT(from_ + length_ <= string_->length());
14093 uint8_t* chars = string_->GetChars() + from_;
14094 hash_field_ = StringHasher::HashSequentialString(
14095 chars, length_, string_->GetHeap()->HashSeed());
14096 uint32_t result = hash_field_ >> String::kHashShift;
14097 ASSERT(result != 0); // Ensure that the hash value of 0 is never computed.
14098 return result;
14099 }
14100
14101
14102 uint32_t HashForObject(Object* other) {
14103 return String::cast(other)->Hash();
14104 }
14105
14106 bool IsMatch(Object* string) {
14107 Vector<const uint8_t> chars(string_->GetChars() + from_, length_);
14108 return String::cast(string)->IsOneByteEqualTo(chars);
14109 }
14110
14111 MaybeObject* AsObject(Heap* heap) {
14112 if (hash_field_ == 0) Hash();
14113 Vector<const uint8_t> chars(string_->GetChars() + from_, length_);
14114 return heap->AllocateOneByteInternalizedString(chars, hash_field_);
14115 }
14116
14117 private:
14118 Handle<SeqOneByteString> string_;
14119 int from_;
14120 int length_;
14121 uint32_t hash_field_;
14122 };
14123
14124
14125 class TwoByteStringKey : public SequentialStringKey<uc16> {
14126 public:
14127 explicit TwoByteStringKey(Vector<const uc16> str, uint32_t seed)
14128 : SequentialStringKey<uc16>(str, seed) { }
14129
14130 bool IsMatch(Object* string) {
14131 return String::cast(string)->IsTwoByteEqualTo(string_);
14132 }
14133
14134 MaybeObject* AsObject(Heap* heap) {
14135 if (hash_field_ == 0) Hash();
14136 return heap->AllocateTwoByteInternalizedString(string_, hash_field_);
14137 }
14138 };
14139 14020
14140 14021
14141 // InternalizedStringKey carries a string/internalized-string object as key. 14022 // InternalizedStringKey carries a string/internalized-string object as key.
14142 class InternalizedStringKey : public HashTableKey { 14023 class InternalizedStringKey : public HashTableKey {
14143 public: 14024 public:
14144 explicit InternalizedStringKey(String* string) 14025 explicit InternalizedStringKey(String* string)
14145 : string_(string) { } 14026 : string_(string) { }
14146 14027
14147 bool IsMatch(Object* string) { 14028 bool IsMatch(Object* string) {
14148 return String::cast(string)->Equals(string_); 14029 return String::cast(string)->Equals(string_);
(...skipping 1043 matching lines...) Expand 10 before | Expand all | Expand 10 after
15192 if (entry == kNotFound) { 15073 if (entry == kNotFound) {
15193 return false; 15074 return false;
15194 } else { 15075 } else {
15195 *result = String::cast(KeyAt(entry)); 15076 *result = String::cast(KeyAt(entry));
15196 ASSERT(StringShape(*result).IsInternalized()); 15077 ASSERT(StringShape(*result).IsInternalized());
15197 return true; 15078 return true;
15198 } 15079 }
15199 } 15080 }
15200 15081
15201 15082
15202 MaybeObject* StringTable::LookupUtf8String(Vector<const char> str,
15203 Object** s) {
15204 Utf8StringKey key(str, GetHeap()->HashSeed());
15205 return LookupKey(&key, s);
15206 }
15207
15208
15209 MaybeObject* StringTable::LookupOneByteString(Vector<const uint8_t> str,
15210 Object** s) {
15211 OneByteStringKey key(str, GetHeap()->HashSeed());
15212 return LookupKey(&key, s);
15213 }
15214
15215
15216 MaybeObject* StringTable::LookupSubStringOneByteString(
15217 Handle<SeqOneByteString> str,
15218 int from,
15219 int length,
15220 Object** s) {
15221 SubStringOneByteStringKey key(str, from, length);
15222 return LookupKey(&key, s);
15223 }
15224
15225
15226 MaybeObject* StringTable::LookupTwoByteString(Vector<const uc16> str,
15227 Object** s) {
15228 TwoByteStringKey key(str, GetHeap()->HashSeed());
15229 return LookupKey(&key, s);
15230 }
15231
15232
15233 MaybeObject* StringTable::LookupKey(HashTableKey* key, Object** s) { 15083 MaybeObject* StringTable::LookupKey(HashTableKey* key, Object** s) {
15234 int entry = FindEntry(key); 15084 int entry = FindEntry(key);
15235 15085
15236 // String already in table. 15086 // String already in table.
15237 if (entry != kNotFound) { 15087 if (entry != kNotFound) {
15238 *s = KeyAt(entry); 15088 *s = KeyAt(entry);
15239 return this; 15089 return this;
15240 } 15090 }
15241 15091
15242 // Adding new string. Grow table if needed. 15092 // Adding new string. Grow table if needed.
(...skipping 1517 matching lines...) Expand 10 before | Expand all | Expand 10 after
16760 #define ERROR_MESSAGES_TEXTS(C, T) T, 16610 #define ERROR_MESSAGES_TEXTS(C, T) T,
16761 static const char* error_messages_[] = { 16611 static const char* error_messages_[] = {
16762 ERROR_MESSAGES_LIST(ERROR_MESSAGES_TEXTS) 16612 ERROR_MESSAGES_LIST(ERROR_MESSAGES_TEXTS)
16763 }; 16613 };
16764 #undef ERROR_MESSAGES_TEXTS 16614 #undef ERROR_MESSAGES_TEXTS
16765 return error_messages_[reason]; 16615 return error_messages_[reason];
16766 } 16616 }
16767 16617
16768 16618
16769 } } // namespace v8::internal 16619 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/objects.h ('k') | src/objects-inl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698