👏

[Bug #19470] 巨大な配列に対して4つ以上の要素を読み取りを行ったあとの書き込みが遅くなるというバグ報告

2024/06/06に公開

[Bug #19470] Frequent small range-reads from and then writes to a large array are very slow

  • 巨大な配列に対して4つ以上の要素を読み取りを行ったあとの書き込みが遅くなるというバグ報告
arr = [0] * 100000
times = []

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
  # 3つの要素へのアクセスなら問題ない
  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
  # 4つの要素にアクセスした後に配列を書き換えると遅くなる
  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
# => [0.012026550999991647, 0.011709127999992613, 5.5741764999999415, 5.8227263360001, 5.598053216000058]
  • コピーオンライトの影響でこうなっているってコメントには書いてありますね
GitHubで編集を提案

Discussion