2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "SmallStrings.h"
29 #include "JSGlobalObject.h"
32 #include <wtf/Noncopyable.h>
35 static const unsigned numCharactersToStore
= 0x100;
37 static inline bool isMarked(JSString
* string
)
39 return string
&& Heap::isCellMarked(string
);
42 class SmallStringsStorage
: public Noncopyable
{
44 SmallStringsStorage();
46 UString::Rep
* rep(unsigned char character
) { return m_reps
[character
].get(); }
49 RefPtr
<UString::Rep
> m_reps
[numCharactersToStore
];
52 SmallStringsStorage::SmallStringsStorage()
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 m_reps
[i
] = UStringImpl::create(baseString
, i
, 1);
62 SmallStrings::SmallStrings()
64 COMPILE_ASSERT(numCharactersToStore
== sizeof(m_singleCharacterStrings
) / sizeof(m_singleCharacterStrings
[0]), IsNumCharactersConstInSyncWithClassUsage
);
68 SmallStrings::~SmallStrings()
72 void SmallStrings::markChildren(MarkStack
& markStack
)
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.
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.
84 bool isAnyStringMarked
= isMarked(m_emptyString
);
85 for (unsigned i
= 0; i
< numCharactersToStore
&& !isAnyStringMarked
; ++i
)
86 isAnyStringMarked
= isMarked(m_singleCharacterStrings
[i
]);
88 if (!isAnyStringMarked
) {
94 markStack
.append(m_emptyString
);
95 for (unsigned i
= 0; i
< numCharactersToStore
; ++i
) {
96 if (m_singleCharacterStrings
[i
])
97 markStack
.append(m_singleCharacterStrings
[i
]);
101 void SmallStrings::clear()
104 for (unsigned i
= 0; i
< numCharactersToStore
; ++i
)
105 m_singleCharacterStrings
[i
] = 0;
108 unsigned SmallStrings::count() const
113 for (unsigned i
= 0; i
< numCharactersToStore
; ++i
) {
114 if (m_singleCharacterStrings
[i
])
120 void SmallStrings::createEmptyString(JSGlobalData
* globalData
)
122 ASSERT(!m_emptyString
);
123 m_emptyString
= new (globalData
) JSString(globalData
, "", JSString::HasOtherOwner
);
126 void SmallStrings::createSingleCharacterString(JSGlobalData
* globalData
, unsigned char character
)
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
);
134 UString::Rep
* SmallStrings::singleCharacterStringRep(unsigned char character
)
137 m_storage
.set(new SmallStringsStorage
);
138 return m_storage
->rep(character
);