Project

General

Profile

Misc #10754

Tiny optimisation of Set#include?

Added by ismael (Ismael Abreu) almost 5 years ago. Updated almost 5 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
[ruby-core:67664]

Description

Hi!

I'm not sure if this a relevant optimisation but here it goes. A simple way to improve Set#include?
I don't think there should be any side effects with this change.

Here are some dummy benchmarks https://gist.github.com/ismaelga/68bb3ea51b4742f65699
They are not consistent, but that should be because of garbage collection or something. Right?

I've also sent a Pull Request: https://github.com/ruby/ruby/pull/810


Files

patch.diff (544 Bytes) patch.diff ismael (Ismael Abreu), 01/18/2015 01:18 AM

Associated revisions

Revision 709e0ecb
Added by normal almost 5 years ago

set: speed up Set#include?

  • lib/set.rb (initialize): internal hash defaults to false

  • lib/set.rb (include?): use Hash#[] for optimized dispatch.
    Patch by Ismael Abreu ismaelga@gmail.com
    [ruby-core:67664] [Misc #10754]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49568 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 49568
Added by normalperson (Eric Wong) almost 5 years ago

set: speed up Set#include?

  • lib/set.rb (initialize): internal hash defaults to false

  • lib/set.rb (include?): use Hash#[] for optimized dispatch.
    Patch by Ismael Abreu ismaelga@gmail.com
    [ruby-core:67664] [Misc #10754]

Revision 49568
Added by normal almost 5 years ago

set: speed up Set#include?

  • lib/set.rb (initialize): internal hash defaults to false

  • lib/set.rb (include?): use Hash#[] for optimized dispatch.
    Patch by Ismael Abreu ismaelga@gmail.com
    [ruby-core:67664] [Misc #10754]

Revision 49568
Added by normal almost 5 years ago

set: speed up Set#include?

  • lib/set.rb (initialize): internal hash defaults to false

  • lib/set.rb (include?): use Hash#[] for optimized dispatch.
    Patch by Ismael Abreu ismaelga@gmail.com
    [ruby-core:67664] [Misc #10754]

Revision 49568
Added by normal almost 5 years ago

set: speed up Set#include?

  • lib/set.rb (initialize): internal hash defaults to false

  • lib/set.rb (include?): use Hash#[] for optimized dispatch.
    Patch by Ismael Abreu ismaelga@gmail.com
    [ruby-core:67664] [Misc #10754]

Revision 49568
Added by normal almost 5 years ago

set: speed up Set#include?

  • lib/set.rb (initialize): internal hash defaults to false

  • lib/set.rb (include?): use Hash#[] for optimized dispatch.
    Patch by Ismael Abreu ismaelga@gmail.com
    [ruby-core:67664] [Misc #10754]

History

Updated by normalperson (Eric Wong) almost 5 years ago

This speedup probably happens because Hash#[] has an optimized dispatch
VM instruction (opt_aref in insns.def) while Hash#include? does not.

Seems like an OK change to make, I'll commit in a few days unless others
raise objections.

Updated by ismael (Ismael Abreu) almost 5 years ago

With the benchmarks I've run I noticed that sometimes rarely, the old include? was faster.
Do you have any idea why is that? The only cause I can think of is the GC.

Updated by normalperson (Eric Wong) almost 5 years ago

I didn't notice the old code being faster, and I don't see your
benchmark triggering GC. Perhaps it is related to memory size
(swapping or CPU cache misses) or power management
(CPU clock frequency adjustment, "turbo boost", etc...)
Modern hardware is tricky to benchmark :/

Updated by ismael (Ismael Abreu) almost 5 years ago

Any update on this?

Updated by normalperson (Eric Wong) almost 5 years ago

Thanks for the reminder, I got distracted with other stuff :x
Committed r49568

Updated by ismael (Ismael Abreu) almost 5 years ago

Awesome! :)

Shouldn't this issue be closed also?

Updated by marcandre (Marc-Andre Lafortune) almost 5 years ago

  • Status changed from Open to Closed

Also available in: Atom PDF