What are the advantages of memory-mapped files?

前端 未结 4 1624
北恋
北恋 2020-11-27 10:45

I\'ve been researching memory mapped files for a project and would appreciate any thoughts from people who have either used them before, or decided against using them, and w

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-27 11:04

    I have used a memory mapped file to implement an 'auto complete' feature while the user is typing. I have well over 1 million product part numbers stored in a single index file. The file has some typical header information but the bulk of the file is a giant array of fixed size records sorted on the key field.

    At runtime the file is memory mapped, cast to a C-style struct array, and we do a binary search to find matching part numbers as the user types. Only a few memory pages of the file are actually read from disk -- whichever pages are hit during the binary search.

    • Concurrency - I had an implementation problem where it would sometimes memory map the file multiple times in the same process space. This was a problem as I recall because sometimes the system couldn't find a large enough free block of virtual memory to map the file to. The solution was to only map the file once and thunk all calls to it. In retrospect using a full blown Windows service would of been cool.
    • Random Access - The binary search is certainly random access and lightning fast
    • Performance - The lookup is extremely fast. As users type a popup window displays a list of matching product part numbers, the list shrinks as they continue to type. There is no noticeable lag while typing.

提交回复
热议问题