Instead of allocating such arrays, we could introduce an incremental allocation behind the hood. E.g.: we'd execute the query once to retrieve the top 100, and if there is more, we'd do it again with 200, then 400, then 800, and so on.