什么是为“写”功能适当的缓冲区大小? [英] what's the proper buffer size for 'write' function?

查看:150
本文介绍了什么是为“写”功能适当的缓冲区大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是低级别的I / O功能的写写一些数据到磁盘在我的code(Linux下C语言)。首先,我积累的数据在内存缓冲区,然后我用写当缓冲区已满,将数据写入到磁盘。那么,什么是对'写'最好的缓冲区大小?根据我的测试中,它是不是越快越大,​​所以我在这里寻找答案。

I am using the low-level I/O function 'write' to write some data to disk in my code (C language on Linux). First, I accumulate the data in a memory buffer, and then I use 'write' to write the data to disk when the buffer is full. So what's the best buffer size for 'write'? According to my tests it isn't the bigger the faster, so I am here to look for the answer.

推荐答案

有可能是在做这是文件系统的块大小的倍数,尤其是如果你正在更新的地方在文件中写入一些优势。如果你写不到的部分块的文件时,操作系统必须读取旧块,结合新的内容,然后将它写出来的。这个,如果你走笔小块序列,因为更新将在将在稍后刷新的内存缓冲区来完成并不一定发生。不过,过一段时间你可能会引发一些效率低下,如果你不填充块(和正确对齐之一:在偏移是块大小的倍数块大小的倍数),每个写入操作。

There is probably some advantage in doing writes which are multiples of the filesystem block size, especially if you are updating a file in place. If you write less than a partial block to a file, the OS has to read the old block, combine in the new contents and then write it out. This doesn't necessarily happen if you rapidly write small pieces in sequence because the updates will be done on buffers in memory which are flushed later. Still, once in a while you could be triggering some inefficiency if you are not filling a block (and a properly aligned one: multiple of block size at an offset which is a multiple of the block size) with each write operation.

传输大小此问题并不一定与MMAP消失。如果你映射一个文件,然后的memcpy 一些数据的地图,你正在一个页面脏。该网页已经在一段时间后被刷新:它是不确定的时候。如果你再拍的memcpy 这倒是同一个页面,该页面可能现在是干净的,你正在做再次弄脏。因此,它被写入两次。页面大小的倍数页对齐的副本将要走的路。

This issue of transfer size does not necessarily go away with mmap. If you map a file, and then memcpy some data into the map, you are making a page dirty. That page has to be flushed at some later time: it is indeterminate when. If you make another memcpy which touches the same page, that page could be clean now and you're making it dirty again. So it gets written twice. Page-aligned copies of multiples-of a page size will be the way to go.

这篇关于什么是为“写”功能适当的缓冲区大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆