Revision: 18910
Author:   [email protected]
Date:     Wed Jan 29 14:31:34 2014 UTC
Log:      Generalize internalization of substrings.

Make a template version of SubStringKey, which allows internalization of substrings of sequential and external strings.

[email protected], [email protected]

Review URL: https://codereview.chromium.org/143223004
http://code.google.com/p/v8/source/detail?r=18910

Modified:
 /branches/bleeding_edge/src/factory.cc
 /branches/bleeding_edge/src/factory.h
 /branches/bleeding_edge/src/objects-inl.h
 /branches/bleeding_edge/src/objects.cc

=======================================
--- /branches/bleeding_edge/src/factory.cc      Fri Jan 24 16:01:15 2014 UTC
+++ /branches/bleeding_edge/src/factory.cc      Wed Jan 29 14:31:34 2014 UTC
@@ -227,7 +227,7 @@

 Handle<String> Factory::InternalizeOneByteString(
     Handle<SeqOneByteString> string, int from, int length) {
-  SubStringOneByteStringKey key(string, from, length);
+  SubStringKey<uint8_t> key(string, from, length);
   return InternalizeStringWithKey(&key);
 }

@@ -244,6 +244,12 @@
                      isolate()->heap()->InternalizeStringWithKey(key),
                      String);
 }
+
+
+template Handle<String> Factory::InternalizeStringWithKey<
+    SubStringKey<uint8_t> > (SubStringKey<uint8_t>* key);
+template Handle<String> Factory::InternalizeStringWithKey<
+    SubStringKey<uint16_t> > (SubStringKey<uint16_t>* key);


 Handle<String> Factory::NewStringFromOneByte(Vector<const uint8_t> string,
=======================================
--- /branches/bleeding_edge/src/factory.h       Fri Jan 17 10:27:57 2014 UTC
+++ /branches/bleeding_edge/src/factory.h       Wed Jan 29 14:31:34 2014 UTC
@@ -99,9 +99,9 @@
   }
   Handle<String> InternalizeString(Handle<String> str);
   Handle<String> InternalizeOneByteString(Vector<const uint8_t> str);
-  Handle<String> InternalizeOneByteString(Handle<SeqOneByteString>,
-                                   int from,
-                                   int length);
+  Handle<String> InternalizeOneByteString(
+      Handle<SeqOneByteString>, int from, int length);
+
   Handle<String> InternalizeTwoByteString(Vector<const uc16> str);

   template<class StringTableKey>
=======================================
--- /branches/bleeding_edge/src/objects-inl.h   Fri Jan 24 16:01:15 2014 UTC
+++ /branches/bleeding_edge/src/objects-inl.h   Wed Jan 29 14:31:34 2014 UTC
@@ -499,38 +499,47 @@
 };


-class SubStringOneByteStringKey : public HashTableKey {
+template<class Char>
+class SubStringKey : public HashTableKey {
  public:
-  explicit SubStringOneByteStringKey(Handle<SeqOneByteString> string,
-                                     int from,
-                                     int length)
-      : string_(string), from_(from), length_(length) { }
+  SubStringKey(Handle<String> string, int from, int length)
+      : string_(string), from_(from), length_(length) {
+    if (string_->IsSlicedString()) {
+      string_ = Handle<String>(Unslice(*string_, &from_));
+    }
+    ASSERT(string_->IsSeqString() || string->IsExternalString());
+  }

   virtual uint32_t Hash() {
     ASSERT(length_ >= 0);
     ASSERT(from_ + length_ <= string_->length());
-    uint8_t* chars = string_->GetChars() + from_;
+    const Char* chars = GetChars() + from_;
     hash_field_ = StringHasher::HashSequentialString(
         chars, length_, string_->GetHeap()->HashSeed());
     uint32_t result = hash_field_ >> String::kHashShift;
ASSERT(result != 0); // Ensure that the hash value of 0 is never computed.
     return result;
   }
-

   virtual uint32_t HashForObject(Object* other) {
     return String::cast(other)->Hash();
   }

-  virtual bool IsMatch(Object* string) {
-    Vector<const uint8_t> chars(string_->GetChars() + from_, length_);
-    return String::cast(string)->IsOneByteEqualTo(chars);
-  }
-
+  virtual bool IsMatch(Object* string);
   virtual MaybeObject* AsObject(Heap* heap);

  private:
-  Handle<SeqOneByteString> string_;
+  const Char* GetChars();
+  String* Unslice(String* string, int* offset) {
+    while (string->IsSlicedString()) {
+      SlicedString* sliced = SlicedString::cast(string);
+      *offset += sliced->offset();
+      string = sliced->parent();
+    }
+    return string;
+  }
+
+  Handle<String> string_;
   int from_;
   int length_;
   uint32_t hash_field_;
=======================================
--- /branches/bleeding_edge/src/objects.cc      Mon Jan 27 12:05:47 2014 UTC
+++ /branches/bleeding_edge/src/objects.cc      Wed Jan 29 14:31:34 2014 UTC
@@ -13832,17 +13832,61 @@
 }


-MaybeObject* SubStringOneByteStringKey::AsObject(Heap* heap) {
+MaybeObject* TwoByteStringKey::AsObject(Heap* heap) {
   if (hash_field_ == 0) Hash();
-  Vector<const uint8_t> chars(string_->GetChars() + from_, length_);
+  return heap->AllocateTwoByteInternalizedString(string_, hash_field_);
+}
+
+
+template<>
+const uint8_t* SubStringKey<uint8_t>::GetChars() {
+  return string_->IsSeqOneByteString()
+      ? SeqOneByteString::cast(*string_)->GetChars()
+      : ExternalAsciiString::cast(*string_)->GetChars();
+}
+
+
+template<>
+const uint16_t* SubStringKey<uint16_t>::GetChars() {
+  return string_->IsSeqTwoByteString()
+      ? SeqTwoByteString::cast(*string_)->GetChars()
+      : ExternalTwoByteString::cast(*string_)->GetChars();
+}
+
+
+template<>
+MaybeObject* SubStringKey<uint8_t>::AsObject(Heap* heap) {
+  if (hash_field_ == 0) Hash();
+  Vector<const uint8_t> chars(GetChars() + from_, length_);
   return heap->AllocateOneByteInternalizedString(chars, hash_field_);
 }


-MaybeObject* TwoByteStringKey::AsObject(Heap* heap) {
+template<>
+MaybeObject* SubStringKey<uint16_t>::AsObject(
+    Heap* heap) {
   if (hash_field_ == 0) Hash();
-  return heap->AllocateTwoByteInternalizedString(string_, hash_field_);
+  Vector<const uint16_t> chars(GetChars() + from_, length_);
+  return heap->AllocateTwoByteInternalizedString(chars, hash_field_);
 }
+
+
+template<>
+bool SubStringKey<uint8_t>::IsMatch(Object* string) {
+  Vector<const uint8_t> chars(GetChars() + from_, length_);
+  return String::cast(string)->IsOneByteEqualTo(chars);
+}
+
+
+template<>
+bool SubStringKey<uint16_t>::IsMatch(Object* string) {
+  Vector<const uint16_t> chars(GetChars() + from_, length_);
+  return String::cast(string)->IsTwoByteEqualTo(chars);
+}
+
+
+template class SubStringKey<uint8_t>;
+template class SubStringKey<uint16_t>;


// InternalizedStringKey carries a string/internalized-string object as key.

--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to