This example, for comparison, uses memory mapping (apologies for the very stripped down version of the MappedFile class, I would otherwise have had to include a lot more code with some dependencies -- the original has a few more features).
[source lang="cpp"]#include <windows.h>#include <stdio.h>#include <time.h>#include <stdint.h>class MappedFile{ HANDLE file; HANDLE mapping; void* data; uint32_t len; unsigned int mode;public: enum map_mode_t { readwrite }; MappedFile(const char *name, map_mode_t map_mode = readwrite) : file(), mapping(), data(), len(), mode(map_mode) { Open(name, map_mode); } ~MappedFile(){ Close(); } void Open(const char *name, map_mode_t map_mode = readwrite); void Close(); void* Map(unsigned int map_size = 0); void UnMap(); void* Data() const { return data; }; uint32_t Length() const { return len; };};void MappedFile::Open(const char *name, map_mode_t map_mode){ Close(); mode = map_mode; int os_mode = GENERIC_READ | GENERIC_WRITE; file = CreateFile(name, os_mode, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); len = GetFileSize(file, 0); Map();}void MappedFile::Close(){ UnMap(); CloseHandle(file); file = 0;}void* MappedFile::Map(unsigned int map_size){ int os_mode = PAGE_READWRITE; int os_mode2 = FILE_MAP_WRITE; UnMap(); mapping = CreateFileMappingA(file, 0, os_mode, 0, map_size, 0); data = MapViewOfFile(mapping, os_mode2, 0, 0, map_size); return data;}void MappedFile::UnMap(){ if(data) { UnmapViewOfFile(data); CloseHandle(mapping); data = 0; }}int main(){ printf("%u create mapping\n", time(0)); MappedFile f("test.dat"); if(!f.Data()) { puts("fail."); return 1; } printf("len = %u (%u MiB)\n", f.Length(), f.Length()/(1024*1024)); printf("%u begin xor\n", time(0)); for(char* c = (char*) f.Data(); c < f.Data() + f.Length(); ++c) *c ^= 'x'; printf("%u end xor, closing mapping\n", time(0)); f.Close(); printf("%u finished\n", time(0)); return 0;}[/source]
Compiled using Code::Blocks / MinGW-TDM 4.7.1-1, no special stuff, just pressing the "build" button, on a 7200rpm Samsung SATA-150 disk.
Output:1352892208 create mapping
len = 546904331 (521 MiB)
1352892208 begin xor
1352892208 end xor, closing mapping
1352892208 finished
Process returned 0 (0x0) execution time : 0.891 s
Press any key to continue.
These timings show three things:
1. The Sysinternals Cacheset program is broken (I used this to clear the cache prior to the test)
2. The data is quite obviously fetched from the buffer cache.
3. Writing (or modifying) half a gigabyte worth of data does not "stall" (if the computer has sufficient RAM), just like I told you.
Edited by samoth, 14 November 2012 - 05:38 AM.