From d3574c117a637a4456aa3ee78e24d8df510b9355 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 18 Sep 2023 14:44:51 -0700 Subject: Move IO#readline to Ruby This commit moves IO#readline to Ruby. In order to call C functions, keyword arguments must be converted to hashes. Prior to this commit, code like `io.readline(chomp: true)` would allocate a hash. This commits moves the keyword "denaturing" to Ruby, allowing us to send positional arguments to the C API and avoiding the hash allocation. Here is an allocation benchmark for the method: ``` x = GC.stat(:total_allocated_objects) File.open("/usr/share/dict/words") do |f| f.readline(chomp: true) until f.eof? end p ALLOCATIONS: GC.stat(:total_allocated_objects) - x ``` Before this commit, the output was this: ``` $ make run ./miniruby -I./lib -I. -I.ext/common -r./arm64-darwin22-fake ./test.rb {:ALLOCATIONS=>707939} ``` Now it is this: ``` $ make run ./miniruby -I./lib -I. -I.ext/common -r./arm64-darwin22-fake ./test.rb {:ALLOCATIONS=>471962} ``` [Bug #19890] [ruby-core:114803] --- internal/vm.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'internal') diff --git a/internal/vm.h b/internal/vm.h index 571cf73c52..d10c14eb4d 100644 --- a/internal/vm.h +++ b/internal/vm.h @@ -58,6 +58,8 @@ VALUE rb_yield_refine_block(VALUE refinement, VALUE refinements); VALUE ruby_vm_special_exception_copy(VALUE); PUREFUNC(st_table *rb_vm_fstring_table(void)); +void rb_lastline_set_up(VALUE val, unsigned int up); + /* vm_eval.c */ VALUE rb_current_realfilepath(void); VALUE rb_check_block_call(VALUE, ID, int, const VALUE *, rb_block_call_func_t, VALUE); -- cgit v1.2.3