From: eregontp@...
Date: 2017-09-10T20:20:38+00:00
Subject: [ruby-core:82739] [Ruby trunk Misc#13884] Reduce number of memory allocations for "and", "or" and "diff" operations on small arrays

Issue #13884 has been updated by Eregon (Benoit Daloze).


Note that this would change the semantics as it uses #== and not #eql? to compare elements.
The comparison should always be done with #eql? to remain compatible.

It also does not call #hash but that is likely a much smaller problem.

----------------------------------------
Misc #13884: Reduce number of memory allocations for "and", "or" and "diff" operations on small arrays
https://bugs.ruby-lang.org/issues/13884#change-66588

* Author: DmitryBochkarev (Dmitry Bochkarev)
* Status: Open
* Priority: Normal
* Assignee: DmitryBochkarev (Dmitry Bochkarev)
----------------------------------------
Very often, arrays are used to filter parameters and to select interesting items from 2 collections and very often these collections are small enough, for example:

~~~ ruby
SAFE_COLUMNS = [:id, :title, :created_at]

def columns
  @all_columns & SAFE_COLUMNS
end
~~~


In this patch, I got rid of unnecessary memory allocations for small arrays when "and", "or" and "diff" operations are performed.

I tested this patch on 64 architecture and found out that in arrays with 32 elements, element search is performed faster than retrieving an element from the hash (tested on collections with 16, 32, 64 and 128 elements).

---Files--------------------------------
array_opt.diff (5.21 KB)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>