osheroff/active_record_shards — pull request #1 by bquorning (2012-06-07 12:59:50+00:00)
Title: Bquorning/memoize shard name

Running a performance test, it seemed that `ShardSelection#shard_name` could use some memoization:

Objects: 4.78% 1.68% 17590.00 6177.00 0.00 11413.00 1782 ActiveRecordShards::ShardSelection#shard_name
ProcessTime: 5.43% 0.87% 0.11 0.02 0.00 0.10 1782 ActiveRecordShards::ShardSelection#shard_name

Same performance test, with this branch applied:

Objects: - - 2289.00 2281.00 0.00 8.00 2279 ActiveRecordShards::ShardSelection#shard_name
ProcessTime: 2.34% 0.48% 0.05 0.01 0.00 0.04 2279 ActiveRecordShards::ShardSelection#shard_name

The primary goal here is to create fewer objects, so the GC has to run fewer times. For this reason, I am using an Array as the memoization hash key. This is slower than looking up with a string containing each of the elements, but calling `to_s` on them would generate almost as many objects as the memoization itself was saving.