From: "rdil (Reece Dunham) via ruby-core" Date: 2026-11-14T16:45:52+00:00 Subject: [ruby-core:127491] [Ruby Bug#22551] YJIT: String#<< clears the receiver's coderange Issue #22551 has been reported by rdil (Reece Dunham). ---------------------------------------- Bug #22551: YJIT: String#<< clears the receiver's coderange https://bugs.ruby-lang.org/issues/22551 * Author: rdil (Reece Dunham) * Status: Open * ruby -v: 4.0.7 * Backport: 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN ---------------------------------------- With YJIT, `str << other` leaves `str` with an unknown coderange when both strings have the same encoding. The interpreter keeps the coderange. Any later operation that needs it (`length`, `[]`, `valid_encoding?`) then rescans the whole string, so code that appends and reads in a loop is much slower with YJIT than without. ```ruby # Run twice: `ruby repro.rb` and `ruby --yjit repro.rb` require 'objspace' require 'json' def append(buf, chunk) = buf << chunk def coderange(str) = JSON.parse(ObjectSpace.dump(str))['coderange'] || 'unknown' chunk = '�' * 100 # valid UTF-8, not ASCII chunk.valid_encoding? buf = +'�' buf.valid_encoding? 1_000.times { append(+'�', chunk) } # let YJIT compile append append(buf, chunk) puts RUBY_DESCRIPTION puts "coderange after <<: #{coderange(buf)}" buf = +'' started = Process.clock_gettime(Process::CLOCK_MONOTONIC) 2_000.times do append(buf, chunk) buf.length end puts format('2,000 appends, each followed by #length: %.0f ms', (Process.clock_gettime(Process::CLOCK_MONOTONIC) - started) * 1000) ``` Results on arm64 macOS: | ruby | without YJIT | with --yjit | |--------|--------------|-----------------| | 3.3.13 | valid, 17 ms | unknown, 400 ms | | 3.4.9 | valid, 17 ms | unknown, 477 ms | | 4.0.5 | valid, 17 ms | unknown, 479 ms | | 4.0.7 | valid, 17 ms | unknown, 504 ms | -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/