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

Side by Side Diff: components/packed_ct_ev_whitelist/bit_stream_reader.cc

Issue 1070903003: Optimize BitStreamReader, speed up EV Whitelist loading (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "components/packed_ct_ev_whitelist/bit_stream_reader.h" 5 #include "components/packed_ct_ev_whitelist/bit_stream_reader.h"
6 6
7 #include "base/big_endian.h" 7 #include "base/big_endian.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/numerics/safe_conversions.h" 9 #include "base/numerics/safe_conversions.h"
10
Ryan Sleevi 2015/04/09 01:33:57 Don't delete this newline
ian fette 2015/04/09 16:22:13 Done.
11 namespace packed_ct_ev_whitelist { 10 namespace packed_ct_ev_whitelist {
12 namespace internal { 11 namespace internal {
13 12
14 BitStreamReader::BitStreamReader(const base::StringPiece& source) 13 BitStreamReader::BitStreamReader(const base::StringPiece& source)
15 : source_(source), current_byte_(0), current_bit_(7) { 14 : source_(source), current_byte_(0), current_bit_(7) {
16 DCHECK_LT(source_.length(), UINT32_MAX); 15 DCHECK_LT(source_.length(), UINT32_MAX);
17 } 16 }
18 17
19 bool BitStreamReader::ReadUnaryEncoding(uint64_t* out) { 18 bool BitStreamReader::ReadUnaryEncoding(uint64_t* out) {
20 if (BitsLeft() == 0) 19 if (BitsLeft() == 0)
21 return false; 20 return false;
22 21
23 *out = 0; 22 *out = 0;
24 while ((BitsLeft() > 0) && ReadBit()) 23 while ((BitsLeft() > 0) && ReadBit())
25 ++(*out); 24 ++(*out);
26 25
27 return true; 26 return true;
28 } 27 }
29 28
30 bool BitStreamReader::ReadBits(uint8_t num_bits, uint64_t* out) { 29 bool BitStreamReader::ReadBits(uint8_t num_bits, uint64_t* out) {
31 DCHECK_LE(num_bits, 64); 30 DCHECK_LE(num_bits, 64);
32 31
33 if (BitsLeft() < num_bits) 32 if (BitsLeft() < num_bits)
34 return false; 33 return false;
35 34
36 *out = 0; 35 *out = 0;
37 for (uint8_t i = 0; i < num_bits; ++i) 36
38 (*out) |= (static_cast<uint64_t>(ReadBit()) << (num_bits - (i + 1))); 37 for (; num_bits && (current_bit_ != 7); --num_bits)
38 (*out) |= (static_cast<uint64_t>(ReadBit() << (num_bits - 1)));
39 for (; num_bits / 8; num_bits -= 8) {
40 (*out) |= (static_cast<uint64_t>(source_.data()[current_byte_]) & 0xff)
41 << (num_bits - 8);
Ryan Sleevi 2015/04/09 01:33:57 I'm not terribly keen on this, which duplicates th
ian fette 2015/04/09 02:43:06 One important precondition is whether the stream i
Ryan Sleevi 2015/04/09 02:57:16 Right, I was just talking in the context of the se
ian fette 2015/04/09 16:22:13 Done.
42 current_byte_++;
43 }
44 for (; num_bits; --num_bits)
45 (*out) |= (static_cast<uint64_t>(ReadBit() << (num_bits - 1)));
39 46
40 return true; 47 return true;
41 } 48 }
42 49
43 uint64_t BitStreamReader::BitsLeft() const { 50 uint64_t BitStreamReader::BitsLeft() const {
44 if (current_byte_ == source_.length()) 51 if (current_byte_ == source_.length())
45 return 0; 52 return 0;
46 DCHECK_GT(source_.length(), current_byte_); 53 DCHECK_GT(source_.length(), current_byte_);
47 return (source_.length() - (current_byte_ + 1)) * 8 + current_bit_ + 1; 54 return (source_.length() - (current_byte_ + 1)) * 8 + current_bit_ + 1;
48 } 55 }
49 56
50 uint8_t BitStreamReader::ReadBit() { 57 uint8_t BitStreamReader::ReadBit() {
51 DCHECK_GT(BitsLeft(), 0u); 58 DCHECK_GT(BitsLeft(), 0u);
52 DCHECK(current_bit_ < 8 && current_bit_ >= 0); 59 DCHECK(current_bit_ < 8 && current_bit_ >= 0);
53 uint8_t res = 60 uint8_t res =
54 (source_.data()[current_byte_] & (1 << current_bit_)) >> current_bit_; 61 (source_.data()[current_byte_] & (1 << current_bit_)) >> current_bit_;
55 current_bit_--; 62 current_bit_--;
56 if (current_bit_ < 0) { 63 if (current_bit_ < 0) {
57 current_byte_++; 64 current_byte_++;
58 current_bit_ = 7; 65 current_bit_ = 7;
59 } 66 }
60 67
61 return res; 68 return res;
62 } 69 }
63 70
64 } // namespace internal 71 } // namespace internal
65 } // namespace packed_ct_ev_whitelist 72 } // namespace packed_ct_ev_whitelist
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