Attached patches increase PDF load speed on Windows platform with MSVC runtime

 * By using lower_bound algorithm instead of equal_range (much faster
   with slow MSVC debug iterators)
 * By using "rbS" file read flag instead of "rb", where "S" means
   "optimize buffering for sequental access"
 * By using fgetc/ungetc instead of ftello/fseeko for next byte lookup


I'm not sure that fgetc/ungetc trick should be used only under MSVC: C library reference says that ungetc always can rollback reading single character, so it can be used for next byte lookup on any platform. It's faster than doing ftello/fgetc/fseeko stuff, and size of benefit depends on C library implementation.


------------------------------------------------------------------------
Sergey Shambir, Senior Software Engineer, iSpring Solutions <http://www.ispringsolutions.com/>
>From 9cb7bdfdf7eceb81089b5226b8a7b0b426b43a81 Mon Sep 17 00:00:00 2001
From: Sergey Shambir <ssham...@yandex.ru>
Date: Mon, 18 Jul 2016 10:38:56 +0300
Subject: [PATCH 1/2] OPTIMIZE: Speedup PDF loading in Debug MSVC builds by
 using 'std::lower_bound' instead of 'std::equal_range'

---
 src/base/PdfVecObjects.cpp | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)

diff --git a/src/base/PdfVecObjects.cpp b/src/base/PdfVecObjects.cpp
index 529e3b4..47e20a8 100644
--- a/src/base/PdfVecObjects.cpp
+++ b/src/base/PdfVecObjects.cpp
@@ -149,17 +149,11 @@ PdfObject* PdfVecObjects::GetObject( const PdfReference & ref ) const
         const_cast<PdfVecObjects*>(this)->Sort();
 
     PdfObject refObj( ref, NULL );
-    std::pair<TCIVecObjects,TCIVecObjects> it = 
-        std::equal_range( m_vector.begin(), m_vector.end(), &refObj, ObjectComparatorPredicate() );
-
-    if( it.first != it.second )
-        return *(it.first);
-
-    /*
-    const TCIVecObjects it ( std::find_if( this->begin(), this->end(), ObjectsComparator( ref ) ) );
-    if( it != this->end() )
-        return (*it);
-    */
+    TCIVecObjects it = std::lower_bound( m_vector.begin(), m_vector.end(), &refObj, ObjectComparatorPredicate() );
+    if( it != m_vector.end() && (refObj.Reference() == (*it)->Reference()) )
+    {
+        return *it;
+    }
 
     return NULL;
 }
-- 
2.7.4

>From 9945aefb77c1e5760c6b2caace0ccd5f0eca2417 Mon Sep 17 00:00:00 2001
From: Sergey Shambir <sergey.sham...@ispringsolutions.com>
Date: Mon, 1 Aug 2016 13:51:40 +0300
Subject: [PATCH 2/2] OPTIMIZE: improved Load/DelayLoad performance 2-3 times
 on Windows with MSVC compiler/runtime

---
 src/base/PdfInputDevice.cpp | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/base/PdfInputDevice.cpp b/src/base/PdfInputDevice.cpp
index 0c34dd0..76811d8 100644
--- a/src/base/PdfInputDevice.cpp
+++ b/src/base/PdfInputDevice.cpp
@@ -82,7 +82,7 @@ PdfInputDevice::PdfInputDevice( const wchar_t* pszFilename )
 
     try {
         // James McGill 16.02.2011 Fix wide character filename loading in windows
-        m_pFile = _wfopen(pszFilename, L"rb");
+        m_pFile = _wfopen(pszFilename, L"rbS");
         if( !m_pFile)
         {
             PdfError e( ePdfError_FileNotFound, __FILE__, __LINE__ );
@@ -177,6 +177,10 @@ int PdfInputDevice::Look() const
     if (m_pStream)
         return m_pStream->peek();
     if (m_pFile) {
+#if defined(_MSC_VER) // workaround slow ftello/fseeko prior to Visual Studio 2015
+        int ch = fgetc(m_pFile);
+        ungetc(ch, m_pFile);
+#else
         pdf_long lOffset = ftello( m_pFile );
 
         if( lOffset == -1 )
@@ -186,6 +190,7 @@ int PdfInputDevice::Look() const
 
         if( fseeko( m_pFile, lOffset, SEEK_SET ) == -1 )
             PODOFO_RAISE_ERROR_INFO( ePdfError_InvalidDeviceOperation, "Failed to seek back to the previous position" );
+#endif
 
         return ch;
     }
-- 
2.7.4

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Podofo-users mailing list
Podofo-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/podofo-users

Reply via email to