mirror of
https://github.com/yse/easy_profiler.git
synced 2024-12-28 17:28:14 +08:00
Removed redundant copying of data from std::stringstream to std::ofstream in dumpBlocksToFile()
This commit is contained in:
parent
c0a23866ed
commit
105d8334e1
@ -60,6 +60,11 @@ namespace profiler {
|
|||||||
m_stream.write((const char*)&_data, sizeof(T));
|
m_stream.write((const char*)&_data, sizeof(T));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
::std::stringstream& stream()
|
||||||
|
{
|
||||||
|
return m_stream;
|
||||||
|
}
|
||||||
|
|
||||||
const ::std::stringstream& stream() const
|
const ::std::stringstream& stream() const
|
||||||
{
|
{
|
||||||
return m_stream;
|
return m_stream;
|
||||||
|
@ -1009,11 +1009,22 @@ uint32_t ProfileManager::dumpBlocksToStream(profiler::OStream& _outputStream, bo
|
|||||||
|
|
||||||
uint32_t ProfileManager::dumpBlocksToFile(const char* _filename)
|
uint32_t ProfileManager::dumpBlocksToFile(const char* _filename)
|
||||||
{
|
{
|
||||||
|
std::ofstream outputFile(_filename, std::fstream::binary);
|
||||||
|
if (!outputFile.is_open())
|
||||||
|
return 0;
|
||||||
|
|
||||||
profiler::OStream outputStream;
|
profiler::OStream outputStream;
|
||||||
|
|
||||||
|
// Replace outputStream buffer to outputFile buffer to avoid redundant copying
|
||||||
|
typedef ::std::basic_iostream<std::stringstream::char_type, std::stringstream::traits_type> stringstream_parent;
|
||||||
|
stringstream_parent& s = outputStream.stream();
|
||||||
|
auto oldbuf = s.rdbuf(outputFile.rdbuf());
|
||||||
|
|
||||||
|
// Write data directly to file
|
||||||
const auto blocksNumber = dumpBlocksToStream(outputStream, true);
|
const auto blocksNumber = dumpBlocksToStream(outputStream, true);
|
||||||
|
|
||||||
std::ofstream of(_filename, std::fstream::binary);
|
// Restore old outputStream buffer to avoid possible second memory free on stringstream destructor
|
||||||
of << outputStream.stream().str();
|
s.rdbuf(oldbuf);
|
||||||
|
|
||||||
return blocksNumber;
|
return blocksNumber;
|
||||||
}
|
}
|
||||||
|
@ -372,12 +372,16 @@ extern "C" {
|
|||||||
|
|
||||||
::std::stringstream str;
|
::std::stringstream str;
|
||||||
|
|
||||||
|
// Replace str buffer to inFile buffer to avoid redundant copying
|
||||||
typedef ::std::basic_iostream<::std::stringstream::char_type, ::std::stringstream::traits_type> stringstream_parent;
|
typedef ::std::basic_iostream<::std::stringstream::char_type, ::std::stringstream::traits_type> stringstream_parent;
|
||||||
stringstream_parent& s = str;
|
stringstream_parent& s = str;
|
||||||
auto oldbuf = s.rdbuf(inFile.rdbuf());
|
auto oldbuf = s.rdbuf(inFile.rdbuf());
|
||||||
|
|
||||||
|
// Read data from file
|
||||||
auto result = fillTreesFromStream(progress, str, serialized_blocks, serialized_descriptors, descriptors, blocks,
|
auto result = fillTreesFromStream(progress, str, serialized_blocks, serialized_descriptors, descriptors, blocks,
|
||||||
threaded_trees, total_descriptors_number, gather_statistics, _log);
|
threaded_trees, total_descriptors_number, gather_statistics, _log);
|
||||||
|
|
||||||
|
// Restore old str buffer to avoid possible second memory free on stringstream destructor
|
||||||
s.rdbuf(oldbuf);
|
s.rdbuf(oldbuf);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user