This patch avoids string concatenations which add *big* overhead for large documents. Also, the debug-related changes yield a smaller performance improvement.
For 1.22 (Python version).
Significant performance improvements
Significant performance improvements