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

Side by Side Diff: mojo/edk/system/channel.cc

Issue 1835933002: [mojo-edk] Trim read buffer to keep its size down (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "mojo/edk/system/channel.h" 5 #include "mojo/edk/system/channel.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <limits> 10 #include <limits>
(...skipping 16 matching lines...) Expand all
27 "Invalid Header size."); 27 "Invalid Header size.");
28 28
29 #if defined(OS_CHROMEOS) || defined(OS_ANDROID) 29 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
30 static_assert(sizeof(Channel::Message::Header) == 8, 30 static_assert(sizeof(Channel::Message::Header) == 8,
31 "Header must be 8 bytes on ChromeOS and Android"); 31 "Header must be 8 bytes on ChromeOS and Android");
32 #endif 32 #endif
33 33
34 } // namespace 34 } // namespace
35 35
36 const size_t kReadBufferSize = 4096; 36 const size_t kReadBufferSize = 4096;
37 const size_t kMaxUnusedReadBufferCapacity = 256 * 1024; 37 const size_t kMaxUnusedReadBufferCapacity = 64 * 1024;
38 const size_t kMaxChannelMessageSize = 256 * 1024 * 1024; 38 const size_t kMaxChannelMessageSize = 256 * 1024 * 1024;
39 const size_t kMaxAttachedHandles = 128; 39 const size_t kMaxAttachedHandles = 128;
40 40
41 Channel::Message::Message(size_t payload_size, 41 Channel::Message::Message(size_t payload_size,
42 size_t max_handles, 42 size_t max_handles,
43 Header::MessageType message_type) 43 Header::MessageType message_type)
44 : max_handles_(max_handles) { 44 : max_handles_(max_handles) {
45 DCHECK_LE(max_handles_, kMaxAttachedHandles); 45 DCHECK_LE(max_handles_, kMaxAttachedHandles);
46 46
47 size_t extra_header_size = 0; 47 size_t extra_header_size = 0;
(...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 size_ = std::max(num_preserved_bytes, kReadBufferSize); 401 size_ = std::max(num_preserved_bytes, kReadBufferSize);
402 char* new_data = static_cast<char*>( 402 char* new_data = static_cast<char*>(
403 base::AlignedAlloc(size_, kChannelMessageAlignment)); 403 base::AlignedAlloc(size_, kChannelMessageAlignment));
404 memcpy(new_data, data_ + num_discarded_bytes_, num_preserved_bytes); 404 memcpy(new_data, data_ + num_discarded_bytes_, num_preserved_bytes);
405 base::AlignedFree(data_); 405 base::AlignedFree(data_);
406 data_ = new_data; 406 data_ = new_data;
407 num_discarded_bytes_ = 0; 407 num_discarded_bytes_ = 0;
408 num_occupied_bytes_ = num_preserved_bytes; 408 num_occupied_bytes_ = num_preserved_bytes;
409 } 409 }
410 410
411 // TODO: we should also adaptively shrink the buffer in case of the 411 if (num_occupied_bytes_ == 0 && size_ > kMaxUnusedReadBufferCapacity) {
412 // occasional abnormally large read. 412 // Opportunistically shrink the read buffer back down to a small size if
413 // it's grown very large. We only do this if there are no remaining
414 // unconsumed bytes in the buffer to avoid copies in most the common
415 // cases.
416 size_ = kMaxUnusedReadBufferCapacity;
417 base::AlignedFree(data_);
418 data_ = static_cast<char*>(
419 base::AlignedAlloc(size_, kChannelMessageAlignment));
420 }
413 } 421 }
414 422
415 private: 423 private:
416 char* data_ = nullptr; 424 char* data_ = nullptr;
417 425
418 // The total size of the allocated buffer. 426 // The total size of the allocated buffer.
419 size_t size_ = 0; 427 size_t size_ = 0;
420 428
421 // The number of discarded bytes at the beginning of the allocated buffer. 429 // The number of discarded bytes at the beginning of the allocated buffer.
422 size_t num_discarded_bytes_ = 0; 430 size_t num_discarded_bytes_ = 0;
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 return true; 523 return true;
516 } 524 }
517 525
518 void Channel::OnError() { 526 void Channel::OnError() {
519 if (delegate_) 527 if (delegate_)
520 delegate_->OnChannelError(); 528 delegate_->OnChannelError();
521 } 529 }
522 530
523 } // namespace edk 531 } // namespace edk
524 } // namespace mojo 532 } // namespace mojo
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698