Fix #4935

These changes greatly reduce the time and memory needed to render the document in #4935.
According to Travis, this line is one character too long.