creport: optimize ScopedFile performance

This commit is contained in:
Michael Scire
2024-09-23 02:41:57 -07:00
parent 027e209073
commit 009f581721
4 changed files with 56 additions and 11 deletions

View File

@@ -60,22 +60,24 @@ namespace ams::creport {
/* Print the line prefix. */
if (first) {
this->WriteFormat("%s", prefix);
this->Write(prefix, prefix_len);
first = false;
} else {
this->WriteFormat("%*s", prefix_len, "");
std::memset(g_format_buffer, ' ', prefix_len);
this->Write(g_format_buffer, prefix_len);
}
/* Dump up to 0x20 of hex memory. */
{
char hex[MaximumLineLength * 2 + 2] = {};
for (size_t i = 0; i < cur_size; i++) {
util::SNPrintf(hex + i * 2, 3, "%02X", data_u8[data_ofs++]);
hex[i * 2 + 0] = "0123456789ABCDEF"[data_u8[data_ofs] >> 4];
hex[i * 2 + 1] = "0123456789ABCDEF"[data_u8[data_ofs] & 0xF];
++data_ofs;
}
hex[cur_size * 2 + 0] = '\n';
hex[cur_size * 2 + 1] = '\x00';
this->WriteString(hex);
this->Write(hex, cur_size * 2 + 1);
}
/* Continue. */
@@ -83,16 +85,40 @@ namespace ams::creport {
}
}
void ScopedFile::Write(const void *data, size_t size) {
/* If we're not open, we can't write. */
if (!this->IsOpen()) {
return;
}
/* Advance, if we write successfully. */
if (R_SUCCEEDED(fs::WriteFile(m_file, m_offset, data, size, fs::WriteOption::Flush))) {
m_offset += size;
/* If we have a cache, write to it. */
if (m_cache != nullptr) {
/* Write into the cache, if we can. */
if (m_cache_size - m_cache_offset >= size || R_SUCCEEDED(this->TryWriteCache())) {
std::memcpy(m_cache + m_cache_offset, data, size);
m_cache_offset += size;
}
} else {
/* Advance, if we write successfully. */
if (R_SUCCEEDED(fs::WriteFile(m_file, m_offset, data, size, fs::WriteOption::None))) {
m_offset += size;
}
}
}
Result ScopedFile::TryWriteCache() {
/* If there's no cached data, there's nothing to do. */
R_SUCCEED_IF(m_cache_offset == 0);
/* Try to write any cached data. */
R_TRY(fs::WriteFile(m_file, m_offset, m_cache, m_cache_offset, fs::WriteOption::None));
/* Update our extents. */
m_offset += m_cache_offset;
m_cache_offset = 0;
R_SUCCEED();
}
}