[EMAIL PROTECTED] wrote: >On Tue, Nov 27, 2001 at 11:57:18PM +1100, Ben Stanley wrote: > >>Here is the mmap patch for calculating CRCs quickly. >> > >How about wrapping #warnings in #ifdef __GNUC__? > Oh - I thought that was standard... :-)
If you insist, here's a new patch.
--- lyx-1.1.6fix3-orig/src/support/lyxsum.C Wed Jun 6 02:07:10 2001 +++ lyx-1.1.6fix3/src/support/lyxsum.C Wed Nov 28 00:57:55 2001 @@ -15,11 +15,41 @@ #include <config.h> +// Figure out what iterator implementation we are going to use... +// This currently selects mmap over istreambuf_iterator. +#ifdef HAVE_MMAP + #ifdef __GNUC__ + #warning lyx::sum() using mmap (lightning fast but untested) + #endif + #define USE_MMAP +#elif HAVE_DECL_ISTREAMBUF_ITERATOR + #ifdef __GNUC__ + #warning lyx::sum() using istreambuf_iterator (fast) + #endif + #define USE_ISTREAMBUF_ITER + #define USE_STREAM +#else + #ifdef __GNUC__ + #warning lyx::sum() using istream_iterator (slow as a snail) + #endif + #define USE_ISTREAM_ITER + #define USE_STREAM +#endif + + #include <fstream> #include <iterator> #include "support/lyxlib.h" +#ifdef USE_MMAP +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> +#include <sys/mman.h> +#endif + using std::ifstream; using std::ios; @@ -104,23 +134,56 @@ } + // And this would be the file interface. unsigned long lyx::sum(string const & file) { + +#ifdef USE_STREAM std::ifstream ifs(file.c_str()); if (!ifs) return 0; - -#ifdef HAVE_DECL_ISTREAMBUF_ITERATOR +#endif + + // Setups + +#ifdef USE_ISTREAMBUF_ITER // This is a lot faster... std::istreambuf_iterator<char> beg(ifs); std::istreambuf_iterator<char> end; -#else +#endif + +#ifdef USE_ISTREAM_ITER // than this. ifs.unsetf(std::ios::skipws); std::istream_iterator<char> beg(ifs); std::istream_iterator<char> end; #endif - return do_crc(beg, end); +#ifdef USE_MMAP + int fd = open(file.c_str(), O_RDONLY); + if( !fd ) return 0; + + struct stat info; + int st = fstat(fd, &info); + + void * mm = mmap(0, info.st_size, PROT_READ, + MAP_PRIVATE /*| MAP_ANON*/, fd, 0); // MAP_ANON is not POSIX + if (mm == MAP_FAILED) { + close(fd); + return 0; + } + char *beg = (char*)mm; + char *end = ((char*)mm)+info.st_size; +#endif + + // Do the CRC + unsigned long result = do_crc(beg,end); + + // Clean up +#ifdef USE_MMAP + munmap( mm, info.st_size ); + close(fd); +#endif + return result; }