Skip to content

Commit 9b114c5

Browse files
authored
[libc++] Optimize fstream::read (#165223)
``` Benchmark old new Difference % Difference ----------- -------- -------- ------------ -------------- bm_read 2468.45 736.27 -1732.18 -70.17% ```
1 parent 33e2151 commit 9b114c5

File tree

5 files changed

+37
-6
lines changed

5 files changed

+37
-6
lines changed

libcxx/docs/ReleaseNotes/22.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ Improvements and New Features
6666
by up to 2.5x
6767
- The performance of ``erase(iterator, iterator)`` in the unordered containers has been improved by up to 1.9x
6868
- The performance of ``map::insert_or_assign`` has been improved by up to 2x
69-
- ``ofstream::write`` has been optimized to pass through large strings to system calls directly instead of copying them
70-
in chunks into a buffer.
69+
- ``ofstream::write`` and ``ifstream::read`` have been optimized to pass through large reads and writes to system calls
70+
directly instead of copying them in chunks.
7171
- Multiple internal types have been refactored to use ``[[no_unique_address]]``, resulting in faster compile times and
7272
reduced debug information.
7373

libcxx/include/fstream

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,19 @@ protected:
308308
return basic_streambuf<_CharT, _Traits>::xsputn(__str, __len);
309309
}
310310

311+
_LIBCPP_HIDE_FROM_ABI_VIRTUAL streamsize xsgetn(char_type* __str, streamsize __len) override {
312+
if (__always_noconv_) {
313+
const streamsize __n = std::min(this->egptr() - this->gptr(), __len);
314+
if (__n != 0) {
315+
traits_type::copy(__str, this->gptr(), __n);
316+
this->__gbump_ptrdiff(__n);
317+
}
318+
if (__len - __n >= this->egptr() - this->eback())
319+
return std::fread(__str + __n, sizeof(char_type), __len - __n, __file_);
320+
}
321+
return basic_streambuf<_CharT, _Traits>::xsgetn(__str, __len);
322+
}
323+
311324
private:
312325
char* __extbuf_;
313326
const char* __extbufnext_;

libcxx/test/benchmarks/streams/ofstream.bench.cpp renamed to libcxx/test/benchmarks/streams/fstream.bench.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
#include <benchmark/benchmark.h>
1313

14-
static void bm_write(benchmark::State& state) {
14+
static void bm_ofstream_write(benchmark::State& state) {
1515
std::vector<char> buffer;
1616
buffer.resize(16384);
1717

@@ -20,6 +20,24 @@ static void bm_write(benchmark::State& state) {
2020
for (auto _ : state)
2121
stream.write(buffer.data(), buffer.size());
2222
}
23-
BENCHMARK(bm_write);
23+
BENCHMARK(bm_ofstream_write);
24+
25+
static void bm_ifstream_read(benchmark::State& state) {
26+
std::vector<char> buffer;
27+
buffer.resize(16384);
28+
29+
std::ofstream gen_testfile("testfile");
30+
gen_testfile.write(buffer.data(), buffer.size());
31+
32+
std::ifstream stream("testfile");
33+
assert(stream);
34+
35+
for (auto _ : state) {
36+
stream.read(buffer.data(), buffer.size());
37+
benchmark::DoNotOptimize(buffer);
38+
stream.seekg(0);
39+
}
40+
}
41+
BENCHMARK(bm_ifstream_read);
2442

2543
BENCHMARK_MAIN();

libcxx/test/libcxx/input.output/file.streams/fstreams/filebuf/traits_mismatch.verify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919

2020
std::basic_filebuf<char, std::char_traits<wchar_t> > f;
2121
// expected-error-re@*:* {{static assertion failed{{.*}}traits_type::char_type must be the same type as CharT}}
22-
// expected-error@*:* 10 {{only virtual member functions can be marked 'override'}}
22+
// expected-error@*:* 11 {{only virtual member functions can be marked 'override'}}

libcxx/test/libcxx/input.output/file.streams/fstreams/traits_mismatch.verify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ std::basic_fstream<char, std::char_traits<wchar_t> > f;
2121
// expected-error-re@*:* {{static assertion failed{{.*}}traits_type::char_type must be the same type as CharT}}
2222
// expected-error-re@*:* {{static assertion failed{{.*}}traits_type::char_type must be the same type as CharT}}
2323

24-
// expected-error@*:* 12 {{only virtual member functions can be marked 'override'}}
24+
// expected-error@*:* 13 {{only virtual member functions can be marked 'override'}}
2525

2626
// FIXME: As of commit r324062 Clang incorrectly generates a diagnostic about mismatching
2727
// exception specifications for types which are already invalid for one reason or another.

0 commit comments

Comments
 (0)