Revision: 4400
Author: [email protected]
Date: Tue Apr 13 10:00:33 2010
Log: Expose a hint which communicates that string might be written many times.

Review URL: http://codereview.chromium.org/1609021
http://code.google.com/p/v8/source/detail?r=4400

Modified:
 /branches/bleeding_edge/include/v8.h
 /branches/bleeding_edge/src/api.cc
 /branches/bleeding_edge/test/cctest/test-strings.cc

=======================================
--- /branches/bleeding_edge/include/v8.h        Thu Apr  8 11:23:10 2010
+++ /branches/bleeding_edge/include/v8.h        Tue Apr 13 10:00:33 2010
@@ -859,18 +859,23 @@
    * \return The number of bytes copied to the buffer
    * excluding the NULL terminator.
    */
- int Write(uint16_t* buffer, int start = 0, int length = -1) const; // UTF-16 - int WriteAscii(char* buffer, int start = 0, int length = -1) const; // ASCII
+  enum WriteHints {
+    NO_HINTS = 0,
+    HINT_MANY_WRITES_EXPECTED = 1
+  };
+
+  int Write(uint16_t* buffer,
+            int start = 0,
+            int length = -1,
+            WriteHints hints = NO_HINTS) const;  // UTF-16
+  int WriteAscii(char* buffer,
+                 int start = 0,
+                 int length = -1,
+                 WriteHints hints = NO_HINTS) const;  // ASCII
   int WriteUtf8(char* buffer,
                 int length = -1,
-                int* nchars_ref = NULL) const; // UTF-8
-
-  /**
-   * Flatten internal memory. Operations on the string tend to run faster
-   * after flattening especially if the string is a concatenation of many
-   * others.
-   */
-  void Flatten();
+                int* nchars_ref = NULL,
+                WriteHints hints = NO_HINTS) const; // UTF-8

   /**
    * A zero length string.
=======================================
--- /branches/bleeding_edge/src/api.cc  Thu Apr  8 11:23:10 2010
+++ /branches/bleeding_edge/src/api.cc  Tue Apr 13 10:00:33 2010
@@ -2641,12 +2641,20 @@
 }


-int String::WriteUtf8(char* buffer, int capacity, int* nchars_ref) const {
+int String::WriteUtf8(char* buffer,
+                      int capacity,
+                      int* nchars_ref,
+                      WriteHints hints) const {
   if (IsDeadCheck("v8::String::WriteUtf8()")) return 0;
   LOG_API("String::WriteUtf8");
   ENTER_V8;
   i::Handle<i::String> str = Utils::OpenHandle(this);
   StringTracker::RecordWrite(str);
+  if (hints & HINT_MANY_WRITES_EXPECTED) {
+    // Flatten the string for efficiency.  This applies whether we are
+    // using StringInputBuffer or Get(i) to access the characters.
+    str->TryFlatten();
+  }
   write_input_buffer.Reset(0, *str);
   int len = str->length();
   // Encode the first K - 3 bytes directly into the buffer since we
@@ -2688,16 +2696,21 @@
 }


-int String::WriteAscii(char* buffer, int start, int length) const {
+int String::WriteAscii(char* buffer,
+                       int start,
+                       int length,
+                       WriteHints hints) const {
   if (IsDeadCheck("v8::String::WriteAscii()")) return 0;
   LOG_API("String::WriteAscii");
   ENTER_V8;
   ASSERT(start >= 0 && length >= -1);
   i::Handle<i::String> str = Utils::OpenHandle(this);
   StringTracker::RecordWrite(str);
-  // Flatten the string for efficiency.  This applies whether we are
-  // using StringInputBuffer or Get(i) to access the characters.
-  str->TryFlatten();
+  if (hints & HINT_MANY_WRITES_EXPECTED) {
+    // Flatten the string for efficiency.  This applies whether we are
+    // using StringInputBuffer or Get(i) to access the characters.
+    str->TryFlatten();
+  }
   int end = length;
   if ( (length == -1) || (length > str->length() - start) )
     end = str->length() - start;
@@ -2715,13 +2728,21 @@
 }


-int String::Write(uint16_t* buffer, int start, int length) const {
+int String::Write(uint16_t* buffer,
+                  int start,
+                  int length,
+                  WriteHints hints) const {
   if (IsDeadCheck("v8::String::Write()")) return 0;
   LOG_API("String::Write");
   ENTER_V8;
   ASSERT(start >= 0 && length >= -1);
   i::Handle<i::String> str = Utils::OpenHandle(this);
   StringTracker::RecordWrite(str);
+  if (hints & HINT_MANY_WRITES_EXPECTED) {
+    // Flatten the string for efficiency.  This applies whether we are
+    // using StringInputBuffer or Get(i) to access the characters.
+    str->TryFlatten();
+  }
   int end = length;
   if ( (length == -1) || (length > str->length() - start) )
     end = str->length() - start;
@@ -2731,13 +2752,6 @@
     buffer[end] = '\0';
   return end;
 }
-
-
-void v8::String::Flatten() {
-  if (IsDeadCheck("v8::String::Flatten()")) return;
-  i::Handle<i::String> str = Utils::OpenHandle(this);
-  i::FlattenString(str);
-}


 bool v8::String::IsExternal() const {
=======================================
--- /branches/bleeding_edge/test/cctest/test-strings.cc Thu Apr 8 11:23:10 2010 +++ /branches/bleeding_edge/test/cctest/test-strings.cc Tue Apr 13 10:00:33 2010
@@ -345,38 +345,6 @@
       CHECK_EQ(kNoChar, buffer[j]);
   }
 }
-
-
-TEST(StringConcatFlatten) {
-  InitializeVM();
-  v8::HandleScope handle_scope;
-
-  const char* stringA = "0123456789";
-  const char* stringB = "ABCDEFGHIJ";
-
-  v8::Local<v8::String> a = v8::String::New(stringA);
-  v8::Local<v8::String> b = v8::String::New(stringB);
-
-  v8::Local<v8::String> cons = v8::String::Concat(a, b);
-
-  i::Handle<i::String> str = v8::Utils::OpenHandle(*cons);
-  CHECK(!str->IsFlat());
-
-  cons->Flatten();
-
-  CHECK(str->IsFlat());
-
-  char buffer[21];
-  cons->WriteUtf8(buffer);
-
-  for (int i = 0; i < 10; i++) {
-    CHECK_EQ(stringA[i], buffer[i]);
-  }
-
-  for (int i = 0; i < 10; i++) {
-    CHECK_EQ(stringB[i], buffer[i + 10]);
-  }
-}


 TEST(ExternalShortStringAdd) {

--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

To unsubscribe, reply using "remove me" as the subject.

Reply via email to