Optimal buffer size for write(2)

后端 未结 3 1185
不思量自难忘°
不思量自难忘° 2020-12-06 03:13

Let\'s say I want to write a 1 GB of data to the file on, say ext3 Linux filesystem using write(2) syscall and this happens in a very busy

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-06 04:10

    As discussed in comments, I believe the exact size don't matter that much, assuming it is :

    • a small multiple of the file system size (see comment by Joachim Pileborg suggesting stat(".") etc.)
    • a power of two (because computers and kernels like them)
    • not too big (e.g. fitting in some cache inside your processor, e.g. L2 cache)
    • aligned in memory (e.g. to a page size using posix_memalign).

    So a power of two between 16kbytes and a few megabytes should probably fit. Most of the time is spent on reading the disk. Filesystem and disk benchmarks are quite flat in that range.

    4Kbytes seems to often be the page size and the disk chunk size.

    Of course, you can tune things, even tune, when making the file system with mke2fs, the file system block size.

    And I'll bet that the optimal is really dependent upon your hardware (SSD, hard disks?) and your system (and its load).

提交回复
热议问题