yob/pdf-reader — pull request #64 by alexdowad (2012-08-31 20:47:27+00:00)
Title: Performance optimizations in PageState (cuts ~45% off time to extract text from large PDF)

When I was profiling to determine whether my regexp-based lexer was anything more than a waste of time, I noticed that Matrix#* (called from within PageState) was a major hotspot. Additionally, about 30% of the benchmark time was being spent in the garbage collector, so I suspected that all those matrix operations might be allocating a lot of objects.

After examining PageState, I found it did not actually need the full generality of Matrix#*. *All* the matrices it uses are 3x3, so it is easy to write "special case" code which is much faster than Matrix#*, and which doesn't allocate any memory to boot.

`ruby -Ilib tools/page_bench spec/data/no_text_spaces.pdf` before:

user system total real
1 0.240000 0.000000 0.240000 ( 0.247030)
2 0.150000 0.000000 0.150000 ( 0.146098)
3 0.130000 0.000000 0.130000 ( 0.124756)
4 0.120000 0.000000 0.120000 ( 0.124500)
5 0.180000 0.000000 0.180000 ( 0.183685)
6 0.110000 0.000000 0.110000 ( 0.103796)

and after:

user system total real
1 0.130000 0.000000 0.130000 ( 0.136008)
2 0.090000 0.000000 0.090000 ( 0.081782)
3 0.060000 0.000000 0.060000 ( 0.063514)
4 0.070000 0.000000 0.070000 ( 0.065876)
5 0.090000 0.000000 0.090000 ( 0.094359)
6 0.050000 0.000000 0.050000 ( 0.054543)