OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #ifndef COURGETTE_MEMORY_ALLOCATOR_H_ | 5 #ifndef COURGETTE_MEMORY_ALLOCATOR_H_ |
6 #define COURGETTE_MEMORY_ALLOCATOR_H_ | 6 #define COURGETTE_MEMORY_ALLOCATOR_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
326 CheckBool reserve(size_t size) WARN_UNUSED_RESULT { | 326 CheckBool reserve(size_t size) WARN_UNUSED_RESULT { |
327 if (failed()) | 327 if (failed()) |
328 return false; | 328 return false; |
329 | 329 |
330 if (size <= alloc_size_) | 330 if (size <= alloc_size_) |
331 return true; | 331 return true; |
332 | 332 |
333 if (size < kStartSize) | 333 if (size < kStartSize) |
334 size = kStartSize; | 334 size = kStartSize; |
335 | 335 |
336 // Use a size 1% higher than requested. In practice, this makes Courgette as | |
337 // much as 5x faster on typical Chrome update payloads as a lot of future | |
338 // reserve() calls will become no-ops instead of costly resizes that copy | |
339 // all the data. Note that doing this here instead of outside the function | |
340 // is more efficient, since it's after the no-op early return checks above. | |
341 size *= 1.01; | |
gab
2014/09/12 04:12:19
This will have no impact on reserves below 100, sh
gab
2014/09/12 04:16:06
Oops s/multiple of 2/power of 2/ of course.
| |
336 T* new_buffer = alloc_.allocate(size); | 342 T* new_buffer = alloc_.allocate(size); |
337 if (!new_buffer) { | 343 if (!new_buffer) { |
338 clear(); | 344 clear(); |
339 alloc_size_ = kAllocationFailure; | 345 alloc_size_ = kAllocationFailure; |
340 } else { | 346 } else { |
341 if (buffer_) { | 347 if (buffer_) { |
342 memcpy(new_buffer, buffer_, size_ * sizeof(T)); | 348 memcpy(new_buffer, buffer_, size_ * sizeof(T)); |
343 alloc_.deallocate(buffer_, alloc_size_); | 349 alloc_.deallocate(buffer_, alloc_size_); |
344 } | 350 } |
345 buffer_ = new_buffer; | 351 buffer_ = new_buffer; |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
457 protected: | 463 protected: |
458 T* buffer_; | 464 T* buffer_; |
459 size_t size_; // how much of the buffer we're using. | 465 size_t size_; // how much of the buffer we're using. |
460 size_t alloc_size_; // how much space we have allocated. | 466 size_t alloc_size_; // how much space we have allocated. |
461 Allocator alloc_; | 467 Allocator alloc_; |
462 }; | 468 }; |
463 | 469 |
464 } // namespace courgette | 470 } // namespace courgette |
465 | 471 |
466 #endif // COURGETTE_MEMORY_ALLOCATOR_H_ | 472 #endif // COURGETTE_MEMORY_ALLOCATOR_H_ |
OLD | NEW |