From: "kddnewton (Kevin Newton) via ruby-core" Date: 2026-12-01T15:01:37+00:00 Subject: [ruby-core:127785] [Ruby Feature#22667] Define clear limits for source code (number of arguments, number of lines, etc) Issue #22667 has been updated by kddnewton (Kevin Newton). We have a bit of this: https://bugs.ruby-lang.org/issues/20488#note-1. Prism limits line numbers to `int32_t` and source offsets to `uint32_t`. So I think even today we could make that an `int32_t` and it would be considered a non-breaking change. There is also VM_CALL_KW_LEN_MAX, which limits the number of unique keyword arguments on calls and parameters on method definitions to `UINT16_MAX`. Personally I think it makes sense to limit positionals in the same way. Another thing that could help would be to limit node ids. Prism limits to `uint32_t`, but I believe they're stored as full ints in iseqs. We need a sentinel for when an insn doesn't come from a node, but maybe `UINT32_MAX` would suffice? ---------------------------------------- Feature #22667: Define clear limits for source code (number of arguments, number of lines, etc) https://bugs.ruby-lang.org/issues/22667#change-120342 * Author: byroot (Jean Boussier) * Status: Open ---------------------------------------- ### Context Instructions Sequences (ISeq) are a very important contributor to Ruby applications memory usage, every method, every block etc has an associated ISeq, and Iseq are quite large. On 4.0, the smallest possible Iseq (`RubyVM::InstructionSequence.compile('')`, just `putnil; leave;`) is between `448B` and `480B` large depending on which allocator is used. Given it's quite idiomatic in Ruby to use lots of small methods, real world Ruby application tend to contain a lot of ISeq. For largish Rails application, Iseq quickly start occupying several hundred megabytes of memory: ```ruby require "objspace" require "json" GC.start f = ObjectSpace.dump_all(output: :file) f.rewind n = b = 0 f.each_line do |l| next unless l.include?(%q("imemo_type":"iseq")) n += 1 b += JSON.parse(l)["memsize"] end puts "#{n} iseqs, #{(b / 1e6).round(1)} MB, #{b / n} B/iseq" ``` >From one of our production app: ``` 589211 iseqs, 599.7 MB, 1017 B/iseq ``` And that's just the `memsize_of` estimate, that doesn't account for the `malloc` overhead and padding. A lot of that tend to be shared thanks to copy-on-write, but still. I've recently made improvements in this space (on master the smallest ISeq is now `352B`), but there's some further optimisations I'd like to do that I'm unclear if they're safe or not, mostly because Ruby doesn't have clearly defined limits for various constructs. ### Number of arguments Currently we use `unsigned int` to store the number of arguments a method or proc accepts, so theoretically, you could define a method that takes 4 billion positional arguments, but it's not even clear if attempting to do so would run into some other VM limit and how it would fail. Should we specify a maximum number of arguments a method can take and fail anything larger with a SyntaxError? And if so what should that limit be? I personally think 255 would be more than plenty, but another options is 65536 (2**16). Currently we store between 5 and 9 such arguments counts per Iseq, so 20 or 36B. a 255 limit on the total number of arguments would allow to pack that structure much tighter. ### Line numbers In ISeq we record the line number associated with each instruction, here again we use a full `int`, so theoretically a method could span on 4 billion lines (+/- 2 billions), that seems a bit ridiculous. Should we enforce a limit there as well? Would it be ok to limit ISeqs to `65536` lines? Or is that too small and we should keep the current 2B limit? Alternatively, even a signed 24bit limit (+/-8M lines) could be beneficial. -- 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/