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 摘要。