Affects Version/s: None
Fix Version/s: None
set and vec currently reconstruct their inputs even when they are already of the requested type. Since it's a pretty common pattern to call set/vec on an input to ensure its type, this seems like an easy performance win in a common case.
Proposed: Check for set? in set and vec? in vec and return the coll as is if already of the requested type.
See attached clj1410-bench.txt for test details :
|set/10||set||1.452 µs||0.002 µs||726x faster|
|set/1000||set||248.842 µs||0.006 µs||41473x faster|
|vector/10||set||1.288 µs||1.323 µs||slightly slower|
|vector/1000||set||222.992 µs||221.116 µs||~same|
|set/10||vec||0.614 µs||0.592 µs||~same|
|set/1000||vec||56.876 µs||55.920 µs||~same|
|vector/10||vec||0.252 µs||0.007 µs||36x faster|
|vector/1000||vec||24.428 µs||0.007 µs||3500x faster|
As expected, if an instance of the correct type is passed, then the difference is large (with bigger savings for sets which do more work for dupe checking). In cases where the type is different, there is an extra instance? check (which looks to be jit'ed away or negligible). We only see a slower time in the case of passing a small vector to the set function - 3% slower (35 ns). The benefit seems greater than the cost for this change.
Group discussion: https://groups.google.com/forum/#!topic/clojure-dev/fg4wtqzu0eY