Skip to content

Commit

Permalink
[libc++] Optimize ofstream::write
Browse files Browse the repository at this point in the history
  • Loading branch information
philnik777 committed Jan 17, 2025
1 parent e79bb87 commit 502f8f2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions libcxx/docs/ReleaseNotes/20.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ Improvements and New Features
std::errc::not_a_directory``, or use ``err.default_error_condition()`` to map to an ``error_condition``, and then test
its ``value()`` and ``category()``.

- ``ofstream::write`` has been optimized to pass through large strings to system calls directly instead of copying them
in chunks into a buffer.

Deprecations and Removals
-------------------------

Expand Down
12 changes: 12 additions & 0 deletions libcxx/include/fstream
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ _LIBCPP_EXPORTED_FROM_ABI void* __filebuf_windows_native_handle(FILE* __file) no

template <class _CharT, class _Traits>
class _LIBCPP_TEMPLATE_VIS basic_filebuf : public basic_streambuf<_CharT, _Traits> {
using __base = basic_streambuf<_CharT, _Traits>;

public:
typedef _CharT char_type;
typedef _Traits traits_type;
Expand Down Expand Up @@ -304,6 +306,16 @@ protected:
int sync() override;
void imbue(const locale& __loc) override;

_LIBCPP_HIDE_FROM_ABI streamsize xsputn(const char_type* __str, streamsize __len) override {
if (__always_noconv_ && __len >= (this->epptr() - this->pbase())) {
if (traits_type::eq_int_type(overflow(), traits_type::eof()))
return 0;

return std::fwrite(__str, sizeof(char_type), __len, __file_);
}
return __base::xsputn(__str, __len);
}

private:
char* __extbuf_;
const char* __extbufnext_;
Expand Down
25 changes: 25 additions & 0 deletions libcxx/test/benchmarks/streams/ofstream.bench.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include <fstream>
#include <vector>

#include <benchmark/benchmark.h>

static void bm_write(benchmark::State& state) {
std::vector<char> buffer;
buffer.resize(16384);

std::ofstream stream("/dev/null");

for (auto _ : state)
stream.write(buffer.data(), buffer.size());
}
BENCHMARK(bm_write);

BENCHMARK_MAIN();

0 comments on commit 502f8f2

Please sign in to comment.