From 729b680841889a56b55555daa7b4bb9ee71eb3f9 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Wed, 2 Feb 2011 21:56:40 +0000 Subject: [PATCH] set MAP_NOCACHE if we can Set the MAP_NOCACHE flags in mmap() if we can. On OS X this stops the mad super-aggressive disc caching and produces a huge improveemnt in performance. --- ChangeLog | 2 ++ TODO | 5 +++-- libvips/iofuncs/im_mapfile.c | 11 +++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 69ecc86e..227e61c6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -28,6 +28,8 @@ - move im_stretch3() to deprecated - move im_clamp() to deprecated - gtk-doc for video ... all operators done! amazing argh +- set MAP_NOCACHE on OS X, otherwise performance dives off a cliff with + files larger than memory 30/11/10 started 7.24.0 - bump for new stable diff --git a/TODO b/TODO index 7b6e04b2..5b831511 100644 --- a/TODO +++ b/TODO @@ -1,7 +1,8 @@ -- does OS X have any mmap flags which could stop their crazy caching? +- use g_mmap() or whatever it's called ... or perhaps they don't have all + these options + -- use g_mmap() or whatever it's called - use |_O_TEMPORARY to open()'s mode on Windows for tmpfiles to increase the diff --git a/libvips/iofuncs/im_mapfile.c b/libvips/iofuncs/im_mapfile.c index 5a2bc852..a09e5bd8 100644 --- a/libvips/iofuncs/im_mapfile.c +++ b/libvips/iofuncs/im_mapfile.c @@ -163,12 +163,23 @@ im__mmap( int fd, int writeable, size_t length, gint64 offset ) #else /*!OS_WIN32*/ { int prot; + int flags; if( writeable ) prot = PROT_WRITE; else prot = PROT_READ; + flags = MAP_SHARED; + + /* OS X caches mmapped files very aggressively if this flags is not + * set. Scanning a large file without this flag will cause every other + * process to get swapped out and kill performance. + */ +#ifdef MAP_NOCACHE + flags |= MAP_NOCACHE; +#endif /*MAP_NOCACHE*/ + /* Casting gint64 to off_t should be safe, even on *nixes without * LARGEFILE. */