SmallStrings.cpp revision 2bde8e466a4451c7319e3a072d118917957d6554
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
34namespace JSC {
35
36static inline bool isMarked(JSCell* string)
37{
38    return string && Heap::isMarked(string);
39}
40
41class SmallStringsStorage {
42    WTF_MAKE_NONCOPYABLE(SmallStringsStorage); WTF_MAKE_FAST_ALLOCATED;
43public:
44    SmallStringsStorage();
45
46    StringImpl* rep(unsigned char character)
47    {
48        return m_reps[character].get();
49    }
50
51private:
52    static const unsigned singleCharacterStringCount = maxSingleCharacterString + 1;
53
54    RefPtr<StringImpl> m_reps[singleCharacterStringCount];
55};
56
57SmallStringsStorage::SmallStringsStorage()
58{
59    UChar* characterBuffer = 0;
60    RefPtr<StringImpl> baseString = StringImpl::createUninitialized(singleCharacterStringCount, characterBuffer);
61    for (unsigned i = 0; i < singleCharacterStringCount; ++i) {
62        characterBuffer[i] = i;
63        m_reps[i] = StringImpl::create(baseString, i, 1);
64    }
65}
66
67SmallStrings::SmallStrings()
68{
69    COMPILE_ASSERT(singleCharacterStringCount == sizeof(m_singleCharacterStrings) / sizeof(m_singleCharacterStrings[0]), IsNumCharactersConstInSyncWithClassUsage);
70    clear();
71}
72
73SmallStrings::~SmallStrings()
74{
75}
76
77void SmallStrings::markChildren(HeapRootMarker& heapRootMarker)
78{
79    /*
80       Our hypothesis is that small strings are very common. So, we cache them
81       to avoid GC churn. However, in cases where this hypothesis turns out to
82       be false -- including the degenerate case where all JavaScript execution
83       has terminated -- we don't want to waste memory.
84
85       To test our hypothesis, we check if any small string has been marked. If
86       so, it's probably reasonable to mark the rest. If not, we clear the cache.
87     */
88
89    bool isAnyStringMarked = isMarked(m_emptyString);
90    for (unsigned i = 0; i < singleCharacterStringCount && !isAnyStringMarked; ++i)
91        isAnyStringMarked = isMarked(m_singleCharacterStrings[i]);
92
93    if (!isAnyStringMarked) {
94        clear();
95        return;
96    }
97
98    if (m_emptyString)
99        heapRootMarker.mark(&m_emptyString);
100    for (unsigned i = 0; i < singleCharacterStringCount; ++i) {
101        if (m_singleCharacterStrings[i])
102            heapRootMarker.mark(&m_singleCharacterStrings[i]);
103    }
104}
105
106void SmallStrings::clear()
107{
108    m_emptyString = 0;
109    for (unsigned i = 0; i < singleCharacterStringCount; ++i)
110        m_singleCharacterStrings[i] = 0;
111}
112
113unsigned SmallStrings::count() const
114{
115    unsigned count = 0;
116    if (m_emptyString)
117        ++count;
118    for (unsigned i = 0; i < singleCharacterStringCount; ++i) {
119        if (m_singleCharacterStrings[i])
120            ++count;
121    }
122    return count;
123}
124
125void SmallStrings::createEmptyString(JSGlobalData* globalData)
126{
127    ASSERT(!m_emptyString);
128    m_emptyString = new (globalData) JSString(globalData, "", JSString::HasOtherOwner);
129}
130
131void SmallStrings::createSingleCharacterString(JSGlobalData* globalData, unsigned char character)
132{
133    if (!m_storage)
134        m_storage = adoptPtr(new SmallStringsStorage);
135    ASSERT(!m_singleCharacterStrings[character]);
136    m_singleCharacterStrings[character] = new (globalData) JSString(globalData, PassRefPtr<StringImpl>(m_storage->rep(character)), JSString::HasOtherOwner);
137}
138
139StringImpl* SmallStrings::singleCharacterStringRep(unsigned char character)
140{
141    if (!m_storage)
142        m_storage = adoptPtr(new SmallStringsStorage);
143    return m_storage->rep(character);
144}
145
146} // namespace JSC
147