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

Side by Side Diff: net/spdy/hpack_header_table.cc

Issue 549583003: Make HPACK static table static and immutable. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 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 | « net/spdy/hpack_header_table.h ('k') | net/spdy/hpack_header_table_test.cc » ('j') | 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 "net/spdy/hpack_header_table.h" 5 #include "net/spdy/hpack_header_table.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "net/spdy/hpack_constants.h" 10 #include "net/spdy/hpack_constants.h"
11 #include "net/spdy/hpack_static_table.h"
11 #include "net/spdy/hpack_string_util.h" 12 #include "net/spdy/hpack_string_util.h"
12 13
13 namespace net { 14 namespace net {
14 15
15 using base::StringPiece; 16 using base::StringPiece;
16 17
17 namespace {
18
19 // An entry in the static table. Must be a POD in order to avoid
20 // static initializers, i.e. no user-defined constructors or
21 // destructors.
22 struct StaticEntry {
23 const char* const name;
24 const size_t name_len;
25 const char* const value;
26 const size_t value_len;
27 };
28
29 // The "constructor" for a StaticEntry that computes the lengths at
30 // compile time.
31 #define STATIC_ENTRY(name, value) \
32 { name, arraysize(name) - 1, value, arraysize(value) - 1 }
33
34 const StaticEntry kStaticTable[] = {
35 STATIC_ENTRY(":authority" , ""), // 1
36 STATIC_ENTRY(":method" , "GET"), // 2
37 STATIC_ENTRY(":method" , "POST"), // 3
38 STATIC_ENTRY(":path" , "/"), // 4
39 STATIC_ENTRY(":path" , "/index.html"), // 5
40 STATIC_ENTRY(":scheme" , "http"), // 6
41 STATIC_ENTRY(":scheme" , "https"), // 7
42 STATIC_ENTRY(":status" , "200"), // 8
43 STATIC_ENTRY(":status" , "204"), // 9
44 STATIC_ENTRY(":status" , "206"), // 10
45 STATIC_ENTRY(":status" , "304"), // 11
46 STATIC_ENTRY(":status" , "400"), // 12
47 STATIC_ENTRY(":status" , "404"), // 13
48 STATIC_ENTRY(":status" , "500"), // 14
49 STATIC_ENTRY("accept-charset" , ""), // 15
50 STATIC_ENTRY("accept-encoding" , "gzip, deflate"), // 16
51 STATIC_ENTRY("accept-language" , ""), // 17
52 STATIC_ENTRY("accept-ranges" , ""), // 18
53 STATIC_ENTRY("accept" , ""), // 19
54 STATIC_ENTRY("access-control-allow-origin", ""), // 20
55 STATIC_ENTRY("age" , ""), // 21
56 STATIC_ENTRY("allow" , ""), // 22
57 STATIC_ENTRY("authorization" , ""), // 23
58 STATIC_ENTRY("cache-control" , ""), // 24
59 STATIC_ENTRY("content-disposition" , ""), // 25
60 STATIC_ENTRY("content-encoding" , ""), // 26
61 STATIC_ENTRY("content-language" , ""), // 27
62 STATIC_ENTRY("content-length" , ""), // 28
63 STATIC_ENTRY("content-location" , ""), // 29
64 STATIC_ENTRY("content-range" , ""), // 30
65 STATIC_ENTRY("content-type" , ""), // 31
66 STATIC_ENTRY("cookie" , ""), // 32
67 STATIC_ENTRY("date" , ""), // 33
68 STATIC_ENTRY("etag" , ""), // 34
69 STATIC_ENTRY("expect" , ""), // 35
70 STATIC_ENTRY("expires" , ""), // 36
71 STATIC_ENTRY("from" , ""), // 37
72 STATIC_ENTRY("host" , ""), // 38
73 STATIC_ENTRY("if-match" , ""), // 39
74 STATIC_ENTRY("if-modified-since" , ""), // 40
75 STATIC_ENTRY("if-none-match" , ""), // 41
76 STATIC_ENTRY("if-range" , ""), // 42
77 STATIC_ENTRY("if-unmodified-since" , ""), // 43
78 STATIC_ENTRY("last-modified" , ""), // 44
79 STATIC_ENTRY("link" , ""), // 45
80 STATIC_ENTRY("location" , ""), // 46
81 STATIC_ENTRY("max-forwards" , ""), // 47
82 STATIC_ENTRY("proxy-authenticate" , ""), // 48
83 STATIC_ENTRY("proxy-authorization" , ""), // 49
84 STATIC_ENTRY("range" , ""), // 50
85 STATIC_ENTRY("referer" , ""), // 51
86 STATIC_ENTRY("refresh" , ""), // 52
87 STATIC_ENTRY("retry-after" , ""), // 53
88 STATIC_ENTRY("server" , ""), // 54
89 STATIC_ENTRY("set-cookie" , ""), // 55
90 STATIC_ENTRY("strict-transport-security" , ""), // 56
91 STATIC_ENTRY("transfer-encoding" , ""), // 57
92 STATIC_ENTRY("user-agent" , ""), // 58
93 STATIC_ENTRY("vary" , ""), // 59
94 STATIC_ENTRY("via" , ""), // 60
95 STATIC_ENTRY("www-authenticate" , ""), // 61
96 };
97
98 #undef STATIC_ENTRY
99
100 } // namespace
101
102 bool HpackHeaderTable::EntryComparator::operator() ( 18 bool HpackHeaderTable::EntryComparator::operator() (
103 const HpackEntry* lhs, const HpackEntry* rhs) const { 19 const HpackEntry* lhs, const HpackEntry* rhs) const {
104 int result = lhs->name().compare(rhs->name()); 20 int result = lhs->name().compare(rhs->name());
105 if (result != 0) 21 if (result != 0)
106 return result < 0; 22 return result < 0;
107 result = lhs->value().compare(rhs->value()); 23 result = lhs->value().compare(rhs->value());
108 if (result != 0) 24 if (result != 0)
109 return result < 0; 25 return result < 0;
110 const size_t lhs_index = table_->IndexOf(lhs); 26 const size_t lhs_index = lhs->IsLookup() ? 0 : 1 + lhs->InsertionIndex();
111 const size_t rhs_index = table_->IndexOf(rhs); 27 const size_t rhs_index = rhs->IsLookup() ? 0 : 1 + rhs->InsertionIndex();
112 DCHECK(lhs == rhs || lhs_index != rhs_index) 28 DCHECK(lhs == rhs || lhs_index != rhs_index)
113 << "lhs: (" << lhs->name() << ", " << rhs->value() << ") rhs: (" 29 << "lhs: (" << lhs->name() << ", " << rhs->value() << ") rhs: ("
114 << rhs->name() << ", " << rhs->value() << ")" 30 << rhs->name() << ", " << rhs->value() << ")"
115 << " lhs index: " << lhs_index << " rhs index: " << rhs_index; 31 << " lhs index: " << lhs_index << " rhs index: " << rhs_index;
116 return lhs_index < rhs_index; 32 return lhs_index < rhs_index;
117 } 33 }
118 34
119 HpackHeaderTable::HpackHeaderTable() 35 HpackHeaderTable::HpackHeaderTable()
120 : index_(EntryComparator(this)), 36 : static_entries_(ObtainHpackStaticTable().GetStaticEntries()),
37 static_index_(ObtainHpackStaticTable().GetStaticIndex()),
121 settings_size_bound_(kDefaultHeaderTableSizeSetting), 38 settings_size_bound_(kDefaultHeaderTableSizeSetting),
122 size_(0), 39 size_(0),
123 max_size_(kDefaultHeaderTableSizeSetting), 40 max_size_(kDefaultHeaderTableSizeSetting),
124 total_insertions_(0) { 41 total_insertions_(static_entries_.size()) {}
125 for (const StaticEntry* it = kStaticTable;
126 it != kStaticTable + arraysize(kStaticTable); ++it) {
127 static_entries_.push_back(
128 HpackEntry(StringPiece(it->name, it->name_len),
129 StringPiece(it->value, it->value_len),
130 true, // is_static
131 total_insertions_));
132 CHECK(index_.insert(&static_entries_.back()).second);
133
134 ++total_insertions_;
135 }
136 }
137 42
138 HpackHeaderTable::~HpackHeaderTable() {} 43 HpackHeaderTable::~HpackHeaderTable() {}
139 44
140 HpackEntry* HpackHeaderTable::GetByIndex(size_t index) { 45 const HpackEntry* HpackHeaderTable::GetByIndex(size_t index) {
141 if (index == 0) { 46 if (index == 0) {
142 return NULL; 47 return NULL;
143 } 48 }
144 index -= 1; 49 index -= 1;
145 if (index < static_entries_.size()) { 50 if (index < static_entries_.size()) {
146 return &static_entries_[index]; 51 return &static_entries_[index];
147 } 52 }
148 index -= static_entries_.size(); 53 index -= static_entries_.size();
149 if (index < dynamic_entries_.size()) { 54 if (index < dynamic_entries_.size()) {
150 return &dynamic_entries_[index]; 55 return &dynamic_entries_[index];
151 } 56 }
152 return NULL; 57 return NULL;
153 } 58 }
154 59
155 HpackEntry* HpackHeaderTable::GetByName(StringPiece name) { 60 const HpackEntry* HpackHeaderTable::GetByName(StringPiece name) {
156 HpackEntry query(name, ""); 61 HpackEntry query(name, "");
157 OrderedEntrySet::const_iterator it = index_.lower_bound(&query); 62 {
158 if (it != index_.end() && (*it)->name() == name) { 63 OrderedEntrySet::const_iterator it = static_index_.lower_bound(&query);
159 return *it; 64 if (it != static_index_.end() && (*it)->name() == name) {
65 return *it;
66 }
67 }
68 {
69 OrderedEntrySet::const_iterator it = dynamic_index_.lower_bound(&query);
70 if (it != dynamic_index_.end() && (*it)->name() == name) {
71 return *it;
72 }
160 } 73 }
161 return NULL; 74 return NULL;
162 } 75 }
163 76
164 HpackEntry* HpackHeaderTable::GetByNameAndValue(StringPiece name, 77 const HpackEntry* HpackHeaderTable::GetByNameAndValue(StringPiece name,
165 StringPiece value) { 78 StringPiece value) {
166 HpackEntry query(name, value); 79 HpackEntry query(name, value);
167 OrderedEntrySet::const_iterator it = index_.lower_bound(&query); 80 {
168 if (it != index_.end() && (*it)->name() == name && (*it)->value() == value) { 81 OrderedEntrySet::const_iterator it = static_index_.lower_bound(&query);
169 return *it; 82 if (it != static_index_.end() &&
83 (*it)->name() == name &&
84 (*it)->value() == value) {
85 return *it;
86 }
87 }
88 {
89 OrderedEntrySet::const_iterator it = dynamic_index_.lower_bound(&query);
90 if (it != dynamic_index_.end() &&
91 (*it)->name() == name &&
92 (*it)->value() == value) {
93 return *it;
94 }
170 } 95 }
171 return NULL; 96 return NULL;
172 } 97 }
173 98
174 size_t HpackHeaderTable::IndexOf(const HpackEntry* entry) const { 99 size_t HpackHeaderTable::IndexOf(const HpackEntry* entry) const {
175 if (entry->IsLookup()) { 100 if (entry->IsLookup()) {
176 return 0; 101 return 0;
177 } else if (entry->IsStatic()) { 102 } else if (entry->IsStatic()) {
178 return 1 + entry->InsertionIndex(); 103 return 1 + entry->InsertionIndex();
179 } else { 104 } else {
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 } 152 }
228 return count; 153 return count;
229 } 154 }
230 155
231 void HpackHeaderTable::Evict(size_t count) { 156 void HpackHeaderTable::Evict(size_t count) {
232 for (size_t i = 0; i != count; ++i) { 157 for (size_t i = 0; i != count; ++i) {
233 CHECK(!dynamic_entries_.empty()); 158 CHECK(!dynamic_entries_.empty());
234 HpackEntry* entry = &dynamic_entries_.back(); 159 HpackEntry* entry = &dynamic_entries_.back();
235 160
236 size_ -= entry->Size(); 161 size_ -= entry->Size();
237 CHECK_EQ(1u, index_.erase(entry)); 162 CHECK_EQ(1u, dynamic_index_.erase(entry));
238 dynamic_entries_.pop_back(); 163 dynamic_entries_.pop_back();
239 } 164 }
240 } 165 }
241 166
242 HpackEntry* HpackHeaderTable::TryAddEntry(StringPiece name, StringPiece value) { 167 const HpackEntry* HpackHeaderTable::TryAddEntry(StringPiece name,
168 StringPiece value) {
243 Evict(EvictionCountForEntry(name, value)); 169 Evict(EvictionCountForEntry(name, value));
244 170
245 size_t entry_size = HpackEntry::Size(name, value); 171 size_t entry_size = HpackEntry::Size(name, value);
246 if (entry_size > (max_size_ - size_)) { 172 if (entry_size > (max_size_ - size_)) {
247 // Entire table has been emptied, but there's still insufficient room. 173 // Entire table has been emptied, but there's still insufficient room.
248 DCHECK(dynamic_entries_.empty()); 174 DCHECK(dynamic_entries_.empty());
249 DCHECK_EQ(0u, size_); 175 DCHECK_EQ(0u, size_);
250 return NULL; 176 return NULL;
251 } 177 }
252 dynamic_entries_.push_front(HpackEntry(name, 178 dynamic_entries_.push_front(HpackEntry(name,
253 value, 179 value,
254 false, // is_static 180 false, // is_static
255 total_insertions_)); 181 total_insertions_));
256 CHECK(index_.insert(&dynamic_entries_.front()).second); 182 CHECK(dynamic_index_.insert(&dynamic_entries_.front()).second);
257 183
258 size_ += entry_size; 184 size_ += entry_size;
259 ++total_insertions_; 185 ++total_insertions_;
260 186
261 return &dynamic_entries_.front(); 187 return &dynamic_entries_.front();
262 } 188 }
263 189
264 void HpackHeaderTable::DebugLogTableState() const { 190 void HpackHeaderTable::DebugLogTableState() const {
265 DVLOG(2) << "Dynamic table:"; 191 DVLOG(2) << "Dynamic table:";
266 for (EntryTable::const_iterator it = dynamic_entries_.begin(); 192 for (EntryTable::const_iterator it = dynamic_entries_.begin();
267 it != dynamic_entries_.end(); ++it) { 193 it != dynamic_entries_.end(); ++it) {
268 DVLOG(2) << " " << it->GetDebugString(); 194 DVLOG(2) << " " << it->GetDebugString();
269 } 195 }
270 DVLOG(2) << "Full Index:"; 196 DVLOG(2) << "Full Static Index:";
271 for (OrderedEntrySet::const_iterator it = index_.begin(); 197 for (OrderedEntrySet::const_iterator it = static_index_.begin();
272 it != index_.end(); ++it) { 198 it != static_index_.end(); ++it) {
199 DVLOG(2) << " " << (*it)->GetDebugString();
200 }
201 DVLOG(2) << "Full Dynamic Index:";
202 for (OrderedEntrySet::const_iterator it = dynamic_index_.begin();
203 it != dynamic_index_.end(); ++it) {
273 DVLOG(2) << " " << (*it)->GetDebugString(); 204 DVLOG(2) << " " << (*it)->GetDebugString();
274 } 205 }
275 } 206 }
276 207
277 } // namespace net 208 } // namespace net
OLDNEW
« no previous file with comments | « net/spdy/hpack_header_table.h ('k') | net/spdy/hpack_header_table_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698