OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/tracing/core/scattered_stream_writer.h" | |
6 | |
7 #include <string.h> | |
8 | |
9 #include "base/logging.h" | |
10 | |
11 namespace tracing { | |
12 namespace v2 { | |
13 | |
14 ScatteredStreamWriter::ScatteredStreamWriter(Delegate* delegate) | |
15 : delegate_(delegate), | |
16 cur_range_({nullptr, nullptr}), | |
17 write_ptr_(nullptr) {} | |
18 | |
19 ScatteredStreamWriter::~ScatteredStreamWriter() {} | |
20 | |
21 void ScatteredStreamWriter::Reset(ContiguousMemoryRange range) { | |
22 cur_range_ = range; | |
23 write_ptr_ = range.begin; | |
24 DCHECK_LT(write_ptr_, cur_range_.end); | |
25 } | |
26 | |
27 void ScatteredStreamWriter::Extend() { | |
28 Reset(delegate_->GetNewBuffer()); | |
29 } | |
30 | |
31 void ScatteredStreamWriter::WriteByte(uint8_t value) { | |
32 if (write_ptr_ >= cur_range_.end) | |
petrcermak
2016/06/29 10:27:48
You do this in 2 places. Assuming the compiler inl
Primiano Tucci (use gerrit)
2016/06/30 12:17:34
I thought to that. Is not about perf and inlining.
| |
33 Extend(); | |
34 *write_ptr_++ = value; | |
35 } | |
36 | |
37 void ScatteredStreamWriter::WriteBytes(const uint8_t* src, size_t size) { | |
38 uint8_t* const end = write_ptr_ + size; | |
39 if (end <= cur_range_.end) { | |
petrcermak
2016/06/29 10:27:48
You could make this method simpler and avoid recur
Primiano Tucci (use gerrit)
2016/06/30 12:17:34
I know, but that would have 3 if-s in the fastpath
| |
40 // Fast-path, the buffer fits into the current contiguous range. | |
41 // TODO(primiano): perf optimization, this is a tracing hot path. The | |
42 // compiler can make strong optimization on memcpy if the size arg is a | |
43 // constexpr. Make a templated variant of this for fixed-size writes. | |
44 memcpy(write_ptr_, src, size); | |
45 write_ptr_ = end; | |
46 return; | |
petrcermak
2016/06/29 10:27:48
you use both return and else. only one is necessar
Primiano Tucci (use gerrit)
2016/06/30 12:17:34
right. done.
| |
47 } else { | |
48 // Slow path, scatter the writes. | |
49 size_t bytes_left = size; | |
50 while (bytes_left > 0) { | |
51 if (write_ptr_ >= cur_range_.end) | |
52 Extend(); | |
53 const size_t burst_size = std::min(bytes_available(), bytes_left); | |
54 WriteBytes(src, burst_size); | |
55 bytes_left -= burst_size; | |
56 src += burst_size; | |
57 } | |
58 } | |
59 } | |
60 | |
61 // TODO(primiano): perf optimization: I suspect that at the end this will always | |
62 // be called with |size| == 4, in which case we might just hardcode it. | |
63 ContiguousMemoryRange ScatteredStreamWriter::ReserveBytes(size_t size) { | |
64 if (write_ptr_ + size > cur_range_.end) | |
65 Extend(); | |
66 uint8_t* begin = write_ptr_; | |
67 write_ptr_ += size; | |
68 #ifndef NDEBUG | |
69 memset(begin, '\xFF', size); | |
70 #endif | |
71 return {begin, begin + size}; | |
petrcermak
2016/06/29 10:27:48
You need to check that |size| is not greater than
Primiano Tucci (use gerrit)
2016/06/30 12:17:34
Correct, that was just an assumption, perhaps too
| |
72 } | |
73 | |
74 } // namespace v2 | |
75 } // namespace tracing | |
OLD | NEW |