Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: Source/bindings/v8/SerializedScriptValue.cpp

Issue 24043003: SerializedScriptValue's Writer calls realloc a lot (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | Source/wtf/text/StringBuffer.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010 Google Inc. All rights reserved. 2 * Copyright (C) 2010 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 are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 514 matching lines...) Expand 10 before | Expand all | Expand 10 after
525 doWriteUint32(length); 525 doWriteUint32(length);
526 } 526 }
527 527
528 void writeDenseArray(uint32_t numProperties, uint32_t length) 528 void writeDenseArray(uint32_t numProperties, uint32_t length)
529 { 529 {
530 append(DenseArrayTag); 530 append(DenseArrayTag);
531 doWriteUint32(numProperties); 531 doWriteUint32(numProperties);
532 doWriteUint32(length); 532 doWriteUint32(length);
533 } 533 }
534 534
535 StringBuffer<BufferValueType>& data() 535 String takeWireString()
536 { 536 {
537 COMPILE_ASSERT(sizeof(BufferValueType) == 2, BufferValueTypeIsTwoBytes);
537 fillHole(); 538 fillHole();
538 return m_buffer; 539 String data = String::adopt(m_buffer);
540 data.impl()->truncateAssumingIsolated((m_position + 1) / sizeof(BufferVa lueType));
541 return data;
539 } 542 }
540 543
541 void writeReferenceCount(uint32_t numberOfReferences) 544 void writeReferenceCount(uint32_t numberOfReferences)
542 { 545 {
543 append(ReferenceCountTag); 546 append(ReferenceCountTag);
544 doWriteUint32(numberOfReferences); 547 doWriteUint32(numberOfReferences);
545 } 548 }
546 549
547 void writeGenerateFreshObject() 550 void writeGenerateFreshObject()
548 { 551 {
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
636 *byteAt(m_position++) = b; 639 *byteAt(m_position++) = b;
637 } 640 }
638 641
639 void append(const uint8_t* data, int length) 642 void append(const uint8_t* data, int length)
640 { 643 {
641 ensureSpace(length); 644 ensureSpace(length);
642 memcpy(byteAt(m_position), data, length); 645 memcpy(byteAt(m_position), data, length);
643 m_position += length; 646 m_position += length;
644 } 647 }
645 648
646 void ensureSpace(int extra) 649 static unsigned expandedCapacity(unsigned capacity, unsigned newLength)
650 {
651 static const unsigned minimumCapacity = 16;
652 return std::max(capacity, std::max(minimumCapacity, newLength * 2));
653 }
654
655 void ensureSpace(unsigned extra)
647 { 656 {
648 COMPILE_ASSERT(sizeof(BufferValueType) == 2, BufferValueTypeIsTwoBytes); 657 COMPILE_ASSERT(sizeof(BufferValueType) == 2, BufferValueTypeIsTwoBytes);
649 m_buffer.resize((m_position + extra + 1) / 2); // "+ 1" to round up. 658 unsigned oldCapacity = m_buffer.length() * sizeof(BufferValueType);
659 unsigned newCapacity = expandedCapacity(oldCapacity, m_position + extra) ;
660 m_buffer.resize((newCapacity + 1) / sizeof(BufferValueType)); // "+ 1" t o round up.
650 } 661 }
651 662
652 void fillHole() 663 void fillHole()
653 { 664 {
654 COMPILE_ASSERT(sizeof(BufferValueType) == 2, BufferValueTypeIsTwoBytes); 665 COMPILE_ASSERT(sizeof(BufferValueType) == 2, BufferValueTypeIsTwoBytes);
655 // If the writer is at odd position in the buffer, then one of 666 // If the writer is at odd position in the buffer, then one of
656 // the bytes in the last UChar is not initialized. 667 // the bytes in the last UChar is not initialized.
657 if (m_position % 2) 668 if (m_position % 2)
658 *byteAt(m_position) = static_cast<uint8_t>(PaddingTag); 669 *byteAt(m_position) = static_cast<uint8_t>(PaddingTag);
659 } 670 }
(...skipping 1602 matching lines...) Expand 10 before | Expand all | Expand 10 after
2262 2273
2263 PassRefPtr<SerializedScriptValue> SerializedScriptValue::create(const String& da ta) 2274 PassRefPtr<SerializedScriptValue> SerializedScriptValue::create(const String& da ta)
2264 { 2275 {
2265 return create(data, v8::Isolate::GetCurrent()); 2276 return create(data, v8::Isolate::GetCurrent());
2266 } 2277 }
2267 2278
2268 PassRefPtr<SerializedScriptValue> SerializedScriptValue::create(const String& da ta, v8::Isolate* isolate) 2279 PassRefPtr<SerializedScriptValue> SerializedScriptValue::create(const String& da ta, v8::Isolate* isolate)
2269 { 2280 {
2270 Writer writer(isolate); 2281 Writer writer(isolate);
2271 writer.writeWebCoreString(data); 2282 writer.writeWebCoreString(data);
2272 String wireData = String::adopt(writer.data()); 2283 String wireData = writer.takeWireString();
2273 return adoptRef(new SerializedScriptValue(wireData)); 2284 return adoptRef(new SerializedScriptValue(wireData));
2274 } 2285 }
2275 2286
2276 PassRefPtr<SerializedScriptValue> SerializedScriptValue::create() 2287 PassRefPtr<SerializedScriptValue> SerializedScriptValue::create()
2277 { 2288 {
2278 return adoptRef(new SerializedScriptValue()); 2289 return adoptRef(new SerializedScriptValue());
2279 } 2290 }
2280 2291
2281 PassRefPtr<SerializedScriptValue> SerializedScriptValue::nullValue() 2292 PassRefPtr<SerializedScriptValue> SerializedScriptValue::nullValue()
2282 { 2293 {
2283 return nullValue(v8::Isolate::GetCurrent()); 2294 return nullValue(v8::Isolate::GetCurrent());
2284 } 2295 }
2285 2296
2286 PassRefPtr<SerializedScriptValue> SerializedScriptValue::nullValue(v8::Isolate* isolate) 2297 PassRefPtr<SerializedScriptValue> SerializedScriptValue::nullValue(v8::Isolate* isolate)
2287 { 2298 {
2288 Writer writer(isolate); 2299 Writer writer(isolate);
2289 writer.writeNull(); 2300 writer.writeNull();
2290 String wireData = String::adopt(writer.data()); 2301 String wireData = writer.takeWireString();
2291 return adoptRef(new SerializedScriptValue(wireData)); 2302 return adoptRef(new SerializedScriptValue(wireData));
2292 } 2303 }
2293 2304
2294 PassRefPtr<SerializedScriptValue> SerializedScriptValue::undefinedValue() 2305 PassRefPtr<SerializedScriptValue> SerializedScriptValue::undefinedValue()
2295 { 2306 {
2296 return undefinedValue(v8::Isolate::GetCurrent()); 2307 return undefinedValue(v8::Isolate::GetCurrent());
2297 } 2308 }
2298 2309
2299 PassRefPtr<SerializedScriptValue> SerializedScriptValue::undefinedValue(v8::Isol ate* isolate) 2310 PassRefPtr<SerializedScriptValue> SerializedScriptValue::undefinedValue(v8::Isol ate* isolate)
2300 { 2311 {
2301 Writer writer(isolate); 2312 Writer writer(isolate);
2302 writer.writeUndefined(); 2313 writer.writeUndefined();
2303 String wireData = String::adopt(writer.data()); 2314 String wireData = writer.takeWireString();
2304 return adoptRef(new SerializedScriptValue(wireData)); 2315 return adoptRef(new SerializedScriptValue(wireData));
2305 } 2316 }
2306 2317
2307 PassRefPtr<SerializedScriptValue> SerializedScriptValue::booleanValue(bool value ) 2318 PassRefPtr<SerializedScriptValue> SerializedScriptValue::booleanValue(bool value )
2308 { 2319 {
2309 return booleanValue(value, v8::Isolate::GetCurrent()); 2320 return booleanValue(value, v8::Isolate::GetCurrent());
2310 } 2321 }
2311 2322
2312 PassRefPtr<SerializedScriptValue> SerializedScriptValue::booleanValue(bool value , v8::Isolate* isolate) 2323 PassRefPtr<SerializedScriptValue> SerializedScriptValue::booleanValue(bool value , v8::Isolate* isolate)
2313 { 2324 {
2314 Writer writer(isolate); 2325 Writer writer(isolate);
2315 if (value) 2326 if (value)
2316 writer.writeTrue(); 2327 writer.writeTrue();
2317 else 2328 else
2318 writer.writeFalse(); 2329 writer.writeFalse();
2319 String wireData = String::adopt(writer.data()); 2330 String wireData = writer.takeWireString();
2320 return adoptRef(new SerializedScriptValue(wireData)); 2331 return adoptRef(new SerializedScriptValue(wireData));
2321 } 2332 }
2322 2333
2323 PassRefPtr<SerializedScriptValue> SerializedScriptValue::numberValue(double valu e) 2334 PassRefPtr<SerializedScriptValue> SerializedScriptValue::numberValue(double valu e)
2324 { 2335 {
2325 return numberValue(value, v8::Isolate::GetCurrent()); 2336 return numberValue(value, v8::Isolate::GetCurrent());
2326 } 2337 }
2327 2338
2328 PassRefPtr<SerializedScriptValue> SerializedScriptValue::numberValue(double valu e, v8::Isolate* isolate) 2339 PassRefPtr<SerializedScriptValue> SerializedScriptValue::numberValue(double valu e, v8::Isolate* isolate)
2329 { 2340 {
2330 Writer writer(isolate); 2341 Writer writer(isolate);
2331 writer.writeNumber(value); 2342 writer.writeNumber(value);
2332 String wireData = String::adopt(writer.data()); 2343 String wireData = writer.takeWireString();
2333 return adoptRef(new SerializedScriptValue(wireData)); 2344 return adoptRef(new SerializedScriptValue(wireData));
2334 } 2345 }
2335 2346
2336 // Convert serialized string to big endian wire data. 2347 // Convert serialized string to big endian wire data.
2337 void SerializedScriptValue::toWireBytes(Vector<char>& result) const 2348 void SerializedScriptValue::toWireBytes(Vector<char>& result) const
2338 { 2349 {
2339 ASSERT(result.isEmpty()); 2350 ASSERT(result.isEmpty());
2340 size_t length = m_data.length(); 2351 size_t length = m_data.length();
2341 result.resize(length * sizeof(UChar)); 2352 result.resize(length * sizeof(UChar));
2342 UChar* dst = reinterpret_cast<UChar*>(result.data()); 2353 UChar* dst = reinterpret_cast<UChar*>(result.data());
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
2456 if (policy == ThrowExceptions) 2467 if (policy == ThrowExceptions)
2457 setDOMException(InvalidStateError, isolate); 2468 setDOMException(InvalidStateError, isolate);
2458 return; 2469 return;
2459 case Serializer::JSFailure: 2470 case Serializer::JSFailure:
2460 // If there was a JS failure (but no exception), there's not 2471 // If there was a JS failure (but no exception), there's not
2461 // much we can do except for unwinding the C++ stack by 2472 // much we can do except for unwinding the C++ stack by
2462 // pretending there was a JS exception. 2473 // pretending there was a JS exception.
2463 didThrow = true; 2474 didThrow = true;
2464 return; 2475 return;
2465 case Serializer::Success: 2476 case Serializer::Success:
2466 // FIXME: This call to isolatedCopy should be redundant. 2477 m_data = writer.takeWireString();
2467 m_data = String(String::adopt(writer.data())).isolatedCopy(); 2478 ASSERT(m_data.impl()->hasOneRef());
2468 if (arrayBuffers && arrayBuffers->size()) 2479 if (arrayBuffers && arrayBuffers->size())
2469 m_arrayBufferContentsArray = transferArrayBuffers(*arrayBuffers, did Throw, isolate); 2480 m_arrayBufferContentsArray = transferArrayBuffers(*arrayBuffers, did Throw, isolate);
2470 return; 2481 return;
2471 case Serializer::JSException: 2482 case Serializer::JSException:
2472 // We should never get here because this case was handled above. 2483 // We should never get here because this case was handled above.
2473 break; 2484 break;
2474 } 2485 }
2475 ASSERT_NOT_REACHED(); 2486 ASSERT_NOT_REACHED();
2476 } 2487 }
2477 2488
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
2531 v8::V8::AdjustAmountOfExternalAllocatedMemory(-m_externallyAllocatedMemo ry); 2542 v8::V8::AdjustAmountOfExternalAllocatedMemory(-m_externallyAllocatedMemo ry);
2532 } 2543 }
2533 } 2544 }
2534 2545
2535 uint32_t SerializedScriptValue::wireFormatVersion() 2546 uint32_t SerializedScriptValue::wireFormatVersion()
2536 { 2547 {
2537 return WebCore::wireFormatVersion; 2548 return WebCore::wireFormatVersion;
2538 } 2549 }
2539 2550
2540 } // namespace WebCore 2551 } // namespace WebCore
OLDNEW
« no previous file with comments | « no previous file | Source/wtf/text/StringBuffer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698