The reference counting does add a bit of overhead. My approach at the time was to keep the code fairly simple so that others could understand what was going on. The code is readable here but not necessarily the most performant. Feel free to improve it.