Project

General

Profile

Actions

Bug #19470

open

Frequent small range-reads from and then writes to a large array are very slow

Added by giner (Stanislav German-Evtushenko) over 1 year ago. Updated over 1 year ago.

Status:
Open
Assignee:
-
Target version:
-
ruby -v:
ruby 3.2.1 (2023-02-08 revision 31819e82c8) [x86_64-linux]
[ruby-core:112638]

Description

Write to a large array gets very slow when done after range-reading more than 3 items. In such case the original array gets marked as shared which triggers CoW on a small change afterwards. This leads to a significant performance impact and high memory utilization in cases when we need to range-read/write from/to the same array many times. While this issue can be avoided by reading <= 3 elements at a time the main problem is that this behaviour is not obvious and hard to catch on on-trivial projects.

times = []
arr = [0] * 100000

times.push 0
100000.times do
  time_start = Time.now
  arr[5] = 100  # takes 0.01662315899999512
  times[-1] += Time.now - time_start
end

times.push 0
100000.times do
  arr[0..2]
  time_start = Time.now
  arr[5] = 100  # takes 0.01826406799999659
  times[-1] += Time.now - time_start
end

times.push 0
100000.times do
  arr[0..3]
  time_start = Time.now
  arr[5] = 100  # takes 7.757753919000069
  times[-1] += Time.now - time_start
end

times.push 0
100000.times do
  arr.dup
  time_start = Time.now
  arr[5] = 100  # takes 7.626929300999957
  times[-1] += Time.now - time_start
end

times.push 0
100000.times do
  arr.clone
  time_start = Time.now
  arr[5] = 100  # takes 8.216933763000046
  times[-1] += Time.now - time_start
end

p times
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0