Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2009 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 32 namespace blink { | 32 namespace blink { |
| 33 | 33 |
| 34 template<class StringClass> struct StringTraits { | 34 template<class StringClass> struct StringTraits { |
| 35 static const StringClass& fromStringResource(WebCoreStringResourceBase*); | 35 static const StringClass& fromStringResource(WebCoreStringResourceBase*); |
| 36 template <typename V8StringTrait> | 36 template <typename V8StringTrait> |
| 37 static StringClass fromV8String(v8::Local<v8::String>, int); | 37 static StringClass fromV8String(v8::Local<v8::String>, int); |
| 38 }; | 38 }; |
| 39 | 39 |
| 40 template<> | 40 template<> |
| 41 struct StringTraits<String> { | 41 struct StringTraits<String> { |
| 42 static const String& fromStringResource(WebCoreStringResourceBase* resource) | 42 static String fromStringResource(WebCoreStringResourceBase* resource) |
| 43 { | 43 { |
| 44 return resource->webcoreString(); | 44 return resource->webcoreString(); |
| 45 } | 45 } |
| 46 template <typename V8StringTrait> | 46 template <typename V8StringTrait> |
| 47 static String fromV8String(v8::Local<v8::String>, int); | 47 static String fromV8String(v8::Local<v8::String>, int); |
| 48 }; | 48 }; |
| 49 | 49 |
| 50 template<> | 50 template<> |
| 51 struct StringTraits<AtomicString> { | 51 struct StringTraits<AtomicString> { |
| 52 static const AtomicString& fromStringResource(WebCoreStringResourceBase* res ource) | 52 static AtomicString fromStringResource(WebCoreStringResourceBase* resource) |
| 53 { | 53 { |
| 54 return resource->atomicString(); | 54 return resource->atomicString(); |
| 55 } | 55 } |
| 56 template <typename V8StringTrait> | 56 template <typename V8StringTrait> |
| 57 static AtomicString fromV8String(v8::Local<v8::String>, int); | 57 static AtomicString fromV8String(v8::Local<v8::String>, int); |
| 58 }; | 58 }; |
| 59 | 59 |
| 60 struct V8StringTwoBytesTrait { | 60 struct V8StringTwoBytesTrait { |
| 61 typedef UChar CharType; | 61 typedef UChar CharType; |
| 62 ALWAYS_INLINE static void write(v8::Local<v8::String> v8String, CharType* bu ffer, int length) | 62 ALWAYS_INLINE static void write(v8::Local<v8::String> v8String, CharType* bu ffer, int length) |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 101 | 101 |
| 102 template<typename StringType> | 102 template<typename StringType> |
| 103 StringType v8StringToWebCoreString(v8::Local<v8::String> v8String, ExternalMode external) | 103 StringType v8StringToWebCoreString(v8::Local<v8::String> v8String, ExternalMode external) |
| 104 { | 104 { |
| 105 { | 105 { |
| 106 // This portion of this function is very hot in certain Dromeao benchmar ks. | 106 // This portion of this function is very hot in certain Dromeao benchmar ks. |
| 107 v8::String::Encoding encoding; | 107 v8::String::Encoding encoding; |
| 108 v8::String::ExternalStringResourceBase* resource = v8String->GetExternal StringResourceBase(&encoding); | 108 v8::String::ExternalStringResourceBase* resource = v8String->GetExternal StringResourceBase(&encoding); |
| 109 if (LIKELY(!!resource)) { | 109 if (LIKELY(!!resource)) { |
| 110 WebCoreStringResourceBase* base; | 110 WebCoreStringResourceBase* base; |
| 111 if (encoding == v8::String::ONE_BYTE_ENCODING) | 111 if (UNLIKELY(resource->isCompressable())) { |
| 112 base = static_cast<WebCoreStringResource8*>(resource); | 112 if (encoding == v8::String::ONE_BYTE_ENCODING) |
| 113 else | 113 base = static_cast<WebCoreCompressableStringResource8*>(reso urce); |
| 114 base = static_cast<WebCoreStringResource16*>(resource); | 114 else |
| 115 base = static_cast<WebCoreCompressableStringResource16*>(res ource); | |
| 116 } else { | |
| 117 if (encoding == v8::String::ONE_BYTE_ENCODING) | |
| 118 base = static_cast<WebCoreStringResource8*>(resource); | |
| 119 else | |
| 120 base = static_cast<WebCoreStringResource16*>(resource); | |
| 121 } | |
| 115 return StringTraits<StringType>::fromStringResource(base); | 122 return StringTraits<StringType>::fromStringResource(base); |
| 116 } | 123 } |
| 117 } | 124 } |
| 118 | 125 |
| 119 int length = v8String->Length(); | 126 int length = v8String->Length(); |
| 120 if (UNLIKELY(!length)) | 127 if (UNLIKELY(!length)) |
| 121 return StringType(""); | 128 return StringType(""); |
| 122 | 129 |
| 123 bool oneByte = v8String->ContainsOnlyOneByte(); | 130 bool oneByte = v8String->ContainsOnlyOneByte(); |
| 124 StringType result(oneByte ? StringTraits<StringType>::template fromV8String< V8StringOneByteTrait>(v8String, length) : StringTraits<StringType>::template fro mV8String<V8StringTwoBytesTrait>(v8String, length)); | 131 StringType result(oneByte ? StringTraits<StringType>::template fromV8String< V8StringOneByteTrait>(v8String, length) : StringTraits<StringType>::template fro mV8String<V8StringTwoBytesTrait>(v8String, length)); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 136 delete stringResource; | 143 delete stringResource; |
| 137 } | 144 } |
| 138 return result; | 145 return result; |
| 139 } | 146 } |
| 140 | 147 |
| 141 // Explicitly instantiate the above template with the expected parameterizations , | 148 // Explicitly instantiate the above template with the expected parameterizations , |
| 142 // to ensure the compiler generates the code; otherwise link errors can result i n GCC 4.4. | 149 // to ensure the compiler generates the code; otherwise link errors can result i n GCC 4.4. |
| 143 template String v8StringToWebCoreString<String>(v8::Local<v8::String>, ExternalM ode); | 150 template String v8StringToWebCoreString<String>(v8::Local<v8::String>, ExternalM ode); |
| 144 template AtomicString v8StringToWebCoreString<AtomicString>(v8::Local<v8::String >, ExternalMode); | 151 template AtomicString v8StringToWebCoreString<AtomicString>(v8::Local<v8::String >, ExternalMode); |
| 145 | 152 |
| 153 template String v8StringToWebCoreString2<String>(v8::Local<v8::String>, External Mode); | |
|
haraken
2015/11/24 11:15:41
Do we need this template?
hajimehoshi
2015/11/26 10:49:13
Sorry but this was a test code. Removed.
| |
| 154 | |
| 155 template<typename StringType> | |
| 156 StringType v8StringToWebCoreString2(v8::Local<v8::String> v8String, ExternalMode external) | |
|
haraken
2015/11/24 11:15:41
Ditto.
hajimehoshi
2015/11/26 10:49:13
Done.
| |
| 157 { | |
| 158 { | |
| 159 // This portion of this function is very hot in certain Dromeao benchmar ks. | |
| 160 v8::String::Encoding encoding; | |
| 161 v8::String::ExternalStringResourceBase* resource = v8String->GetExternal StringResourceBase(&encoding); | |
| 162 if (LIKELY(!!resource)) { | |
| 163 // TODO(hajimehoshi): What if resource is a compressablestring??? | |
| 164 WebCoreStringResourceBase* base; | |
| 165 if (encoding == v8::String::ONE_BYTE_ENCODING) | |
| 166 base = static_cast<WebCoreStringResource8*>(resource); | |
| 167 else | |
| 168 base = static_cast<WebCoreStringResource16*>(resource); | |
| 169 return StringTraits<StringType>::fromStringResource(base); | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 int length = v8String->Length(); | |
| 174 if (UNLIKELY(!length)) | |
| 175 return StringType(""); | |
| 176 | |
| 177 bool oneByte = v8String->ContainsOnlyOneByte(); | |
| 178 StringType result(oneByte ? StringTraits<StringType>::template fromV8String< V8StringOneByteTrait>(v8String, length) : StringTraits<StringType>::template fro mV8String<V8StringTwoBytesTrait>(v8String, length)); | |
| 179 | |
| 180 if (external != Externalize || !v8String->CanMakeExternal()) | |
| 181 return result; | |
| 182 | |
| 183 if (result.is8Bit()) { | |
| 184 WebCoreStringResource8* stringResource = new WebCoreStringResource8(resu lt); | |
| 185 if (UNLIKELY(!v8String->MakeExternal(stringResource))) | |
| 186 delete stringResource; | |
| 187 } else { | |
| 188 WebCoreStringResource16* stringResource = new WebCoreStringResource16(re sult); | |
| 189 if (UNLIKELY(!v8String->MakeExternal(stringResource))) | |
| 190 delete stringResource; | |
| 191 } | |
| 192 return result; | |
| 193 } | |
| 194 | |
| 146 // Fast but non thread-safe version. | 195 // Fast but non thread-safe version. |
| 147 String int32ToWebCoreStringFast(int value) | 196 String int32ToWebCoreStringFast(int value) |
| 148 { | 197 { |
| 149 // Caching of small strings below is not thread safe: newly constructed Atom icString | 198 // Caching of small strings below is not thread safe: newly constructed Atom icString |
| 150 // are not safely published. | 199 // are not safely published. |
| 151 ASSERT(isMainThread()); | 200 ASSERT(isMainThread()); |
| 152 | 201 |
| 153 // Most numbers used are <= 100. Even if they aren't used there's very littl e cost in using the space. | 202 // Most numbers used are <= 100. Even if they aren't used there's very littl e cost in using the space. |
| 154 const int kLowNumbers = 100; | 203 const int kLowNumbers = 100; |
| 155 DEFINE_STATIC_LOCAL(Vector<AtomicString>, lowNumbers, (kLowNumbers + 1)); | 204 DEFINE_STATIC_LOCAL(Vector<AtomicString>, lowNumbers, (kLowNumbers + 1)); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 169 | 218 |
| 170 String int32ToWebCoreString(int value) | 219 String int32ToWebCoreString(int value) |
| 171 { | 220 { |
| 172 // If we are on the main thread (this should always true for non-workers), c all the faster one. | 221 // If we are on the main thread (this should always true for non-workers), c all the faster one. |
| 173 if (isMainThread()) | 222 if (isMainThread()) |
| 174 return int32ToWebCoreStringFast(value); | 223 return int32ToWebCoreStringFast(value); |
| 175 return String::number(value); | 224 return String::number(value); |
| 176 } | 225 } |
| 177 | 226 |
| 178 } // namespace blink | 227 } // namespace blink |
| OLD | NEW |