May assumptions may be totally wrong (in which case just disregard what I'm saying), but assuming
$s is a simple counter, you would be better off by storing the references in an array instead of in a hash. If the keys to your hash are sequential integers starting with zero, an array is less overhead and faster.
CountZero
"If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law