Home

Awesome

fast_io

fast_io is a C++20 input/output library that provides exceptional speed and is designed to replace the commonly used <iostream> and <cstdio> libraries. It is a header-only library and is licensed under the MIT license, making it easy to include in any project. However, it requires a C++20 compiler that supports concepts.

The original repository for fast_io has been archived due to git bloat over time, but older commits can still be found there. old commits were here.

Examples

Print to stdout

#include <fast_io.h>

int main() {
	print("Hello, fast_io world!\n");
}

Memory map file

#include <fast_io.h>

int main() {
	fast_io::native_file_loader file_data("text.txt");
	// file_data satisfies std::ranges::contiguous_range
}

Other examples

For other up-to-date examples, please look in the examples/ folder.

Deprecated examples are here but they might not work anymore.

This i18n repo stores the i18n source files.

https://bitbucket.org/ejsvifq_mabmip/fast_io_i18n

F.A.Q.

What does "fast" mean in fast_io?

The term fast in fast_io does not necessarily imply that it is the fastest input/output library available (as it would have been named as fastest_io instead). Instead, the term refers to the fact that fast_io is notably faster than <iostream> and <cstdio> libraries in all cases.

What are the supported platforms, compilers...?

See support.md for a list of supported things and platform specific usage notes.

Other questions?

You can ask questions on the fast_io discord server or QQ group: 801441303.

Documentation

See either https://ewindy.gitee.io/fast_io_rst/index.html or https://gitee.com/qabeowjbtkwb/fast_io/wikis.

Features

Post C++20

  1. Module support
  2. Coroutine support for async IO
  3. Improve and refactor code once Zero-overhead deterministic exceptions are added to the standard

Benchmarks

  1. I/O 10M integers

Goal: Print out ten million integers from 0 to 10M to file. Then reopen that file to scan back.

All benchmarks are in benchmarks/0000.10m_size_t/unit.

Notice: I modified libstdc++'s BUFSIZ 1048576 due to BUFSIZ is too small (512 bytes) for MinGW-W64, or it performs horribly.

PlatformWindowsMinGW-W64 GCC 11.0.0MSVCRT + libstdc++
MethodOutput timeInput timeComment
stdio.h(fprintf/fscanf)2.412987s5.607791s
fstream0.462012s1.192s
fstream with rdbuf.sputc trick0.33895s1.170173s
fast_io::i/obuf_file0.04903s0.080996s
fast_io::i/obuf_file_mutex0.146064s0.113155sthread safe
c_locale_i/obuf_file ("C")0.065988s0.086012simbued with locale, locale "C"
c_locale_i/obuf_file local0.153995s:x:imbued with locale, locale ""
fmt::format_int+obuf_file0.122999s:x:
fmt::format_int+ofstream0.209055s:x:
fmt::format+ofstream0.548s:x:fmt makes things slower
fmt::print0.663996s:x:fmt makes things slower
std::to_chars+obuf_file0.12s:x:
std::to_chars+ofstream0.192s:x:
fast_io::c_file_unlocked0.098999s0.126003sI hacked MSVCRT's FILE* implementation
fast_io::c_file0.298988s0.318001sThread Safe. I hacked MSVCRT's FILE* implementation
fast_io::filebuf_file0.048999s0.081sI hacked libstdc++'s streambuf/filebuf implementation
fast_io::iobuf_utf8_file_char160.124s0.112001sUTF-16=>UTF-8 with SSE
fast_io::iobuf_utf8_file_char320.110999s0.111011sUTF-32=>UTF-8 with SSE
std::wofstream2.64s3.843735swofstream with std::locale codecvt. Extremely slow tbh.
fast_io::wfilebuf_io_observer2.415692s2.497704swofstream with std::locale codecvt. This proves fstream can never get fixed.
Rust language0.483s:x:RUST IS SLOW. Also Rust does not deal with locale. Think how bad it is.
Rust itoa library 0.4.6> 0.165s:x:I ignored the \n part for it to ensure no bias.

Rust language is 10x slower than fast_io. + binary bloat and itoa library is still extremely slow and usable for me. It is at least 3x slower than fast_io.

Run the same test on MSVC 19.26.28805.

PlatformWindowsMSVC 19.26.28805Install fmtlib wastes time of my life
MethodOutput timeInput timeComment
stdio.h(fprintf/fscanf)1.5353597s1.4157233s
fstream3.6350262s3.8420339s
fstream with rdbuf.sputc trick3.3735902s3.8145566s
fast_io::i/obuf_file0.0631433s0.1030554s
fast_io::i/obuf_file_mutex0.2190659s0.2485886sthread safe
std::to_chars+obuf_file0.1641641s:x:
std::to_chars+ofstream0.5461922s:x:
fast_io::c_file_unlocked0.1102575s0.2399757sI hacked Universal CRT's FILE* implementation
fast_io::c_file0.2034755s0.2621148sThread Safe. I hacked UCRT's FILE* implementation
fast_io::filebuf_file0.126661s0.2378803sI hacked MSVC STL's streambuf/filebuf implementation

Run the same test on GCC 11. glibc + libstdc++

PlatformLinuxGCC 11.0.0glibc + libstdc++
MethodOutput timeInput timeComment
stdio.h(fprintf/fscanf)0.532792935s0.591907111s
fstream with rdbuf.sputc trick0.318896068s0.429406415s
fast_io::i/obuf_file0.050300857s0.065372395s
fast_io::i/obuf_file_mutex0.05290654s0.083040518sthread safe
c_locale_i/obuf_file ("C")0.051939052s0.065820056simbued with locale, locale "C"
c_locale_i/obuf_file local0.162406082s:x:imbued with locale, locale ""
std::to_chars+obuf_file0.115453587s:x:
fmt::format_int+obuf_file0.1183587s:x:
fmt::format_int+ofstream0.195914384s:x:
fmt::format+ofstream0.633590975s:x:fmt makes things slower
fmt::print0.495270371s:x:fmt makes things slower
boost::iostreams0.400906063s0.444717051sUsing boost iostreams does not make your code faster
fast_io::c_file_unlocked0.060076723s0.073299716sI hacked glibc's FILE* implementation
fast_io::c_file0.092490191s0.104545535sThread Safe. I hacked glibc's FILE* implementation
fast_io::filebuf_file0.052251608s0.06655806sI hacked libstdc++'s streambuf/filebuf implementation

You can see fast_io can also boost the performance of existing facilities for 10x! Yes, it can even improve FILE* and fstream's performance for 10x depending on platforms since I use concepts to abstract them all. fmtlib actually slows down I/O performance.

  1. Output 10M double in round-trip mode with Ryu algorithm

We only perform this test for MSVC since only msvc's charconv implements it. Yes. fast_io defeats msvc's charconv for over 20% for running the same algorithm.

All benchmarks are in benchmarks/0001.10m_double/charconv.

Run the same test on MSVC 19.26.28805.

PlatformWindowsMSVC 19.26.28805
MethodOutput timeComment
i/obuf_file0.4653818s
charconv + obuf_file0.6011s
  1. Raw I/O Performance

All benchmarks are in benchmarks/0014.file_io/file_io.

Output 100000000x "Hello World\n"

Notice: I modified libstdc++'s std::filebuf's BUFSIZ to 1048576 due to BUFSIZ is too small (512 bytes) for MinGW-W64 or it performs horribly.

PlatformWindowsMinGW-W64 GCC 11.0.0MSVCRT + libstdc++
MethodOutput timeComment
fwrite2.524001s
fstream1.013001s
fast_io::obuf_file0.437998s
fast_io::obuf_file_mutex1.371sThread safe
fast_io::c_file_unlocked1.164997sI hacked MSVCRT's FILE* implementation
fast_io::c_file3.337945sThread Safe. I hacked MSVCRT's FILE* implementation. Need further optimization
fast_io::filebuf_file0.467001sI hacked libstdc++'s std::filebuf implementation
PlatformLinuxGCC 11.0.0glibc + libstdc++
MethodOutput timeComment
fwrite1.457288317s
fstream1.249783346s
fast_io::obuf_file0.494827134s
fast_io::obuf_file_mutex0.497138826sThread safe
fast_io::c_file_unlocked0.687976666sI hacked glibc's FILE* implementation
fast_io::c_file0.910792697sThread Safe. I hacked glibc's FILE* implementation
fast_io::filebuf_file0.526955039sI hacked libstdc++'s std::filebuf implementation
PlatformWindowsMSVC 19.26.28805UCRT + MSVC STL
MethodOutput timeComment
fwrite3.3139122s
fstream1.7184119s
fast_io::obuf_file0.7996034s
fast_io::obuf_file_mutex2.2949221sThread safe. It looks like std::mutex is horribly slow for MSVC STL.
fast_io::c_file_unlocked1.2103924sI hacked UCRT's FILE* implementation
fast_io::c_file2.3604295sThread Safe. I hacked UCRT's FILE* implementation
fast_io::filebuf_file1.2805368sI hacked MSVC STL's std::filebuf implementation
  1. Binary Size Just use the benchmark in benchmarks/0014.file_io/file_io. Dude, you should avoid stream as plague tbh. It is not healthy.
PlatformWindowsMinGW-W64 GCC 11.0.0MSVCRT + libstdc++ + static compile
MethodBinary SizeComment
fstream925KBUse fstream is not good for your health since std::locale bloats your binary.
fast_io::obuf_file155KB
fast_io::c_file_unlocked157KBI hacked MSVCRT's FILE* implementation
fast_io::c_file157KBThread Safe. I hacked MSVCRT's FILE* implementation
fast_io::filebuf_file933KBI hacked libstdc++'s std::filebuf implementation. C++ stream sucks
  1. Code Convert

Generate 100000000 🚄 emojis by using the program in benchmarks/0020.utf/fill_nc.cc

Benchmarks are in examples/0043.iconv Universal iconv. (UTF-8 to GB18030 as an example) iconv test:

PlatformWindowsMinGW-W64 GCC 11.0.0MSVCRT + libstdc++
MethodElapsed timeComment
iconv command1.529s
universal.cc1.293suse POSIX libiconv

UTF8->UTF16LE

Benchmarks are in examples/0022.utf

iconv test:

PlatformWindowsMinGW-W64 GCC 11.0.0MSVCRT + libstdc++
MethodElapsed timeComment
iconv command0.967sGNU iconv. No BOM which sucks
utf8_file_to_utf16_file.cc0.498sI use the SSE algorithms provided by the utf-utils project.

UTF8->UTF32LE

Benchmarks are in examples/0022.utf

iconv test:

PlatformWindowsMinGW-W64 GCC 11.0.0MSVCRT + libstdc++
MethodElapsed timeComment
iconv command0.844sGNU iconv. No BOM which sucks
utf8_file_to_utf32_file.cc0.442sI use the SSE algorithms provided by the utf-utils project.

Credits

The creation and development of this project were made possible thanks to the valuable contributions of various open-source projects. While the code was not copied directly from these projects, I used them as references and re-implemented them to suit the specific purposes of this library. In some cases, integration issues arose that required modifications to the original code. I am grateful to these projects and their developers for their commitment to making their code open and accessible to the wider community.

ProjectUrl
Grisu-Exacthttps://github.com/jk-jeon/Grisu-Exact
Ryuhttps://github.com/ulfjack/ryu
SHA-Intrinsicshttps://github.com/noloader/SHA-Intrinsics
SHA1https://github.com/vog/sha1
UTF-utilshttps://github.com/BobSteagall/utf_utils
jenkins-hash-javahttps://github.com/vkandy/jenkins-hash-java
md5https://github.com/JieweiWei/md5
ReactOShttps://github.com/reactos/reactos
dirent_hhttps://github.com/win32ports/dirent_h
GNU C libraryhttps://www.gnu.org/software/libc/
GNU Newlibhttps://sourceware.org/newlib/
Dragonboxhttps://github.com/jk-jeon/dragonbox
JEAIIIhttps://github.com/jeaiii/itoa
Crypto++https://github.com/weidai11/cryptopp
MyItoAhttps://gitee.com/xjkp2283572185/MyStd