hamstergem / hamster

Efficient, Immutable, Thread-Safe Collection classes for Ruby
Other
1.89k stars 94 forks source link

SortedSet#drop, SortedSet#take: return self if collection hasn't changed #155

Closed dubek closed 10 years ago

dubek commented 10 years ago

This PR fixes the trivial case for SortedSet#drop and SortedSet#take.

Supposedly we can take this a step further: even for complex operations like take_while and filter and so on, we can check whether any change was made at all to the collection, and if not - return self. But the cost is a complication of the code. I guess we don't want to do this except in the trivial cases like in this PR.

krainboltgreene commented 10 years ago

Early returns have always been an a bug for me, but quite frankly it seems like it needs to happen in Hamster for performance reasons.

krainboltgreene commented 10 years ago

By the way, here's the failure on travis:

  1) Hamster::List doesn't give horrendously bad performance if thread realizing the list sleeps
     Failure/Error: elapsed.should_not > 0.3
       expected not: > 0.3
                got:   0.332870754
     # ./spec/lib/hamster/list/multithreading_spec.rb:46:in `block (2 levels) in <top (required)>'

Only Ruby 2.0.0.

alexdowad commented 10 years ago

That failure has nothing to do with this patch. Probably my clever (?) code for lazy lists performs badly under Ruby 2.0.0. Can you open an issue for that, and I'll look into it when I have time? In the meantime, I'll merge this patch.