Title: [226068] trunk/Source
Revision
226068
Author
fpi...@apple.com
Date
2017-12-18 12:15:41 -0800 (Mon, 18 Dec 2017)

Log Message

Vector index masking
https://bugs.webkit.org/show_bug.cgi?id=180909

Reviewed by Keith Miller.
        
Source/_javascript_Core:

Adopt index masking for strings.

* dfg/DFGSpeculativeJIT.cpp:
(JSC::DFG::SpeculativeJIT::compileGetCharCodeAt):
(JSC::DFG::SpeculativeJIT::compileGetByValOnString):
* ftl/FTLAbstractHeapRepository.h:
* ftl/FTLLowerDFGToB3.cpp:
(JSC::FTL::DFG::LowerDFGToB3::compileStringCharAt):
(JSC::FTL::DFG::LowerDFGToB3::compileStringCharCodeAt):
* jit/ThunkGenerators.cpp:
(JSC::stringCharLoad):

Source/WTF:

Adds index masking to StringImpl and Vector.
        
Perf:
        
- 0.4% slower on microbenchmarks.
- Octane totally even.
- Kraken may be 0.8% slower.
- Speedometer is 0.8% slower with p = 0.008.
- membuster is even:
    - snap2pre: 0.7% bigger w/ p = 0.26,
    - snap2post: 0.3% smaller w/ p = 0.81,
    - snap3pre: 1.2% bigger w/ p = 0.63,
    - snap3post: 0.4% smaller w/ p = 0.76.

* wtf/MathExtras.h:
(WTF::roundUpToPowerOfTwo):
(WTF::maskForSize):
* wtf/SizeLimits.cpp:
* wtf/Vector.h:
(WTF::VectorBufferBase::allocateBuffer):
(WTF::VectorBufferBase::tryAllocateBuffer):
(WTF::VectorBufferBase::reallocateBuffer):
(WTF::VectorBufferBase::deallocateBuffer):
(WTF::VectorBufferBase::releaseBuffer):
(WTF::VectorBufferBase::VectorBufferBase):
(WTF::VectorBufferBase::updateMask):
(WTF::VectorBuffer::allocateBuffer):
(WTF::VectorBuffer::tryAllocateBuffer):
(WTF::VectorBuffer::swap):
(WTF::VectorBuffer::restoreInlineBufferIfNeeded):
(WTF::Vector::at):
(WTF::Vector::at const):
* wtf/text/StringImpl.h:
(WTF::StringImpl::maskOffset):
(WTF::StringImpl::mask const):
(WTF::StringImplShape::StringImplShape):
(WTF::StringImpl::at const):
(WTF::StringImpl::tailOffset):
* wtf/text/StringView.h:
(WTF::StringView::StringView):
(WTF::StringView::operator=):
(WTF::StringView::initialize):
(WTF::StringView::clear):
(WTF::StringView::operator[] const):
* wtf/text/WTFString.h:
(WTF::String::mask const):

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (226067 => 226068)


--- trunk/Source/_javascript_Core/ChangeLog	2017-12-18 20:13:56 UTC (rev 226067)
+++ trunk/Source/_javascript_Core/ChangeLog	2017-12-18 20:15:41 UTC (rev 226068)
@@ -1,3 +1,22 @@
+2017-12-16  Filip Pizlo  <fpi...@apple.com>
+
+        Vector index masking
+        https://bugs.webkit.org/show_bug.cgi?id=180909
+
+        Reviewed by Keith Miller.
+        
+        Adopt index masking for strings.
+
+        * dfg/DFGSpeculativeJIT.cpp:
+        (JSC::DFG::SpeculativeJIT::compileGetCharCodeAt):
+        (JSC::DFG::SpeculativeJIT::compileGetByValOnString):
+        * ftl/FTLAbstractHeapRepository.h:
+        * ftl/FTLLowerDFGToB3.cpp:
+        (JSC::FTL::DFG::LowerDFGToB3::compileStringCharAt):
+        (JSC::FTL::DFG::LowerDFGToB3::compileStringCharCodeAt):
+        * jit/ThunkGenerators.cpp:
+        (JSC::stringCharLoad):
+
 2017-12-17  Yusuke Suzuki  <utatane....@gmail.com>
 
         [FTL] NewArrayBuffer should be sinked if it is only used for spreading

Modified: trunk/Source/_javascript_Core/dfg/DFGSpeculativeJIT.cpp (226067 => 226068)


--- trunk/Source/_javascript_Core/dfg/DFGSpeculativeJIT.cpp	2017-12-18 20:13:56 UTC (rev 226067)
+++ trunk/Source/_javascript_Core/dfg/DFGSpeculativeJIT.cpp	2017-12-18 20:15:41 UTC (rev 226068)
@@ -2125,6 +2125,7 @@
     GPRReg scratchReg = scratch.gpr();
 
     m_jit.loadPtr(MacroAssembler::Address(stringReg, JSString::offsetOfValue()), scratchReg);
+    m_jit.and32(MacroAssembler::Address(scratchReg, StringImpl::maskOffset()), indexReg);
 
     // Load the character into scratchReg
     JITCompiler::Jump is16Bit = m_jit.branchTest32(MacroAssembler::Zero, MacroAssembler::Address(scratchReg, StringImpl::flagsOffset()), TrustedImm32(StringImpl::flagIs8Bit()));
@@ -2172,6 +2173,7 @@
         speculationCheck(OutOfBounds, JSValueRegs(), 0, outOfBounds);
 
     m_jit.loadPtr(MacroAssembler::Address(baseReg, JSString::offsetOfValue()), scratchReg);
+    m_jit.and32(MacroAssembler::Address(scratchReg, StringImpl::maskOffset()), propertyReg);
 
     // Load the character into scratchReg
     JITCompiler::Jump is16Bit = m_jit.branchTest32(MacroAssembler::Zero, MacroAssembler::Address(scratchReg, StringImpl::flagsOffset()), TrustedImm32(StringImpl::flagIs8Bit()));

Modified: trunk/Source/_javascript_Core/ftl/FTLAbstractHeapRepository.h (226067 => 226068)


--- trunk/Source/_javascript_Core/ftl/FTLAbstractHeapRepository.h	2017-12-18 20:13:56 UTC (rev 226067)
+++ trunk/Source/_javascript_Core/ftl/FTLAbstractHeapRepository.h	2017-12-18 20:15:41 UTC (rev 226068)
@@ -108,6 +108,7 @@
     macro(StringImpl_data, StringImpl::dataOffset()) \
     macro(StringImpl_hashAndFlags, StringImpl::flagsOffset()) \
     macro(StringImpl_length, StringImpl::lengthMemoryOffset()) \
+    macro(StringImpl_mask, StringImpl::maskOffset()) \
     macro(Structure_classInfo, Structure::classInfoOffset()) \
     macro(Structure_globalObject, Structure::globalObjectOffset()) \
     macro(Structure_prototype, Structure::prototypeOffset()) \

Modified: trunk/Source/_javascript_Core/ftl/FTLLowerDFGToB3.cpp (226067 => 226068)


--- trunk/Source/_javascript_Core/ftl/FTLLowerDFGToB3.cpp	2017-12-18 20:13:56 UTC (rev 226067)
+++ trunk/Source/_javascript_Core/ftl/FTLLowerDFGToB3.cpp	2017-12-18 20:15:41 UTC (rev 226068)
@@ -5903,6 +5903,7 @@
         LBasicBlock lastNext = m_out.appendTo(fastPath, slowPath);
             
         LValue stringImpl = m_out.loadPtr(base, m_heaps.JSString_value);
+        LValue indexForAccess = m_out.bitAnd(index, m_out.load32(stringImpl, m_heaps.StringImpl_mask));
             
         LBasicBlock is8Bit = m_out.newBlock();
         LBasicBlock is16Bit = m_out.newBlock();
@@ -5921,7 +5922,7 @@
         // https://bugs.webkit.org/show_bug.cgi?id=174924
         ValueFromBlock char8Bit = m_out.anchor(
             m_out.load8ZeroExt32(m_out.baseIndex(
-                m_heaps.characters8, storage, m_out.zeroExtPtr(index),
+                m_heaps.characters8, storage, m_out.zeroExtPtr(indexForAccess),
                 provenValue(m_node->child2()))));
         m_out.jump(bitsContinuation);
             
@@ -5929,7 +5930,7 @@
 
         LValue char16BitValue = m_out.load16ZeroExt32(
             m_out.baseIndex(
-                m_heaps.characters16, storage, m_out.zeroExtPtr(index),
+                m_heaps.characters16, storage, m_out.zeroExtPtr(indexForAccess),
                 provenValue(m_node->child2())));
         ValueFromBlock char16Bit = m_out.anchor(char16BitValue);
         m_out.branch(
@@ -6011,6 +6012,7 @@
                 index, m_out.load32NonNegative(base, m_heaps.JSString_length)));
         
         LValue stringImpl = m_out.loadPtr(base, m_heaps.JSString_value);
+        LValue indexForAccess = m_out.bitAnd(index, m_out.load32(stringImpl, m_heaps.StringImpl_mask));
         
         m_out.branch(
             m_out.testIsZero32(
@@ -6024,7 +6026,7 @@
         // https://bugs.webkit.org/show_bug.cgi?id=174924
         ValueFromBlock char8Bit = m_out.anchor(
             m_out.load8ZeroExt32(m_out.baseIndex(
-                m_heaps.characters8, storage, m_out.zeroExtPtr(index),
+                m_heaps.characters8, storage, m_out.zeroExtPtr(indexForAccess),
                 provenValue(m_node->child2()))));
         m_out.jump(continuation);
             
@@ -6032,7 +6034,7 @@
             
         ValueFromBlock char16Bit = m_out.anchor(
             m_out.load16ZeroExt32(m_out.baseIndex(
-                m_heaps.characters16, storage, m_out.zeroExtPtr(index),
+                m_heaps.characters16, storage, m_out.zeroExtPtr(indexForAccess),
                 provenValue(m_node->child2()))));
         m_out.jump(continuation);
         

Modified: trunk/Source/_javascript_Core/jit/ThunkGenerators.cpp (226067 => 226068)


--- trunk/Source/_javascript_Core/jit/ThunkGenerators.cpp	2017-12-18 20:13:56 UTC (rev 226067)
+++ trunk/Source/_javascript_Core/jit/ThunkGenerators.cpp	2017-12-18 20:15:41 UTC (rev 226068)
@@ -612,6 +612,7 @@
     SpecializedThunkJIT::JumpList cont8Bit;
     // Load the string flags
     jit.loadPtr(MacroAssembler::Address(SpecializedThunkJIT::regT0, StringImpl::flagsOffset()), SpecializedThunkJIT::regT2);
+    jit.and32(MacroAssembler::Address(SpecializedThunkJIT::regT0, StringImpl::maskOffset()), SpecializedThunkJIT::regT1);
     jit.loadPtr(MacroAssembler::Address(SpecializedThunkJIT::regT0, StringImpl::dataOffset()), SpecializedThunkJIT::regT0);
     is16Bit.append(jit.branchTest32(MacroAssembler::Zero, SpecializedThunkJIT::regT2, MacroAssembler::TrustedImm32(StringImpl::flagIs8Bit())));
     jit.load8(MacroAssembler::BaseIndex(SpecializedThunkJIT::regT0, SpecializedThunkJIT::regT1, MacroAssembler::TimesOne, 0), SpecializedThunkJIT::regT0);

Modified: trunk/Source/WTF/ChangeLog (226067 => 226068)


--- trunk/Source/WTF/ChangeLog	2017-12-18 20:13:56 UTC (rev 226067)
+++ trunk/Source/WTF/ChangeLog	2017-12-18 20:15:41 UTC (rev 226068)
@@ -1,3 +1,57 @@
+2017-12-16  Filip Pizlo  <fpi...@apple.com>
+
+        Vector index masking
+        https://bugs.webkit.org/show_bug.cgi?id=180909
+
+        Reviewed by Keith Miller.
+        
+        Adds index masking to StringImpl and Vector.
+        
+        Perf:
+        
+        - 0.4% slower on microbenchmarks.
+        - Octane totally even.
+        - Kraken may be 0.8% slower.
+        - Speedometer is 0.8% slower with p = 0.008.
+        - membuster is even:
+            - snap2pre: 0.7% bigger w/ p = 0.26,
+            - snap2post: 0.3% smaller w/ p = 0.81,
+            - snap3pre: 1.2% bigger w/ p = 0.63,
+            - snap3post: 0.4% smaller w/ p = 0.76.
+
+        * wtf/MathExtras.h:
+        (WTF::roundUpToPowerOfTwo):
+        (WTF::maskForSize):
+        * wtf/SizeLimits.cpp:
+        * wtf/Vector.h:
+        (WTF::VectorBufferBase::allocateBuffer):
+        (WTF::VectorBufferBase::tryAllocateBuffer):
+        (WTF::VectorBufferBase::reallocateBuffer):
+        (WTF::VectorBufferBase::deallocateBuffer):
+        (WTF::VectorBufferBase::releaseBuffer):
+        (WTF::VectorBufferBase::VectorBufferBase):
+        (WTF::VectorBufferBase::updateMask):
+        (WTF::VectorBuffer::allocateBuffer):
+        (WTF::VectorBuffer::tryAllocateBuffer):
+        (WTF::VectorBuffer::swap):
+        (WTF::VectorBuffer::restoreInlineBufferIfNeeded):
+        (WTF::Vector::at):
+        (WTF::Vector::at const):
+        * wtf/text/StringImpl.h:
+        (WTF::StringImpl::maskOffset):
+        (WTF::StringImpl::mask const):
+        (WTF::StringImplShape::StringImplShape):
+        (WTF::StringImpl::at const):
+        (WTF::StringImpl::tailOffset):
+        * wtf/text/StringView.h:
+        (WTF::StringView::StringView):
+        (WTF::StringView::operator=):
+        (WTF::StringView::initialize):
+        (WTF::StringView::clear):
+        (WTF::StringView::operator[] const):
+        * wtf/text/WTFString.h:
+        (WTF::String::mask const):
+
 2017-12-18  Claudio Saavedra  <csaave...@igalia.com>
 
         [GTK][Darwin] Do not define mach exceptions for GTK+

Modified: trunk/Source/WTF/wtf/MathExtras.h (226067 => 226068)


--- trunk/Source/WTF/wtf/MathExtras.h	2017-12-18 20:13:56 UTC (rev 226067)
+++ trunk/Source/WTF/wtf/MathExtras.h	2017-12-18 20:15:41 UTC (rev 226068)
@@ -334,7 +334,7 @@
 namespace WTF {
 
 // From http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
-inline uint32_t roundUpToPowerOfTwo(uint32_t v)
+inline constexpr uint32_t roundUpToPowerOfTwo(uint32_t v)
 {
     v--;
     v |= v >> 1;
@@ -346,6 +346,13 @@
     return v;
 }
 
+inline constexpr unsigned maskForSize(unsigned size)
+{
+    if (!size)
+        return 0;
+    return roundUpToPowerOfTwo(size) - 1;
+}
+
 inline unsigned fastLog2(unsigned i)
 {
     unsigned log2 = 0;

Modified: trunk/Source/WTF/wtf/SizeLimits.cpp (226067 => 226068)


--- trunk/Source/WTF/wtf/SizeLimits.cpp	2017-12-18 20:13:56 UTC (rev 226067)
+++ trunk/Source/WTF/wtf/SizeLimits.cpp	2017-12-18 20:15:41 UTC (rev 226068)
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2010 Google Inc. All rights reserved.
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -62,14 +63,22 @@
 
 template<typename T>
 struct SameSizeAsVectorWithInlineCapacity<T, 0> {
+    WTF_MAKE_NONCOPYABLE(SameSizeAsVectorWithInlineCapacity);
+public:
     void* bufferPointer;
     unsigned capacity;
     unsigned size;
+    unsigned mask;
 };
 
+template<typename T>
+struct SameSizeAsVectorWithInlineCapacityBase : SameSizeAsVectorWithInlineCapacity<T> {
+};
+
 template<typename T, unsigned inlineCapacity>
-struct SameSizeAsVectorWithInlineCapacity {
-    SameSizeAsVectorWithInlineCapacity<T, 0> baseCapacity;
+struct SameSizeAsVectorWithInlineCapacity : SameSizeAsVectorWithInlineCapacityBase<T> {
+    WTF_MAKE_NONCOPYABLE(SameSizeAsVectorWithInlineCapacity);
+public:
     typename std::aligned_storage<sizeof(T), std::alignment_of<T>::value>::type inlineBuffer[inlineCapacity];
 };
 

Modified: trunk/Source/WTF/wtf/Vector.h (226067 => 226068)


--- trunk/Source/WTF/wtf/Vector.h	2017-12-18 20:13:56 UTC (rev 226067)
+++ trunk/Source/WTF/wtf/Vector.h	2017-12-18 20:15:41 UTC (rev 226068)
@@ -30,6 +30,7 @@
 #include <wtf/FastMalloc.h>
 #include <wtf/Forward.h>
 #include <wtf/MallocPtr.h>
+#include <wtf/MathExtras.h>
 #include <wtf/Noncopyable.h>
 #include <wtf/NotFound.h>
 #include <wtf/StdLibExtras.h>
@@ -266,6 +267,7 @@
             CRASH();
         size_t sizeToAllocate = newCapacity * sizeof(T);
         m_capacity = sizeToAllocate / sizeof(T);
+        updateMask();
         m_buffer = static_cast<T*>(Malloc::malloc(sizeToAllocate));
     }
 
@@ -280,6 +282,7 @@
         if (!newBuffer)
             return false;
         m_capacity = sizeToAllocate / sizeof(T);
+        updateMask();
         m_buffer = newBuffer;
         return true;
     }
@@ -296,6 +299,7 @@
             CRASH();
         size_t sizeToAllocate = newCapacity * sizeof(T);
         m_capacity = sizeToAllocate / sizeof(T);
+        updateMask();
         m_buffer = static_cast<T*>(Malloc::realloc(m_buffer, sizeToAllocate));
     }
 
@@ -307,6 +311,7 @@
         if (m_buffer == bufferToDeallocate) {
             m_buffer = 0;
             m_capacity = 0;
+            m_mask = 0;
         }
 
         Malloc::free(bufferToDeallocate);
@@ -322,6 +327,7 @@
         T* buffer = m_buffer;
         m_buffer = 0;
         m_capacity = 0;
+        m_mask = 0;
         return adoptMallocPtr(buffer);
     }
 
@@ -330,6 +336,7 @@
         : m_buffer(0)
         , m_capacity(0)
         , m_size(0)
+        , m_mask(0)
     {
     }
 
@@ -337,7 +344,9 @@
         : m_buffer(buffer)
         , m_capacity(capacity)
         , m_size(size)
+        , m_mask(0)
     {
+        updateMask();
     }
 
     ~VectorBufferBase()
@@ -345,9 +354,15 @@
         // FIXME: It would be nice to find a way to ASSERT that m_buffer hasn't leaked here.
     }
 
+    void updateMask()
+    {
+        m_mask = maskForSize(m_capacity);
+    }
+
     T* m_buffer;
     unsigned m_capacity;
     unsigned m_size; // Only used by the Vector subclass, but placed here to avoid padding the struct.
+    unsigned m_mask;
 };
 
 template<typename T, size_t inlineCapacity, typename Malloc>
@@ -380,6 +395,7 @@
     {
         std::swap(m_buffer, other.m_buffer);
         std::swap(m_capacity, other.m_capacity);
+        std::swap(m_mask, other.m_mask);
     }
     
     void restoreInlineBufferIfNeeded() { }
@@ -404,6 +420,7 @@
     using Base::releaseBuffer;
 
 protected:
+    using Base::m_mask;
     using Base::m_size;
 
 private:
@@ -442,6 +459,7 @@
         else {
             m_buffer = inlineBuffer();
             m_capacity = inlineCapacity;
+            updateMask();
         }
     }
 
@@ -451,6 +469,7 @@
             return Base::tryAllocateBuffer(newCapacity);
         m_buffer = inlineBuffer();
         m_capacity = inlineCapacity;
+        updateMask();
         return true;
     }
 
@@ -478,19 +497,23 @@
         if (buffer() == inlineBuffer() && other.buffer() == other.inlineBuffer()) {
             swapInlineBuffer(other, mySize, otherSize);
             std::swap(m_capacity, other.m_capacity);
+            std::swap(m_mask, other.m_mask);
         } else if (buffer() == inlineBuffer()) {
             m_buffer = other.m_buffer;
             other.m_buffer = other.inlineBuffer();
             swapInlineBuffer(other, mySize, 0);
             std::swap(m_capacity, other.m_capacity);
+            std::swap(m_mask, other.m_mask);
         } else if (other.buffer() == other.inlineBuffer()) {
             other.m_buffer = m_buffer;
             m_buffer = inlineBuffer();
             swapInlineBuffer(other, 0, otherSize);
             std::swap(m_capacity, other.m_capacity);
+            std::swap(m_mask, other.m_mask);
         } else {
             std::swap(m_buffer, other.m_buffer);
             std::swap(m_capacity, other.m_capacity);
+            std::swap(m_mask, other.m_mask);
         }
     }
 
@@ -500,6 +523,7 @@
             return;
         m_buffer = inlineBuffer();
         m_capacity = inlineCapacity;
+        updateMask();
     }
 
 #if ASAN_ENABLED
@@ -527,11 +551,13 @@
     }
 
 protected:
+    using Base::m_mask;
     using Base::m_size;
 
 private:
     using Base::m_buffer;
     using Base::m_capacity;
+    using Base::updateMask;
     
     void swapInlineBuffer(VectorBuffer& other, size_t mySize, size_t otherSize)
     {
@@ -667,23 +693,23 @@
     {
         if (UNLIKELY(i >= size()))
             OverflowHandler::overflowed();
-        return Base::buffer()[i];
+        return Base::buffer()[i & m_mask];
     }
     const T& at(size_t i) const 
     {
         if (UNLIKELY(i >= size()))
             OverflowHandler::overflowed();
-        return Base::buffer()[i];
+        return Base::buffer()[i & m_mask];
     }
     T& at(Checked<size_t> i)
     {
         RELEASE_ASSERT(i < size());
-        return Base::buffer()[i];
+        return Base::buffer()[i & m_mask];
     }
     const T& at(Checked<size_t> i) const
     {
         RELEASE_ASSERT(i < size());
-        return Base::buffer()[i];
+        return Base::buffer()[i & m_mask];
     }
 
     T& operator[](size_t i) { return at(i); }
@@ -827,6 +853,7 @@
 
     void asanBufferSizeWillChangeTo(size_t);
 
+    using Base::m_mask;
     using Base::m_size;
     using Base::buffer;
     using Base::capacity;

Modified: trunk/Source/WTF/wtf/text/StringImpl.cpp (226067 => 226068)


--- trunk/Source/WTF/wtf/text/StringImpl.cpp	2017-12-18 20:13:56 UTC (rev 226067)
+++ trunk/Source/WTF/wtf/text/StringImpl.cpp	2017-12-18 20:15:41 UTC (rev 226068)
@@ -48,7 +48,7 @@
 
 using namespace Unicode;
 
-static_assert(sizeof(StringImpl) == 2 * sizeof(int) + 2 * sizeof(void*), "StringImpl should stay small");
+static_assert(sizeof(StringImpl) == 2 * sizeof(int) + sizeof(void*) + 2 * sizeof(int), "StringImpl should stay small");
 
 #if STRING_STATS
 StringStats StringImpl::m_stringStats;

Modified: trunk/Source/WTF/wtf/text/StringImpl.h (226067 => 226068)


--- trunk/Source/WTF/wtf/text/StringImpl.h	2017-12-18 20:13:56 UTC (rev 226067)
+++ trunk/Source/WTF/wtf/text/StringImpl.h	2017-12-18 20:15:41 UTC (rev 226068)
@@ -148,6 +148,7 @@
         const char16_t* m_data16Char;
     };
     mutable unsigned m_hashAndFlags;
+    unsigned m_mask;
 };
 
 // FIXME: Use of StringImpl and const is rather confused.
@@ -253,6 +254,7 @@
     static unsigned flagIs8Bit() { return s_hashFlag8BitBuffer; }
     static unsigned flagIsAtomic() { return s_hashFlagStringKindIsAtomic; }
     static unsigned flagIsSymbol() { return s_hashFlagStringKindIsSymbol; }
+    static unsigned maskOffset() { return OBJECT_OFFSETOF(StringImpl, m_mask); }
     static unsigned maskStringKind() { return s_hashMaskStringKind; }
     static unsigned dataOffset() { return OBJECT_OFFSETOF(StringImpl, m_data8); }
 
@@ -263,6 +265,7 @@
     WTF_EXPORT_STRING_API static Ref<StringImpl> adopt(StringBuffer<LChar>&&);
 
     unsigned length() const { return m_length; }
+    unsigned mask() const { return m_mask; }
     static ptrdiff_t lengthMemoryOffset() { return OBJECT_OFFSETOF(StringImpl, m_length); }
     bool isEmpty() const { return !m_length; }
 
@@ -755,6 +758,7 @@
     , m_length(length)
     , m_data8(data8)
     , m_hashAndFlags(hashAndFlags)
+    , m_mask(maskForSize(length))
 {
 }
 
@@ -763,6 +767,7 @@
     , m_length(length)
     , m_data16(data16)
     , m_hashAndFlags(hashAndFlags)
+    , m_mask(maskForSize(length))
 {
 }
 
@@ -771,6 +776,7 @@
     , m_length(length)
     , m_data8Char(characters)
     , m_hashAndFlags(hashAndFlags)
+    , m_mask(maskForSize(length))
 {
 }
 
@@ -779,6 +785,7 @@
     , m_length(length)
     , m_data16Char(characters)
     , m_hashAndFlags(hashAndFlags)
+    , m_mask(maskForSize(length))
 {
 }
 
@@ -1071,7 +1078,7 @@
 inline UChar StringImpl::at(unsigned i) const
 {
     ASSERT_WITH_SECURITY_IMPLICATION(i < m_length);
-    return is8Bit() ? m_data8[i] : m_data16[i];
+    return is8Bit() ? m_data8[i & m_mask] : m_data16[i & m_mask];
 }
 
 inline void StringImpl::assertCaged() const
@@ -1115,7 +1122,7 @@
     // MSVC doesn't support alignof yet.
     return roundUpToMultipleOf<sizeof(T)>(sizeof(StringImpl));
 #else
-    return roundUpToMultipleOf<alignof(T)>(offsetof(StringImpl, m_hashAndFlags) + sizeof(StringImpl::m_hashAndFlags));
+    return roundUpToMultipleOf<alignof(T)>(offsetof(StringImpl, m_mask) + sizeof(StringImpl::m_mask));
 #endif
 }
 

Modified: trunk/Source/WTF/wtf/text/StringView.h (226067 => 226068)


--- trunk/Source/WTF/wtf/text/StringView.h	2017-12-18 20:13:56 UTC (rev 226067)
+++ trunk/Source/WTF/wtf/text/StringView.h	2017-12-18 20:15:41 UTC (rev 226068)
@@ -159,6 +159,8 @@
 
     void initialize(const LChar*, unsigned length);
     void initialize(const UChar*, unsigned length);
+    void initialize(const LChar*, unsigned length, unsigned mask);
+    void initialize(const UChar*, unsigned length, unsigned mask);
 
     template<typename CharacterType, typename MatchedCharacterPredicate>
     StringView stripLeadingAndTrailingMatchedCharacters(const CharacterType*, const MatchedCharacterPredicate&);
@@ -176,6 +178,7 @@
 
     const void* m_characters { nullptr };
     unsigned m_length { 0 };
+    unsigned m_mask { 0 };
     bool m_is8Bit { true };
 
 #if CHECK_STRINGVIEW_LIFETIME
@@ -228,6 +231,7 @@
 inline StringView::StringView(StringView&& other)
     : m_characters(other.m_characters)
     , m_length(other.m_length)
+    , m_mask(other.m_mask)
     , m_is8Bit(other.m_is8Bit)
 {
     ASSERT(other.underlyingStringIsValid());
@@ -241,6 +245,7 @@
 inline StringView::StringView(const StringView& other)
     : m_characters(other.m_characters)
     , m_length(other.m_length)
+    , m_mask(other.m_mask)
     , m_is8Bit(other.m_is8Bit)
 {
     ASSERT(other.underlyingStringIsValid());
@@ -254,6 +259,7 @@
 
     m_characters = other.m_characters;
     m_length = other.m_length;
+    m_mask = other.m_mask;
     m_is8Bit = other.m_is8Bit;
 
     other.clear();
@@ -270,6 +276,7 @@
 
     m_characters = other.m_characters;
     m_length = other.m_length;
+    m_mask = other.m_mask;
     m_is8Bit = other.m_is8Bit;
 
     setUnderlyingString(other);
@@ -280,15 +287,27 @@
 
 inline void StringView::initialize(const LChar* characters, unsigned length)
 {
+    initialize(characters, length, maskForSize(length));
+}
+
+inline void StringView::initialize(const UChar* characters, unsigned length)
+{
+    initialize(characters, length, maskForSize(length));
+}
+
+inline void StringView::initialize(const LChar* characters, unsigned length, unsigned mask)
+{
     m_characters = characters;
     m_length = length;
+    m_mask = mask;
     m_is8Bit = true;
 }
 
-inline void StringView::initialize(const UChar* characters, unsigned length)
+inline void StringView::initialize(const UChar* characters, unsigned length, unsigned mask)
 {
     m_characters = characters;
     m_length = length;
+    m_mask = mask;
     m_is8Bit = false;
 }
 
@@ -311,9 +330,9 @@
 {
     setUnderlyingString(&string);
     if (string.is8Bit())
-        initialize(string.characters8(), string.length());
+        initialize(string.characters8(), string.length(), string.mask());
     else
-        initialize(string.characters16(), string.length());
+        initialize(string.characters16(), string.length(), string.mask());
 }
 
 inline StringView::StringView(const StringImpl* string)
@@ -323,9 +342,9 @@
 
     setUnderlyingString(string);
     if (string->is8Bit())
-        initialize(string->characters8(), string->length());
+        initialize(string->characters8(), string->length(), string->mask());
     else
-        initialize(string->characters16(), string->length());
+        initialize(string->characters16(), string->length(), string->mask());
 }
 
 inline StringView::StringView(const String& string)
@@ -336,10 +355,10 @@
         return;
     }
     if (string.is8Bit()) {
-        initialize(string.characters8(), string.length());
+        initialize(string.characters8(), string.length(), string.mask());
         return;
     }
-    initialize(string.characters16(), string.length());
+    initialize(string.characters16(), string.length(), string.mask());
 }
 
 inline StringView::StringView(const AtomicString& atomicString)
@@ -351,6 +370,7 @@
 {
     m_characters = nullptr;
     m_length = 0;
+    m_mask = 0;
     m_is8Bit = true;
 }
 
@@ -439,8 +459,8 @@
 {
     ASSERT(index < length());
     if (is8Bit())
-        return characters8()[index];
-    return characters16()[index];
+        return characters8()[index & m_mask];
+    return characters16()[index & m_mask];
 }
 
 inline bool StringView::contains(UChar character) const

Modified: trunk/Source/WTF/wtf/text/WTFString.h (226067 => 226068)


--- trunk/Source/WTF/wtf/text/WTFString.h	2017-12-18 20:13:56 UTC (rev 226067)
+++ trunk/Source/WTF/wtf/text/WTFString.h	2017-12-18 20:15:41 UTC (rev 226068)
@@ -149,6 +149,7 @@
     RefPtr<StringImpl> releaseImpl() { return WTFMove(m_impl); }
 
     unsigned length() const { return m_impl ? m_impl->length() : 0; }
+    unsigned mask() const { return m_impl ? m_impl->mask() : 0; }
     const LChar* characters8() const { return m_impl ? m_impl->characters8() : nullptr; }
     const UChar* characters16() const { return m_impl ? m_impl->characters16() : nullptr; }
 
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to