Fix #4935

These changes greatly reduce the time and memory needed to render the document in #4935.