load_stream loads the whole thing when used intuitively

オープン
#259 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
38/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
ruby
領域
backend

調査の方向性

lib/psych.rb の load_stream 実装周辺を確認し、issue にある大きな YAML の例を使って、ブロックありとブロックなしの動作を比較してください。完了条件は、直感的な反復処理によってストリーム全体を最初にメモリへ読み込むことなくドキュメントの処理が開始され、既存のブロック付きの使用も引き続き利用できることです。

索引モデルが issue の本文から書いたものです。

説明

Surprisingly, load_stream doesn't handle streams but loads all docs in an IO object at once, and only then iterates on the result. This results in much latency as well as huge swaths of memory being taken hostage when used intuitively.

Maybe this could return an Enumerator (that could be to_a) or do a lazy map (that could be forced).

Example "naive" code:

# tested with ruby 2.2.3
require 'yaml'

def rss
  `ps -p #{Process.pid} -o rss`.split("\n").last.to_i
end

unless File.exist? 'test.yml'
  File.open('test.yml', 'wb') do |f|
    1_000_000.times do
      f.write "---\n"
      f.write format("foo: '%016x'\n", Random.rand(2**64))
    end
  end
end

s = YAML.load_stream File.open('test.yml')
puts 'loaded'

s.each.with_index do |_, i|
  puts i if i % 100_000 == 0
end

puts "rss: #{rss} kB"

While load_stream does support a block arg, doing the following is cumbersome and non-discoverable:

s = Enumerator.new do |y|
  YAML.load_stream(File.open('test.yml')) { |doc| y << doc }
end
主要言語
Ruby
スター
596
フォーク
228
平均マージ
24分
マージ済み PR(30日)
5

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

ruby/psych のほかの issue

ruby/psych の issue をすべて見る

似ている issue

Ruby の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。