dagolden/Path-Tiny — pull request #95 by schwern (2014-04-14 22:58:30+00:00)
Title: Use the flyweight pattern

Since Path::Tiny objects are immutable, there's no reason to create a new one for the same file path.

This patch makes path() store each object in a cache as a weak ref. If the same path is asked for while an instance already exists, it will return the existing instance. This is about 3x faster and, of course, much more memory efficient. My very rough estimates put a list of 200,000 instances of `path("foo/bar")` at using 48M before and 10M after (10M is the cost of an empty list of 200,000 elements).

The down side is the extra time spent managing the cache increases path() performance by about 35% when a path is not being reused, and there is the extra memory cost of the cache.

I figure this is a win for those case where you care about the runtime and memory performance of Path::Tiny: you have a lot of paths which means you're probably going to have a lot of the same paths.

PS It works on 5.8.1.