mmap的优缺点:

引用:http://lkml.indiana.edu/hypermail/linux/kernel/0004.0/0728.html


People love mmap() and other ways to play with the page tables to 
optimize away a copy operation, and sometimes it is worth it. 

HOWEVER, playing games with the virtual memory mapping is very expensive 
in itself. It has a number of quite real disadvantages that people tend 
to ignore because memory copying is seen as something very slow, and 
sometimes optimizing that copy away is seen as an obvious improvment. 

Downsides to mmap: 
 - quite noticeable setup and teardown costs. And I mean _noticeable_. 
   It's things like following the page tables to unmap everything 
   cleanly. It's the book-keeping for maintaining a list of all the 
   mappings. It's The TLB flush needed after unmapping stuff. 
 - page faulting is expensive. That's how the mapping gets populated, 
   and it's quite slow. 

Upsides of mmap: 
 - if the data gets re-used over and over again (within a single map 
   operation), or if you can avoid a lot of other logic by just mapping 
   something in, mmap() is just the greatest thing since sliced bread. 

   This may be a file that you go over many times (the binary image of 
   an executable is the obvious case here - the code jumps all around 
   the place), or a setup where it's just so convenient to map the whole 
   thing in without regard of the actual usage patterns that mmap() just 
   wins. You may have random access patterns, and use mmap() as a way 
   of keeping track of what data you actually needed. 

 - if the data is large, mmap() is a great way to let the system know 
   what it can do with the data-set. The kernel can forget pages as 
   memory pressure forces the system to page stuff out, and then just 
   automatically re-fetch them again. 

   And the automatic sharing is obviously a case of this.. 

But your test-suite (just copying the data once) is probably pessimal 
for mmap(). 

                Linus 


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值