For another minuscule optimisation: you shouldn't need to look up both `startptr` and `endptr` in each iteration, since each `startptr` is the previous iteration's `endptr`.