mirror of
https://github.com/yse/easy_profiler.git
synced 2025-01-14 08:37:55 +08:00
Added documentation and potentially sped things up a bit. I also commented a potential bug.
This commit is contained in:
parent
60c9f76478
commit
a9591f3070
@ -223,13 +223,13 @@ public:
|
|||||||
|
|
||||||
if (!need_expand(n))
|
if (!need_expand(n))
|
||||||
{
|
{
|
||||||
int8_t* data = m_chunks.back().data + m_shift;
|
int8_t* data = &m_chunks.back().data[0] + m_shift;
|
||||||
m_shift += n + sizeof(uint16_t);
|
m_shift += n + sizeof(uint16_t);
|
||||||
|
|
||||||
*(uint16_t*)data = n;
|
*(uint16_t*)data = n;
|
||||||
data = data + sizeof(uint16_t);
|
data += sizeof(uint16_t);
|
||||||
|
|
||||||
if (m_shift + 1 < N)
|
if (m_shift < N-1)
|
||||||
*(uint16_t*)(data + n) = 0;
|
*(uint16_t*)(data + n) = 0;
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
@ -237,10 +237,10 @@ public:
|
|||||||
|
|
||||||
m_shift = n + sizeof(uint16_t);
|
m_shift = n + sizeof(uint16_t);
|
||||||
m_chunks.emplace_back();
|
m_chunks.emplace_back();
|
||||||
auto data = m_chunks.back().data;
|
int8_t* data = m_chunks.back().data;
|
||||||
|
|
||||||
*(uint16_t*)data = n;
|
*(uint16_t*)data = n;
|
||||||
data = data + sizeof(uint16_t);
|
data += sizeof(uint16_t);
|
||||||
|
|
||||||
*(uint16_t*)(data + n) = 0;
|
*(uint16_t*)(data + n) = 0;
|
||||||
return data;
|
return data;
|
||||||
@ -278,22 +278,36 @@ public:
|
|||||||
void serialize(profiler::OStream& _outputStream)
|
void serialize(profiler::OStream& _outputStream)
|
||||||
{
|
{
|
||||||
// Chunks are stored in reversed order (stack).
|
// Chunks are stored in reversed order (stack).
|
||||||
// To be able to iterate them in direct order we have to invert chunks list.
|
// To be able to iterate them in direct order we have to invert the chunks list.
|
||||||
m_chunks.invert();
|
m_chunks.invert();
|
||||||
|
|
||||||
// Iterate over chunks and perform blocks serialization
|
// Each chunk is an array of N bytes that can hold between
|
||||||
auto current = m_chunks.last;
|
// 1(if the list isn't empty) and however many elements can fit in a chunk,
|
||||||
do {
|
// where an element consists of a payload size + a payload as follows:
|
||||||
|
// data[0..1]: size as a uint16_t
|
||||||
|
// data[2..?<(N-sizeof(uint16_t)-1): payload.
|
||||||
|
|
||||||
|
// Note: if elements don't completely fill a chunk:
|
||||||
|
// 1. If there is space for a uint16_t after the last element,
|
||||||
|
// a uint16_t of 0 is placed after it.
|
||||||
|
// 2. If there ISN'T space for a uint16_t after the last element,
|
||||||
|
// the last bytes contents are undefined.
|
||||||
|
|
||||||
|
chunk* current = m_chunks.last;
|
||||||
|
while (current != nullptr) {
|
||||||
const int8_t* data = current->data;
|
const int8_t* data = current->data;
|
||||||
uint16_t i = 0;
|
int_fast32_t chunkOffset = 0;
|
||||||
while (i + 1 < N && *(uint16_t*)data != 0) {
|
uint16_t payloadSize = *(uint16_t*)data;
|
||||||
const uint16_t size = sizeof(uint16_t) + *(uint16_t*)data;
|
// @Incomplete: doesn't handle the case where an element is one off from N-1!
|
||||||
_outputStream.write((const char*)data, size);
|
while (chunkOffset < N-1 && payloadSize != 0) {
|
||||||
data = data + size;
|
const uint16_t chunkSize = sizeof(uint16_t) + payloadSize;
|
||||||
i += size;
|
_outputStream.write((const char*)data, chunkSize);
|
||||||
|
chunkOffset += (int16_t)chunkSize;
|
||||||
|
data += chunkSize;
|
||||||
|
payloadSize = *(uint16_t*)data;
|
||||||
}
|
}
|
||||||
current = current->prev;
|
current = current->prev;
|
||||||
} while (current != nullptr);
|
}
|
||||||
|
|
||||||
clear();
|
clear();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user