load_stream loads the whole thing when used intuitively

Offen
#259 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
38/100
Issue-Typ
Bug
Klarheit
Größtenteils klar
Aktivitätsstatus
Veraltet
Tech-Stack
ruby
Bereich
backend

Rechercherichtung

Überprüfe lib/psych.rb im Umfeld der load_stream-Implementierung und vergleiche anhand des großen YAML-Beispiels im Issue das Verhalten mit und ohne Block. Als erledigt gilt, wenn die intuitive Iteration mit der Verarbeitung der Dokumente beginnt, ohne zuerst den gesamten Stream in den Speicher zu laden, und die bestehende Verwendung mit Block weiterhin nutzbar bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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
Vorherrschende Sprache
Ruby
Sterne
596
Forks
228
Ø Merge
24 Min.
Gemergte PRs (30 T.)
5

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus ruby/psych

Alle Issues in ruby/psych

Ähnliche Issues

Weitere Issues zu Ruby

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.