To speed up rasterizing a pdf with large bitmap graphics to a high-quality 300 ppi png image, I found that setting -dBufferSpace
as high as possible and -dNumRenderingThreads
to as many cores as available was the most effective for most files, with -dBufferSpace
providing the most significant lift.
The specific values that worked the best were:
-dBufferSpace=2000000000
for 2 gigabytes of buffer space. This took the rasterization of one relatively small file from 14 minutes to just 50 seconds. For smaller files, there wasn't much difference from setting this to 1 gigabyte, but for larger files, it made a significant difference (sometimes 2x faster). Trying to go to 3 gigabytes or above for some reason resulted in an error on startup "Unrecoverable error: rangecheck in .putdeviceprops".
-dNumRenderingThreads=8
for a machine with 8 cores. This took the rasterization of that same file from 14 minutes to 4 minutes (and 8 minutes if using 4 threads). Combining this with the -dBufferSpace
option above took it from 50 seconds to 25 seconds. When combined with -dBufferSpace
however, there appeared to be diminishing returns as the number threads were increased, and for some files there was little effect at all. Strangely for some larger files, setting the number of threads to 1 was actually faster than any other number.
The command overall looked like:
gs -sDEVICE=png16m -r300 -o document.png -dNumRenderingThreads=8 -dBufferSpace=2000000000 -f document.pdf
This was tested with Ghostscript 9.52, and came out of testing the suggestions in @wpgalle3's answer as well as the Improving performance section in the Ghostscript documentation.
A key takeaway from the documentation was that when ghostscript uses "banding mode" due to the raster image output being larger than the value for -dMaxBitmap
, it can take advantage of multiple cores to speed up the process.
Options that were ineffective or counterproductive:
Setting -c "2000000000 setvmthreshold"
(2 gigabytes) either alone or with -dBufferSpace
didn't appear to make a difference.
Setting -sBandListStorage=memory
resulted in a segmentation fault.
Setting -dMaxBitmap=2000000000
(2 gigabytes) significantly slowed down the process and apparently caused it to go haywire, writing hundreds of gigabytes of temporary files without any sign of stopping, prompting me to kill the process short.
Setting -dBandBufferSpace
to half of -dBufferSpace
didn't make a difference for smaller files, but actually slowed down the process rather significantly for larger files by 1.5-1.75x. In the Banding parameters section of the Ghostscript documentation, it's actually suggested not to use -dBandBufferSpace
: "if you only want to allocate more memory for banding, to increase band size and improve performance, use the BufferSpace parameter, not BandBufferSpace."