load_stream loads the whole thing when used intuitively

未關閉
#259 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
38/100
Issue 類型
缺陷
描述清晰度
基本清楚
活躍度
停滯
技術堆疊
ruby
領域
backend

研究方向

檢查 lib/psych.rb 中 load_stream 實作附近的程式碼,並使用 issue 中的大型 YAML 範例比較有 block 和無 block 時的行為。完成的標準是,直觀的迭代能夠在不先將整個 stream 載入記憶體的情況下開始處理文件,同時現有的 block 用法仍然可用。

由索引模型根據 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 分鐘
30 天內合併 PR
5

環境準備

  • 沒有 Dockerfile 或 Docker Compose 檔案
  • 沒有 Pull Request 範本
  • 閱讀貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

ruby/psych 的其他 Issue

查看 ruby/psych 的全部 Issue

相似的 Issue

更多 Ruby Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。