Reviewers: Yang,

Description:
do aligned reads in ContainsOnlyOneByte

[email protected]
BUG=

Please review this at https://codereview.chromium.org/16147004/

SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge

Affected files:
  M src/api.cc


Index: src/api.cc
diff --git a/src/api.cc b/src/api.cc
index 4cdd77ef05a6908c5cc5ebf0fa3398ab0c1f3e72..c950bb8be9825ce0aeaf47750709362c07cf471c 100644
--- a/src/api.cc
+++ b/src/api.cc
@@ -4304,6 +4304,25 @@ bool String::IsOneByte() const {
 }


+// Helpers for ContainsOnlyOneByteHelper
+template<size_t size> struct OneByteMask;
+template<> struct OneByteMask<4> {
+  static const uint32_t value = 0xFF00FF00U;
+};
+template<> struct OneByteMask<8> {
+  static const uint64_t value = 0xFF00FF00FF00ULL;
+};
+static const uintptr_t kOneByteMask = OneByteMask<sizeof(uintptr_t)>::value;
+static const uintptr_t kAlignmentMask = sizeof(uintptr_t) - 1;
+static inline bool Unaligned(const uint16_t* chars) {
+  return reinterpret_cast<const uintptr_t>(chars) & kAlignmentMask;
+}
+static inline const uint16_t* Align(const uint16_t* chars) {
+  return reinterpret_cast<uint16_t*>(
+      reinterpret_cast<uintptr_t>(chars) & ~kAlignmentMask);
+}
+
+
 class ContainsOnlyOneByteHelper {
  public:
   ContainsOnlyOneByteHelper() : is_one_byte_(true) {}
@@ -4315,14 +4334,26 @@ class ContainsOnlyOneByteHelper {
   void VisitOneByteString(const uint8_t* chars, int length) {
     // Nothing to do.
   }
-  // TODO(dcarney): do word aligned read.
   void VisitTwoByteString(const uint16_t* chars, int length) {
-    // Check whole string without breaking.
-    uint16_t total = 0;
-    for (int i = 0; i < length; i++) {
-      total |= chars[i] >> 8;
+    // Accumulated bits.
+    uintptr_t acc = 0;
+    // Align to uintptr_t.
+    const uint16_t* end = chars + length;
+    while (Unaligned(chars) && chars != end) {
+        acc |= *chars++;
+    }
+    // Read word aligned.
+    const uint16_t* aligned_end = Align(end);
+    while (chars < aligned_end) {
+        acc |= *reinterpret_cast<const uintptr_t*>(chars);
+        chars += sizeof(uintptr_t)/sizeof(uint16_t);
+    }
+    // Read the rest.
+    while (chars != end) {
+      acc |= *chars++;
     }
-    if (total != 0) is_one_byte_ = false;
+    // Check result.
+    if ((acc & kOneByteMask) != 0) is_one_byte_ = false;
   }

  private:


--
--
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