Project

General

Profile

Actions

Feature #13884

closed

Reduce number of memory allocations for "and", "or" and "diff" operations on small arrays

Added by DmitryBochkarev (Dmitry Bochkarev) about 7 years ago. Updated about 7 years ago.

Status:
Closed
Target version:
-
[ruby-core:82725]

Description

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:

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) array_opt.diff DmitryBochkarev (Dmitry Bochkarev), 09/09/2017 02:19 PM
bmlog-20170911-205052.26491.tsv (391 Bytes) bmlog-20170911-205052.26491.tsv DmitryBochkarev (Dmitry Bochkarev), 09/11/2017 03:56 PM
bmlog-20170911-205459.26568.tsv (391 Bytes) bmlog-20170911-205459.26568.tsv DmitryBochkarev (Dmitry Bochkarev), 09/11/2017 03:57 PM
array_opt2.diff (7.78 KB) array_opt2.diff DmitryBochkarev (Dmitry Bochkarev), 09/11/2017 04:11 PM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0