SplashXPathScanner: Optionally use small_vector from boost

Currently, each row in the intersections vector is allocated separately,
when the first intersection is added.

To avoid these allocations for common simple polygons,
boost::container::small_vector<4, T> is used, which stores up to
4 intersections inline. small_vector is a header-only class.

For the documents from #57 (fdo#96728) and #24 (fdo#78728), the
runtime/memory is significantly reduced (according to /usr/bin/time -v):
(1) $> pdftoppm -r 18 -aa no runsforever-poppler.pdf
(2) $> pdftoppm surf-types.pdf

Before/After
                                  runsforever-poppler |   surf-types
User time (seconds):                2348.08 / 1773.53 |   7.76 /  5.02
Maximum resident set size (kbytes):   46288 /   45896 |  14076 / 13748
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7001544..edcc5a2 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -286,6 +286,14 @@
   include_directories(SYSTEM ${LCMS2_INCLUDE_DIR})
 endif()
 
+if(ENABLE_SPLASH)
+  find_package(Boost 1.58.0)
+  if(Boost_FOUND)
+    include_directories(SYSTEM ${Boost_INCLUDE_DIRS})
+    set(USE_BOOST_HEADERS ON)
+  endif()
+endif()
+
 # Recent versions of poppler-data install a .pc file.
 # Use it to determine the encoding data path, if available.
 # Default to the same prefix otherwise.
@@ -730,6 +738,7 @@
 show_end_message_yesno("use curl" ENABLE_LIBCURL)
 show_end_message_yesno("use libopenjpeg2" WITH_OPENJPEG)
 show_end_message_yesno("use lcms2" USE_CMS)
+show_end_message_yesno("use boost" Boost_FOUND)
 show_end_message_yesno("command line utils" ENABLE_UTILS)
 show_end_message_yesno("fuzz target" ENABLE_FUZZER)
 show_end_message("test data dir" ${TESTDATADIR})
@@ -762,6 +771,10 @@
   message("Warning: You're not compiling any JPX decoder. Some files will fail to display properly.")
 endif()
 
+if(ENABLE_SPLASH AND NOT Boost_FOUND)
+  message("Warning: Use of boost is recommended for better performance.")
+endif()
+
 set(ARCHIVE_NAME ${CMAKE_PROJECT_NAME}-${POPPLER_VERSION})
 add_custom_target(dist
     COMMAND
diff --git a/poppler/poppler-config.h.cmake b/poppler/poppler-config.h.cmake
index f0a5a1a..746d863 100644
--- a/poppler/poppler-config.h.cmake
+++ b/poppler/poppler-config.h.cmake
@@ -18,6 +18,7 @@
 // Copyright (C) 2016 Tor Lillqvist <tml@collabora.com>
 // Copyright (C) 2017 Adrian Johnson <ajohnson@redneon.com>
 // Copyright (C) 2018 Adam Reichold <adam.reichold@t-online.de>
+// Copyright (C) 2018 Stefan Brüns <stefan.bruens@rwth-aachen.de>
 //
 // To see a description of the changes please see the Changelog file that
 // came with your tarball or type make ChangeLog if you are building from git
@@ -115,6 +116,11 @@
 #cmakedefine USE_CMS 1
 #endif
 
+/* Use header-only classes from Boost in the Splash backend */
+#ifndef USE_BOOST_HEADERS
+#cmakedefine USE_BOOST_HEADERS 1
+#endif
+
 //------------------------------------------------------------------------
 // version
 //------------------------------------------------------------------------
diff --git a/splash/SplashXPathScanner.cc b/splash/SplashXPathScanner.cc
index e8abe94..042a6ef 100644
--- a/splash/SplashXPathScanner.cc
+++ b/splash/SplashXPathScanner.cc
@@ -344,9 +344,11 @@
   }
 
   auto& line = allIntersections[y - yMin];
+#ifndef USE_BOOST_HEADERS
   if (line.empty()) {
       line.reserve(4);
   }
+#endif
   line.push_back(intersect);
 
   return true;
diff --git a/splash/SplashXPathScanner.h b/splash/SplashXPathScanner.h
index 1ac8ce8..88b9d8c 100644
--- a/splash/SplashXPathScanner.h
+++ b/splash/SplashXPathScanner.h
@@ -25,6 +25,12 @@
 
 #include "SplashTypes.h"
 
+#include <poppler-config.h>
+
+#ifdef USE_BOOST_HEADERS
+#include <boost/container/small_vector.hpp>
+#endif
+
 #include <vector>
 
 class SplashXPath;
@@ -94,7 +100,11 @@
   int xMin, yMin, xMax, yMax;
   bool partialClip;
 
+#ifdef USE_BOOST_HEADERS
+  typedef boost::container::small_vector<SplashIntersect, 4> IntersectionLine;
+#else
   typedef std::vector<SplashIntersect> IntersectionLine;
+#endif
   std::vector<IntersectionLine> allIntersections;
 
   friend class SplashXPathScanIterator;
@@ -109,7 +119,11 @@
   bool getNextSpan(int *x0, int *x1);
 
 private:
+#ifdef USE_BOOST_HEADERS
+  typedef boost::container::small_vector<SplashIntersect, 4> IntersectionLine;
+#else
   typedef std::vector<SplashIntersect> IntersectionLine;
+#endif
   const IntersectionLine &line;
 
   size_t interIdx;	// current index into <line>