JavaScriptCore/runtime/SmallStrings.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2008, 2010 Apple Inc. All Rights Reserved.
       
     3  *
       
     4  * Redistribution and use in source and binary forms, with or without
       
     5  * modification, are permitted provided that the following conditions
       
     6  * are met:
       
     7  * 1. Redistributions of source code must retain the above copyright
       
     8  *    notice, this list of conditions and the following disclaimer.
       
     9  * 2. Redistributions in binary form must reproduce the above copyright
       
    10  *    notice, this list of conditions and the following disclaimer in the
       
    11  *    documentation and/or other materials provided with the distribution.
       
    12  *
       
    13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
       
    14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
       
    17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
       
    18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
       
    19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
       
    20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
       
    21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    24  */
       
    25 
       
    26 #include "config.h"
       
    27 #include "SmallStrings.h"
       
    28 
       
    29 #include "JSGlobalObject.h"
       
    30 #include "JSString.h"
       
    31 #include <wtf/Noncopyable.h>
       
    32 #include <wtf/PassOwnPtr.h>
       
    33 
       
    34 namespace JSC {
       
    35 
       
    36 static const unsigned numCharactersToStore = 0x100;
       
    37 
       
    38 static inline bool isMarked(JSString* string)
       
    39 {
       
    40     return string && Heap::isCellMarked(string);
       
    41 }
       
    42 
       
    43 class SmallStringsStorage : public Noncopyable {
       
    44 public:
       
    45     SmallStringsStorage();
       
    46 
       
    47     UString::Rep* rep(unsigned char character) { return m_reps[character].get(); }
       
    48 
       
    49 private:
       
    50     RefPtr<UString::Rep> m_reps[numCharactersToStore];
       
    51 };
       
    52 
       
    53 SmallStringsStorage::SmallStringsStorage()
       
    54 {
       
    55     UChar* characterBuffer = 0;
       
    56     RefPtr<UStringImpl> baseString = UStringImpl::createUninitialized(numCharactersToStore, characterBuffer);
       
    57     for (unsigned i = 0; i < numCharactersToStore; ++i) {
       
    58         characterBuffer[i] = i;
       
    59         m_reps[i] = UStringImpl::create(baseString, i, 1);
       
    60     }
       
    61 }
       
    62 
       
    63 SmallStrings::SmallStrings()
       
    64 {
       
    65     COMPILE_ASSERT(numCharactersToStore == sizeof(m_singleCharacterStrings) / sizeof(m_singleCharacterStrings[0]), IsNumCharactersConstInSyncWithClassUsage);
       
    66     clear();
       
    67 }
       
    68 
       
    69 SmallStrings::~SmallStrings()
       
    70 {
       
    71 }
       
    72 
       
    73 void SmallStrings::markChildren(MarkStack& markStack)
       
    74 {
       
    75     /*
       
    76        Our hypothesis is that small strings are very common. So, we cache them
       
    77        to avoid GC churn. However, in cases where this hypothesis turns out to
       
    78        be false -- including the degenerate case where all JavaScript execution
       
    79        has terminated -- we don't want to waste memory.
       
    80 
       
    81        To test our hypothesis, we check if any small string has been marked. If
       
    82        so, it's probably reasonable to mark the rest. If not, we clear the cache.
       
    83      */
       
    84 
       
    85     bool isAnyStringMarked = isMarked(m_emptyString);
       
    86     for (unsigned i = 0; i < numCharactersToStore && !isAnyStringMarked; ++i)
       
    87         isAnyStringMarked = isMarked(m_singleCharacterStrings[i]);
       
    88     
       
    89     if (!isAnyStringMarked) {
       
    90         clear();
       
    91         return;
       
    92     }
       
    93     
       
    94     if (m_emptyString)
       
    95         markStack.append(m_emptyString);
       
    96     for (unsigned i = 0; i < numCharactersToStore; ++i) {
       
    97         if (m_singleCharacterStrings[i])
       
    98             markStack.append(m_singleCharacterStrings[i]);
       
    99     }
       
   100 }
       
   101 
       
   102 void SmallStrings::clear()
       
   103 {
       
   104     m_emptyString = 0;
       
   105     for (unsigned i = 0; i < numCharactersToStore; ++i)
       
   106         m_singleCharacterStrings[i] = 0;
       
   107 }
       
   108 
       
   109 unsigned SmallStrings::count() const
       
   110 {
       
   111     unsigned count = 0;
       
   112     if (m_emptyString)
       
   113         ++count;
       
   114     for (unsigned i = 0; i < numCharactersToStore; ++i) {
       
   115         if (m_singleCharacterStrings[i])
       
   116             ++count;
       
   117     }
       
   118     return count;
       
   119 }
       
   120 
       
   121 void SmallStrings::createEmptyString(JSGlobalData* globalData)
       
   122 {
       
   123     ASSERT(!m_emptyString);
       
   124     m_emptyString = new (globalData) JSString(globalData, "", JSString::HasOtherOwner);
       
   125 }
       
   126 
       
   127 void SmallStrings::createSingleCharacterString(JSGlobalData* globalData, unsigned char character)
       
   128 {
       
   129     if (!m_storage)
       
   130         m_storage = adoptPtr(new SmallStringsStorage);
       
   131     ASSERT(!m_singleCharacterStrings[character]);
       
   132     m_singleCharacterStrings[character] = new (globalData) JSString(globalData, m_storage->rep(character), JSString::HasOtherOwner);
       
   133 }
       
   134 
       
   135 UString::Rep* SmallStrings::singleCharacterStringRep(unsigned char character)
       
   136 {
       
   137     if (!m_storage)
       
   138         m_storage = adoptPtr(new SmallStringsStorage);
       
   139     return m_storage->rep(character);
       
   140 }
       
   141 
       
   142 } // namespace JSC