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__?

> This has fixed my slow CRC problem.
> 
> It seems to work for me, but I think that Lars should put it into his 
> check_crc test harness and check it a bit more before applying...
> 
> In particular, I have not checked that you get the same CRC values using 
> each iterator method.
> Lars, were you comparing the LyX CRC routine against the boost crc 
> routine in that test code of yours which was generating different 
> results? I'd be interested to see if you get the same result comparing 
> lyx CRC with iterators and lyx CRC with mmap pointers (just iterators, 
> really...) using your check_crc program.
> 
> Ben.

> --- lyx-1.1.6fix3-orig/src/support/lyxsum.C   Wed Jun  6 02:07:10 2001
> +++ lyx-1.1.6fix3/src/support/lyxsum.C        Tue Nov 27 19:25:19 2001
> @@ -15,11 +15,33 @@
>  
>  #include <config.h>
>  
> +#ifdef HAVE_MMAP
> +#warning lyx::sum() using mmap (lightning fast but untested)
> +#define USE_MMAP
> +#elif HAVE_DECL_ISTREAMBUF_ITERATOR
> +#warning lyx::sum() using istreambuf_iterator (fast)
> +#define USE_ISTREAMBUF_ITER
> +#define USE_STREAM
> +#else
> +#warning lyx::sum() using istream_iterator (slow as a snail)
> +#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 +126,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;
>  }
>  


-- 
albert chin ([EMAIL PROTECTED])

Reply via email to