Lars Gullik Bjønnes wrote:

>I think you should submit the complete patch again.
>
Complete patch attached.

process_block does lead to a significant improvement.

Warm cache

ben [11:26:45] 
/share/install/linux/extras/lyx/lyx-HEAD/lyx-devel/src/support/tests $ 
time crccheck.do_crc /share/mp3/bassic/EI.mp3
CRC: 3089166751

real    0m0.448s
user    0m0.450s
sys     0m0.000s
ben [11:26:49] 
/share/install/linux/extras/lyx/lyx-HEAD/lyx-devel/src/support/tests $ 
time crccheck.process_block /share/mp3/bassic/EI.mp3
CRC: 3089166751

real    0m0.150s
user    0m0.130s
sys     0m0.020s

ben [11:39:13] /share/install/linux/extras/lyx/lyx-HEAD $ ls -al 
/share/mp3/bassic/EI.mp3
-rw-r--r--    1 ben      users     5661289 Nov  5 08:45 
/share/mp3/bassic/EI.mp3

--- lyx-devel/src/support/lyxsum.C.orig.cvs     Mon Dec  3 09:20:46 2001
+++ lyx-devel/src/support/lyxsum.C      Mon Dec  3 11:29:17 2001
@@ -10,8 +10,6 @@
 
 #include <config.h>
 
-#include <fstream>
-#include <iterator>
 #include <algorithm>
 #include <boost/crc.hpp>
 
@@ -31,23 +29,88 @@
 
 } // namespace
 
-
-// And this would be the file interface.
-unsigned long lyx::sum(string const & file)
+// Various implementations of lyx::sum(), depending on what methods
+// are available. Order is faster to slowest.
+#if defined(HAVE_MMAP) && defined(HAVE_MUNMAP)
+       #ifdef WITH_WARNINGS
+               #warning lyx::sum() using mmap (lightning fast)
+       #endif
+
+       #include <sys/types.h>
+       #include <sys/stat.h>
+       #include <fcntl.h>
+       #include <unistd.h>
+       #include <sys/mman.h>
+
+       unsigned long lyx::sum(string const & file)
+       {
+               int fd = open(file.c_str(), O_RDONLY);
+               if( !fd ) return 0;
+
+               struct stat info;
+               fstat(fd, &info);
+
+               void * mm = mmap(0, info.st_size, PROT_READ,
+                                MAP_PRIVATE, fd, 0);
+               if (mm == MAP_FAILED) {
+                       close(fd);
+                       return 0;
+               }
+               
+               char *beg = static_cast<char*>(mm);
+               char *end = beg + info.st_size;
+
+               boost::crc_32_type crc;
+               crc.process_block(beg,end);
+               unsigned long result = crc.checksum();
+               
+               munmap( mm, info.st_size );
+               close(fd);
+
+               return result;
+       }
+#else // No mmap
+       #include <fstream>
+       #include <iterator>
+
+       #if HAVE_DECL_ISTREAMBUF_ITERATOR
+               #ifdef WITH_WARNINGS
+                       #warning lyx::sum() using istreambuf_iterator (fast)
+               #endif
+               unsigned long lyx::sum(string const & file)
+               {
+                       std::ifstream ifs(file.c_str());
+                       if (!ifs) return 0;
+
+                       std::istreambuf_iterator<char> beg(ifs);
+                       std::istreambuf_iterator<char> end;
+
+                       return do_crc(beg,end);
+               }
+       #else
+               #ifdef WITH_WARNINGS
+                       #warning lyx::sum() using istream_iterator (slow as a snail)
+               #endif
+               unsigned long lyx::sum(string const & file)
+               {
+                       std::ifstream ifs(file.c_str());
+                       if (!ifs) return 0;
+
+                       ifs.unsetf(std::ios::skipws);
+                       std::istream_iterator<char> beg(ifs);
+                       std::istream_iterator<char> end;
+
+                       return do_crc(beg,end);
+               }
+       #endif
+#endif // mmap
+
+#if 0
+#include <iostream>
+int main(int /*argc*/, char * argv[])
 {
-       std::ifstream ifs(file.c_str());
-       if (!ifs) return 0;
-       
-#ifdef HAVE_DECL_ISTREAMBUF_ITERATOR
-       // This is a lot faster...
-       std::istreambuf_iterator<char> beg(ifs);
-       std::istreambuf_iterator<char> end;
-#else
-       // than this.
-       ifs.unsetf(std::ios::skipws);
-       std::istream_iterator<char> beg(ifs);
-       std::istream_iterator<char> end;
-#endif
+       std::string const fil(argv[1]);
 
-       return do_crc(beg, end);
+       std::cout << "CRC: " << lyx::sum(fil) << std::endl;
 }
+#endif
--- lyx-devel/src/support/ChangeLog.orig        Mon Dec  3 11:32:13 2001
+++ lyx-devel/src/support/ChangeLog     Mon Dec  3 11:36:32 2001
@@ -1,3 +1,8 @@
+2001-12-03  Ben Stanley <[EMAIL PROTECTED]>
+
+       * lyxsum.C: Added mmap version of CRC and made it selected 
+       by default where available. Used process_block for crc for speedup.
+       
 2001-12-01  John Levon  <[EMAIL PROTECTED]>
 
        * filetools.C: more robust failure for DirList()

Reply via email to