Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/renderer_host/duplicate_resource_handler.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/metrics/histogram.h" | |
| 11 #include "content/browser/renderer_host/resource_request_info_impl.h" | |
| 12 #include "net/base/io_buffer.h" | |
| 13 #include "third_party/smhasher/src/MurmurHash3.h" | |
| 14 | |
| 15 | |
| 16 namespace content { | |
| 17 | |
| 18 namespace{ | |
| 19 | |
| 20 std::set<uint32>* GetSetOfHashes() { | |
| 21 static std::set<uint32> seen_resources; | |
| 22 return &seen_resources; | |
| 23 } | |
| 24 | |
| 25 } // namespace | |
| 26 | |
| 27 DuplicateResourceHandler::DuplicateResourceHandler( | |
| 28 scoped_ptr<ResourceHandler> next_handler, | |
| 29 net::URLRequest* request) | |
| 30 : LayeredResourceHandler(next_handler.Pass()), | |
| 31 bytes_hit_(0), | |
| 32 bytes_miss_(0), | |
| 33 read_buffer_size_(0), // keep track of bytes in read buffer | |
| 34 request_(request) { | |
| 35 } | |
| 36 | |
| 37 DuplicateResourceHandler::~DuplicateResourceHandler() { | |
| 38 } | |
| 39 | |
| 40 bool DuplicateResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf, | |
| 41 int* buf_size, int min_size) { | |
| 42 DCHECK_EQ(-1, min_size); | |
| 43 | |
| 44 if (next_handler_->OnWillRead(request_id, buf, buf_size, min_size)) { | |
| 45 read_buffer_ = *buf; | |
| 46 read_buffer_size_ = *buf_size; | |
| 47 return true; | |
| 48 } | |
| 49 | |
| 50 return false; | |
| 51 } | |
| 52 | |
| 53 bool DuplicateResourceHandler::OnReadCompleted(int request_id, int bytes_read, | |
| 54 bool* defer) { | |
| 55 if (!next_handler_->OnReadCompleted(request_id, bytes_read, defer)) | |
| 56 return false; | |
| 57 | |
| 58 DVLOG(4) << "read_buffer_ is " << read_buffer_.get(); | |
| 59 DVLOG(4) << "read_buffer_->data() is " << read_buffer_->data(); | |
| 60 | |
| 61 // find hash of resource | |
| 62 uint32 buf_hash; | |
| 63 MurmurHash3_x86_32(read_buffer_->data(), bytes_read, 0x0, &buf_hash); | |
|
gavinp
2012/07/13 21:16:04
0, but in hexadecimal? Just use 0.
If it helps, y
| |
| 64 | |
| 65 const bool did_we_find_it = GetSetOfHashes()->find(buf_hash) != GetSetOfHashes ()->end(); | |
| 66 UMA_HISTOGRAM_BOOLEAN("Duplicate.HitRate", did_we_find_it); | |
| 67 | |
| 68 // if element is in hash | |
|
gavinp
2012/07/13 21:16:04
Your comment not full sentence with punctuation
| |
| 69 if (did_we_find_it) { | |
| 70 bytes_hit_ += bytes_read; | |
| 71 | |
| 72 } else { | |
| 73 bytes_miss_ += bytes_read; | |
| 74 GetSetOfHashes()->insert(buf_hash); | |
| 75 } | |
| 76 | |
| 77 read_buffer_.release(); | |
| 78 read_buffer_size_ = 0; | |
| 79 | |
| 80 return true; | |
| 81 } | |
| 82 | |
| 83 } //namespace content | |
| OLD | NEW |