source: webkit/trunk/JavaScriptCore/runtime/UStringImpl.cpp@ 52463

Last change on this file since 52463 was 52463, checked in by [email protected], 15 years ago

https://bugs.webkit.org/show_bug.cgi?id=32831
Replace UString::Rep implementation, following introduction of ropes to JSC.

Reviewed by Darin Adler.

JavaScriptCore:

  • Remove redundant overcapacity mechanisms.
  • Reduce memory cost of Rep's.
  • Add an inline storage mechanism akin to that in WebCore's StringImpl.

~1% Sunspider progression.

(JSC::JSString::resolveRope):

  • runtime/SmallStrings.cpp:

(JSC::SmallStringsStorage::SmallStringsStorage):

  • runtime/UString.cpp:

(JSC::initializeUString):
(JSC::createRep):
(JSC::UString::createFromUTF8):
(JSC::UString::createUninitialized):
(JSC::UString::spliceSubstringsWithSeparators):
(JSC::UString::replaceRange):
(JSC::UString::ascii):
(JSC::UString::operator=):
(JSC::UString::toStrictUInt32):
(JSC::equal):

  • runtime/UString.h:

(JSC::UString::isEmpty):
(JSC::UString::cost):
(JSC::makeString):

  • runtime/UStringImpl.cpp: Added.

(JSC::UStringImpl::baseSharedBuffer):
(JSC::UStringImpl::sharedBuffer):
(JSC::UStringImpl::destroy):
(JSC::UStringImpl::computeHash):

  • runtime/UStringImpl.h: Added.

(JSC::UntypedPtrAndBitfield::UntypedPtrAndBitfield):
(JSC::UntypedPtrAndBitfield::asPtr):
(JSC::UntypedPtrAndBitfield::operator&=):
(JSC::UntypedPtrAndBitfield::operator|=):
(JSC::UntypedPtrAndBitfield::operator&):
(JSC::UStringImpl::create):
(JSC::UStringImpl::createCopying):
(JSC::UStringImpl::createUninitialized):
(JSC::UStringImpl::data):
(JSC::UStringImpl::size):
(JSC::UStringImpl::cost):
(JSC::UStringImpl::hash):
(JSC::UStringImpl::computedHash):
(JSC::UStringImpl::setHash):
(JSC::UStringImpl::identifierTable):
(JSC::UStringImpl::setIdentifierTable):
(JSC::UStringImpl::ref):
(JSC::UStringImpl::deref):
(JSC::UStringImpl::allocChars):
(JSC::UStringImpl::copyChars):
(JSC::UStringImpl::computeHash):
(JSC::UStringImpl::null):
(JSC::UStringImpl::empty):
(JSC::UStringImpl::checkConsistency):
(JSC::UStringImpl::):
(JSC::UStringImpl::UStringImpl):
(JSC::UStringImpl::operator new):
(JSC::UStringImpl::bufferOwnerString):
(JSC::UStringImpl::bufferOwnership):
(JSC::UStringImpl::isStatic):

JavaScriptGlue:

  • ForwardingHeaders/wtf/PossiblyNull.h: Added.
    • add forwarding header.

WebCore:

  • ForwardingHeaders/runtime/UStringImpl.h: Added.
    • add forwarding header.
  • platform/text/StringImpl.cpp:

(WebCore::StringImpl::ustring):

  • order of arguments to UString::Rep constructor for shared strings changed.
File size: 5.0 KB
Line 
1/*
2 * Copyright (C) 2009 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 "UStringImpl.h"
28
29#include "Identifier.h"
30#include "UString.h"
31#include "UTF8.h"
32
33using namespace WTF::Unicode;
34using namespace std;
35
36namespace JSC {
37
38SharedUChar* UStringImpl::baseSharedBuffer()
39{
40 ASSERT((bufferOwnership() == BufferShared)
41 || ((bufferOwnership() == BufferOwned) && !m_dataBuffer.asPtr<void*>()));
42
43 if (bufferOwnership() != BufferShared)
44 m_dataBuffer = UntypedPtrAndBitfield(SharedUChar::create(new OwnFastMallocPtr<UChar>(m_data)).releaseRef(), BufferShared);
45
46 return m_dataBuffer.asPtr<SharedUChar*>();
47}
48
49SharedUChar* UStringImpl::sharedBuffer()
50{
51 if (m_length < s_minLengthToShare)
52 return 0;
53 ASSERT(!isStatic());
54
55 UStringImpl* owner = bufferOwnerString();
56 if (owner->bufferOwnership() == BufferInternal)
57 return 0;
58
59 return owner->baseSharedBuffer();
60}
61
62void UStringImpl::destroy()
63{
64 ASSERT(!isStatic());
65 checkConsistency();
66
67 if (identifierTable())
68 Identifier::remove(this);
69
70 if (bufferOwnership() != BufferInternal) {
71 if (bufferOwnership() == BufferOwned)
72 fastFree(m_data);
73 else if (bufferOwnership() == BufferSubstring)
74 m_dataBuffer.asPtr<UStringImpl*>()->deref();
75 else {
76 ASSERT(bufferOwnership() == BufferShared);
77 m_dataBuffer.asPtr<SharedUChar*>()->deref();
78 }
79 }
80
81 delete this;
82}
83
84// Golden ratio - arbitrary start value to avoid mapping all 0's to all 0's
85// or anything like that.
86const unsigned PHI = 0x9e3779b9U;
87
88// Paul Hsieh's SuperFastHash
89// http://www.azillionmonkeys.com/qed/hash.html
90unsigned UStringImpl::computeHash(const UChar* s, int len)
91{
92 unsigned l = len;
93 uint32_t hash = PHI;
94 uint32_t tmp;
95
96 int rem = l & 1;
97 l >>= 1;
98
99 // Main loop
100 for (; l > 0; l--) {
101 hash += s[0];
102 tmp = (s[1] << 11) ^ hash;
103 hash = (hash << 16) ^ tmp;
104 s += 2;
105 hash += hash >> 11;
106 }
107
108 // Handle end case
109 if (rem) {
110 hash += s[0];
111 hash ^= hash << 11;
112 hash += hash >> 17;
113 }
114
115 // Force "avalanching" of final 127 bits
116 hash ^= hash << 3;
117 hash += hash >> 5;
118 hash ^= hash << 2;
119 hash += hash >> 15;
120 hash ^= hash << 10;
121
122 // this avoids ever returning a hash code of 0, since that is used to
123 // signal "hash not computed yet", using a value that is likely to be
124 // effectively the same as 0 when the low bits are masked
125 if (hash == 0)
126 hash = 0x80000000;
127
128 return hash;
129}
130
131// Paul Hsieh's SuperFastHash
132// http://www.azillionmonkeys.com/qed/hash.html
133unsigned UStringImpl::computeHash(const char* s, int l)
134{
135 // This hash is designed to work on 16-bit chunks at a time. But since the normal case
136 // (above) is to hash UTF-16 characters, we just treat the 8-bit chars as if they
137 // were 16-bit chunks, which should give matching results
138
139 uint32_t hash = PHI;
140 uint32_t tmp;
141
142 size_t rem = l & 1;
143 l >>= 1;
144
145 // Main loop
146 for (; l > 0; l--) {
147 hash += static_cast<unsigned char>(s[0]);
148 tmp = (static_cast<unsigned char>(s[1]) << 11) ^ hash;
149 hash = (hash << 16) ^ tmp;
150 s += 2;
151 hash += hash >> 11;
152 }
153
154 // Handle end case
155 if (rem) {
156 hash += static_cast<unsigned char>(s[0]);
157 hash ^= hash << 11;
158 hash += hash >> 17;
159 }
160
161 // Force "avalanching" of final 127 bits
162 hash ^= hash << 3;
163 hash += hash >> 5;
164 hash ^= hash << 2;
165 hash += hash >> 15;
166 hash ^= hash << 10;
167
168 // this avoids ever returning a hash code of 0, since that is used to
169 // signal "hash not computed yet", using a value that is likely to be
170 // effectively the same as 0 when the low bits are masked
171 if (hash == 0)
172 hash = 0x80000000;
173
174 return hash;
175}
176
177}
Note: See TracBrowser for help on using the repository browser.