optimize queue size computation and minor other optimizations