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

Side by Side Diff: base/pickle.cc

Issue 34413002: Pickle::Write* micro-optimizations (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 7 years, 2 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 | Annotate | Revision Log
« base/pickle.h ('K') | « base/pickle.h ('k') | 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 (c) 2012 The Chromium Authors. All rights reserved. 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 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 "base/pickle.h" 5 #include "base/pickle.h"
6 6
7 #include <stdlib.h> 7 #include <stdlib.h>
8 8
9 #include <algorithm> // for max() 9 #include <algorithm> // for max()
10 10
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 return false; 146 return false;
147 *data = read_from; 147 *data = read_from;
148 return true; 148 return true;
149 } 149 }
150 150
151 // Payload is uint32 aligned. 151 // Payload is uint32 aligned.
152 152
153 Pickle::Pickle() 153 Pickle::Pickle()
154 : header_(NULL), 154 : header_(NULL),
155 header_size_(sizeof(Header)), 155 header_size_(sizeof(Header)),
156 capacity_(0) { 156 capacity_(0),
157 write_offset_(0) {
157 Resize(kPayloadUnit); 158 Resize(kPayloadUnit);
158 header_->payload_size = 0; 159 header_->payload_size = 0;
159 } 160 }
160 161
161 Pickle::Pickle(int header_size) 162 Pickle::Pickle(int header_size)
162 : header_(NULL), 163 : header_(NULL),
163 header_size_(AlignInt(header_size, sizeof(uint32))), 164 header_size_(AlignInt(header_size, sizeof(uint32))),
164 capacity_(0) { 165 capacity_(0),
166 write_offset_(0) {
165 DCHECK_GE(static_cast<size_t>(header_size), sizeof(Header)); 167 DCHECK_GE(static_cast<size_t>(header_size), sizeof(Header));
166 DCHECK_LE(header_size, kPayloadUnit); 168 DCHECK_LE(header_size, kPayloadUnit);
167 Resize(kPayloadUnit); 169 Resize(kPayloadUnit);
168 header_->payload_size = 0; 170 header_->payload_size = 0;
169 } 171 }
170 172
171 Pickle::Pickle(const char* data, int data_len) 173 Pickle::Pickle(const char* data, int data_len)
172 : header_(reinterpret_cast<Header*>(const_cast<char*>(data))), 174 : header_(reinterpret_cast<Header*>(const_cast<char*>(data))),
173 header_size_(0), 175 header_size_(0),
174 capacity_(kCapacityReadOnly) { 176 capacity_(kCapacityReadOnly),
177 write_offset_(0) {
175 if (data_len >= static_cast<int>(sizeof(Header))) 178 if (data_len >= static_cast<int>(sizeof(Header)))
176 header_size_ = data_len - header_->payload_size; 179 header_size_ = data_len - header_->payload_size;
177 180
178 if (header_size_ > static_cast<unsigned int>(data_len)) 181 if (header_size_ > static_cast<unsigned int>(data_len))
179 header_size_ = 0; 182 header_size_ = 0;
180 183
181 if (header_size_ != AlignInt(header_size_, sizeof(uint32))) 184 if (header_size_ != AlignInt(header_size_, sizeof(uint32)))
182 header_size_ = 0; 185 header_size_ = 0;
183 186
184 // If there is anything wrong with the data, we're not going to use it. 187 // If there is anything wrong with the data, we're not going to use it.
185 if (!header_size_) 188 if (!header_size_)
186 header_ = NULL; 189 header_ = NULL;
187 } 190 }
188 191
189 Pickle::Pickle(const Pickle& other) 192 Pickle::Pickle(const Pickle& other)
190 : header_(NULL), 193 : header_(NULL),
191 header_size_(other.header_size_), 194 header_size_(other.header_size_),
192 capacity_(0) { 195 capacity_(0),
193 size_t payload_size = header_size_ + other.header_->payload_size; 196 write_offset_(other.write_offset_) {
194 Resize(payload_size); 197 Resize(other.header_->payload_size);
195 memcpy(header_, other.header_, payload_size); 198 memcpy(header_, other.header_, header_size_ + other.header_->payload_size);
196 } 199 }
197 200
198 Pickle::~Pickle() { 201 Pickle::~Pickle() {
199 if (capacity_ != kCapacityReadOnly) 202 if (capacity_ != kCapacityReadOnly)
200 free(header_); 203 free(header_);
201 } 204 }
202 205
203 Pickle& Pickle::operator=(const Pickle& other) { 206 Pickle& Pickle::operator=(const Pickle& other) {
204 if (this == &other) { 207 if (this == &other) {
205 NOTREACHED(); 208 NOTREACHED();
206 return *this; 209 return *this;
207 } 210 }
208 if (capacity_ == kCapacityReadOnly) { 211 if (capacity_ == kCapacityReadOnly) {
209 header_ = NULL; 212 header_ = NULL;
210 capacity_ = 0; 213 capacity_ = 0;
211 } 214 }
212 if (header_size_ != other.header_size_) { 215 if (header_size_ != other.header_size_) {
213 free(header_); 216 free(header_);
214 header_ = NULL; 217 header_ = NULL;
215 header_size_ = other.header_size_; 218 header_size_ = other.header_size_;
216 } 219 }
217 Resize(other.header_size_ + other.header_->payload_size); 220 Resize(other.header_->payload_size);
218 memcpy(header_, other.header_, 221 memcpy(header_, other.header_,
219 other.header_size_ + other.header_->payload_size); 222 other.header_size_ + other.header_->payload_size);
223 write_offset_ = other.write_offset_;
220 return *this; 224 return *this;
221 } 225 }
222 226
223 bool Pickle::WriteString(const std::string& value) { 227 bool Pickle::WriteString(const std::string& value) {
224 if (!WriteInt(static_cast<int>(value.size()))) 228 if (!WriteInt(static_cast<int>(value.size())))
225 return false; 229 return false;
226 230
227 return WriteBytes(value.data(), static_cast<int>(value.size())); 231 return WriteBytes(value.data(), static_cast<int>(value.size()));
228 } 232 }
229 233
(...skipping 10 matching lines...) Expand all
240 return false; 244 return false;
241 245
242 return WriteBytes(value.data(), 246 return WriteBytes(value.data(),
243 static_cast<int>(value.size()) * sizeof(char16)); 247 static_cast<int>(value.size()) * sizeof(char16));
244 } 248 }
245 249
246 bool Pickle::WriteData(const char* data, int length) { 250 bool Pickle::WriteData(const char* data, int length) {
247 return length >= 0 && WriteInt(length) && WriteBytes(data, length); 251 return length >= 0 && WriteInt(length) && WriteBytes(data, length);
248 } 252 }
249 253
250 bool Pickle::WriteBytes(const void* data, int data_len) { 254 bool Pickle::WriteBytes(const void* data, int length) {
251 DCHECK_NE(kCapacityReadOnly, capacity_) << "oops: pickle is readonly"; 255 DCHECK_NE(kCapacityReadOnly, capacity_) << "oops: pickle is readonly";
256 #ifdef ARCH_CPU_64_BITS
257 DCHECK_LE(static_cast<size_t>(length), kuint32max);
258 #endif
259 size_t data_len = AlignInt(length, sizeof(uint32));
260 size_t new_size = write_offset_ + data_len;
261 if (new_size > capacity_) {
262 Resize(std::max(capacity_ * 2, new_size));
263 }
252 264
253 char* dest = BeginWrite(data_len); 265 char* write = mutable_payload() + write_offset_;
254 if (!dest) 266 memcpy(write, data, length);
255 return false; 267 memset(write + length, 0, data_len - length);
256 268 header_->payload_size = write_offset_ + length;
257 memcpy(dest, data, data_len); 269 write_offset_ = new_size;
258
259 EndWrite(dest, data_len);
260 return true; 270 return true;
261 } 271 }
262 272
263 void Pickle::Reserve(size_t length) { 273 template <size_t length> void Pickle::WriteBytesStatic(const void* data) {
piman 2013/10/24 06:29:24 BTW, I looked at inlining this one, it gives us a
264 // write at a uint32-aligned offset from the beginning of the header 274 DCHECK_NE(kCapacityReadOnly, capacity_) << "oops: pickle is readonly";
265 size_t offset = AlignInt(header_->payload_size, sizeof(uint32));
266
267 size_t new_size = offset + length;
268 size_t needed_size = header_size_ + new_size;
269 if (needed_size > capacity_)
270 Resize(capacity_ * 2 + needed_size);
271 }
272
273 char* Pickle::BeginWrite(size_t length) {
274 // write at a uint32-aligned offset from the beginning of the header
275 size_t offset = AlignInt(header_->payload_size, sizeof(uint32));
276
277 size_t new_size = offset + length;
278 size_t needed_size = header_size_ + new_size;
279 if (needed_size > capacity_)
280 Resize(std::max(capacity_ * 2, needed_size));
281
282 #ifdef ARCH_CPU_64_BITS 275 #ifdef ARCH_CPU_64_BITS
283 DCHECK_LE(length, kuint32max); 276 DCHECK_LE(length, kuint32max);
284 #endif 277 #endif
278 size_t data_len = AlignInt(length, sizeof(uint32));
279 size_t new_size = write_offset_ + data_len;
280 if (new_size > capacity_) {
281 Resize(std::max(capacity_ * 2, new_size));
282 }
285 283
286 header_->payload_size = static_cast<uint32>(new_size); 284 char* write = mutable_payload() + write_offset_;
287 return mutable_payload() + offset; 285 memcpy(write, data, length);
286 memset(write + length, 0, data_len - length);
287 header_->payload_size = write_offset_ + length;
288 write_offset_ = new_size;
288 } 289 }
289 290
290 void Pickle::EndWrite(char* dest, int length) { 291 template void Pickle::WriteBytesStatic<2>(const void* data);
291 // Zero-pad to keep tools like valgrind from complaining about uninitialized 292 template void Pickle::WriteBytesStatic<4>(const void* data);
292 // memory. 293 template void Pickle::WriteBytesStatic<8>(const void* data);
293 if (length % sizeof(uint32)) 294
294 memset(dest + length, 0, sizeof(uint32) - (length % sizeof(uint32))); 295 void Pickle::Reserve(size_t length) {
296 size_t data_len = AlignInt(length, sizeof(uint32));
297 size_t new_size = write_offset_ + data_len;
298 if (new_size > capacity_)
299 Resize(capacity_ * 2 + new_size);
295 } 300 }
296 301
297 void Pickle::Resize(size_t new_capacity) { 302 void Pickle::Resize(size_t new_capacity) {
298 CHECK_NE(capacity_, kCapacityReadOnly); 303 CHECK_NE(capacity_, kCapacityReadOnly);
299 304
300 new_capacity = AlignInt(new_capacity, kPayloadUnit); 305 new_capacity = AlignInt(new_capacity, kPayloadUnit);
301 void* p = realloc(header_, new_capacity); 306 void* p = realloc(header_, header_size_ + new_capacity);
302 header_ = reinterpret_cast<Header*>(p); 307 header_ = reinterpret_cast<Header*>(p);
303 capacity_ = new_capacity; 308 capacity_ = new_capacity;
304 } 309 }
305 310
306 // static 311 // static
307 const char* Pickle::FindNext(size_t header_size, 312 const char* Pickle::FindNext(size_t header_size,
308 const char* start, 313 const char* start,
309 const char* end) { 314 const char* end) {
310 DCHECK_EQ(header_size, AlignInt(header_size, sizeof(uint32))); 315 DCHECK_EQ(header_size, AlignInt(header_size, sizeof(uint32)));
311 DCHECK_LE(header_size, static_cast<size_t>(kPayloadUnit)); 316 DCHECK_LE(header_size, static_cast<size_t>(kPayloadUnit));
312 317
313 if (static_cast<size_t>(end - start) < sizeof(Header)) 318 if (static_cast<size_t>(end - start) < sizeof(Header))
314 return NULL; 319 return NULL;
315 320
316 const Header* hdr = reinterpret_cast<const Header*>(start); 321 const Header* hdr = reinterpret_cast<const Header*>(start);
317 const char* payload_base = start + header_size; 322 const char* payload_base = start + header_size;
318 const char* payload_end = payload_base + hdr->payload_size; 323 const char* payload_end = payload_base + hdr->payload_size;
319 if (payload_end < payload_base) 324 if (payload_end < payload_base)
320 return NULL; 325 return NULL;
321 326
322 return (payload_end > end) ? NULL : payload_end; 327 return (payload_end > end) ? NULL : payload_end;
323 } 328 }
OLDNEW
« base/pickle.h ('K') | « base/pickle.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698