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

Side by Side Diff: base/pickle.cc

Issue 35893002: IPC pickling optimization for render passes. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ccmessagesperf-reserve: add algorithm header Created 7 years, 2 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/pickle.h" 5 #include "base/pickle.h"
6 6
7 #include <stdlib.h> 7 #include <stdlib.h>
8 8
9 #include <algorithm> // for max() 9 #include <algorithm> // for max()
10 10
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 if (!header_size_) 188 if (!header_size_)
189 header_ = NULL; 189 header_ = NULL;
190 } 190 }
191 191
192 Pickle::Pickle(const Pickle& other) 192 Pickle::Pickle(const Pickle& other)
193 : header_(NULL), 193 : header_(NULL),
194 header_size_(other.header_size_), 194 header_size_(other.header_size_),
195 capacity_(0), 195 capacity_(0),
196 variable_buffer_offset_(other.variable_buffer_offset_) { 196 variable_buffer_offset_(other.variable_buffer_offset_) {
197 size_t payload_size = header_size_ + other.header_->payload_size; 197 size_t payload_size = header_size_ + other.header_->payload_size;
198 bool resized = Resize(payload_size); 198 Resize(payload_size);
199 CHECK(resized); // Realloc failed.
200 memcpy(header_, other.header_, payload_size); 199 memcpy(header_, other.header_, payload_size);
201 } 200 }
202 201
203 Pickle::~Pickle() { 202 Pickle::~Pickle() {
204 if (capacity_ != kCapacityReadOnly) 203 if (capacity_ != kCapacityReadOnly)
205 free(header_); 204 free(header_);
206 } 205 }
207 206
208 Pickle& Pickle::operator=(const Pickle& other) { 207 Pickle& Pickle::operator=(const Pickle& other) {
209 if (this == &other) { 208 if (this == &other) {
210 NOTREACHED(); 209 NOTREACHED();
211 return *this; 210 return *this;
212 } 211 }
213 if (capacity_ == kCapacityReadOnly) { 212 if (capacity_ == kCapacityReadOnly) {
214 header_ = NULL; 213 header_ = NULL;
215 capacity_ = 0; 214 capacity_ = 0;
216 } 215 }
217 if (header_size_ != other.header_size_) { 216 if (header_size_ != other.header_size_) {
218 free(header_); 217 free(header_);
219 header_ = NULL; 218 header_ = NULL;
220 header_size_ = other.header_size_; 219 header_size_ = other.header_size_;
221 } 220 }
222 bool resized = Resize(other.header_size_ + other.header_->payload_size); 221 Resize(other.header_size_ + other.header_->payload_size);
223 CHECK(resized); // Realloc failed.
224 memcpy(header_, other.header_, 222 memcpy(header_, other.header_,
225 other.header_size_ + other.header_->payload_size); 223 other.header_size_ + other.header_->payload_size);
226 variable_buffer_offset_ = other.variable_buffer_offset_; 224 variable_buffer_offset_ = other.variable_buffer_offset_;
227 return *this; 225 return *this;
228 } 226 }
229 227
230 bool Pickle::WriteString(const std::string& value) { 228 bool Pickle::WriteString(const std::string& value) {
231 if (!WriteInt(static_cast<int>(value.size()))) 229 if (!WriteInt(static_cast<int>(value.size())))
232 return false; 230 return false;
233 231
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 if (new_length < 0 || new_length > *cur_length) { 295 if (new_length < 0 || new_length > *cur_length) {
298 NOTREACHED() << "Invalid length in TrimWriteData."; 296 NOTREACHED() << "Invalid length in TrimWriteData.";
299 return; 297 return;
300 } 298 }
301 299
302 // Update the payload size and variable buffer size 300 // Update the payload size and variable buffer size
303 header_->payload_size -= (*cur_length - new_length); 301 header_->payload_size -= (*cur_length - new_length);
304 *cur_length = new_length; 302 *cur_length = new_length;
305 } 303 }
306 304
305 void Pickle::Reserve(size_t length) {
306 // write at a uint32-aligned offset from the beginning of the header
307 size_t offset = AlignInt(header_->payload_size, sizeof(uint32));
308
309 size_t new_size = offset + length;
310 size_t needed_size = header_size_ + new_size;
311 if (needed_size > capacity_)
312 Resize(capacity_ * 2 + needed_size);
Tom Sepez 2013/10/24 17:48:06 Why 2x? Also, this should be a bool return value,
danakj 2013/10/24 17:51:38 Same reason as in BeginWrite, in order to amortize
313 }
314
307 char* Pickle::BeginWrite(size_t length) { 315 char* Pickle::BeginWrite(size_t length) {
308 // write at a uint32-aligned offset from the beginning of the header 316 // write at a uint32-aligned offset from the beginning of the header
309 size_t offset = AlignInt(header_->payload_size, sizeof(uint32)); 317 size_t offset = AlignInt(header_->payload_size, sizeof(uint32));
310 318
311 size_t new_size = offset + length; 319 size_t new_size = offset + length;
312 size_t needed_size = header_size_ + new_size; 320 size_t needed_size = header_size_ + new_size;
313 if (needed_size > capacity_ && !Resize(std::max(capacity_ * 2, needed_size))) 321 if (needed_size > capacity_)
314 return NULL; 322 Resize(std::max(capacity_ * 2, needed_size));
315 323
316 #ifdef ARCH_CPU_64_BITS 324 #ifdef ARCH_CPU_64_BITS
317 DCHECK_LE(length, kuint32max); 325 DCHECK_LE(length, kuint32max);
318 #endif 326 #endif
319 327
320 header_->payload_size = static_cast<uint32>(new_size); 328 header_->payload_size = static_cast<uint32>(new_size);
321 return mutable_payload() + offset; 329 return mutable_payload() + offset;
322 } 330 }
323 331
324 void Pickle::EndWrite(char* dest, int length) { 332 void Pickle::EndWrite(char* dest, int length) {
325 // Zero-pad to keep tools like valgrind from complaining about uninitialized 333 // Zero-pad to keep tools like valgrind from complaining about uninitialized
326 // memory. 334 // memory.
327 if (length % sizeof(uint32)) 335 if (length % sizeof(uint32))
328 memset(dest + length, 0, sizeof(uint32) - (length % sizeof(uint32))); 336 memset(dest + length, 0, sizeof(uint32) - (length % sizeof(uint32)));
329 } 337 }
330 338
331 bool Pickle::Resize(size_t new_capacity) { 339 void Pickle::Resize(size_t new_capacity) {
340 CHECK_NE(capacity_, kCapacityReadOnly);
Tom Sepez 2013/10/24 17:48:06 There's a minor problem here in that if realloc fa
danakj 2013/10/24 17:51:38 if realloc fails, we will crash inside tcmalloc no
341
332 new_capacity = AlignInt(new_capacity, kPayloadUnit); 342 new_capacity = AlignInt(new_capacity, kPayloadUnit);
333
334 CHECK_NE(capacity_, kCapacityReadOnly);
335 void* p = realloc(header_, new_capacity); 343 void* p = realloc(header_, new_capacity);
336 if (!p)
337 return false;
338
339 header_ = reinterpret_cast<Header*>(p); 344 header_ = reinterpret_cast<Header*>(p);
340 capacity_ = new_capacity; 345 capacity_ = new_capacity;
341 return true;
342 } 346 }
343 347
344 // static 348 // static
345 const char* Pickle::FindNext(size_t header_size, 349 const char* Pickle::FindNext(size_t header_size,
346 const char* start, 350 const char* start,
347 const char* end) { 351 const char* end) {
348 DCHECK_EQ(header_size, AlignInt(header_size, sizeof(uint32))); 352 DCHECK_EQ(header_size, AlignInt(header_size, sizeof(uint32)));
349 DCHECK_LE(header_size, static_cast<size_t>(kPayloadUnit)); 353 DCHECK_LE(header_size, static_cast<size_t>(kPayloadUnit));
350 354
351 if (static_cast<size_t>(end - start) < sizeof(Header)) 355 if (static_cast<size_t>(end - start) < sizeof(Header))
352 return NULL; 356 return NULL;
353 357
354 const Header* hdr = reinterpret_cast<const Header*>(start); 358 const Header* hdr = reinterpret_cast<const Header*>(start);
355 const char* payload_base = start + header_size; 359 const char* payload_base = start + header_size;
356 const char* payload_end = payload_base + hdr->payload_size; 360 const char* payload_end = payload_base + hdr->payload_size;
357 if (payload_end < payload_base) 361 if (payload_end < payload_base)
358 return NULL; 362 return NULL;
359 363
360 return (payload_end > end) ? NULL : payload_end; 364 return (payload_end > end) ? NULL : payload_end;
361 } 365 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698