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