summaryrefslogtreecommitdiff
path: root/internal
AgeCommit message (Collapse)Author
2024-10-11Remove 1 allocation in Enumerable#each_with_index (#11868)Alan Wu
* Remove 1 allocation in Enumerable#each_with_index Previously, each call to Enumerable#each_with_index allocates 2 objects, one for the counting index, the other an imemo_ifunc passed to `self.each` as a block. Use `struct vm_ifunc::data` to hold the counting index directly to remove 1 allocation. * [DOC] Brief summary for usages of `struct vm_ifunc` Notes: Merged-By: maximecb <[email protected]>
2024-10-10Remove duplicate struct declarationAlan Wu
2024-10-08Fix extra semicolon outside of a function in `NO_SANITIZE`Nobuyoshi Nakada
``` internal/sanitizers.h:57:26: error: ISO C does not allow extra ‘;’ outside of a function [-Wpedantic] 57 | COMPILER_WARNING_PUSH; \ | ^ ``` and so many. Remove semicolons following pragma, and repeat the given declaration at the end to consume a semicolon following the macro call. As many `NO_SANITIZE` calls including bigdecimal that is a gem have a trailing semicolon, it was not able to move the semicolon inside `NO_SANITIZE`.
2024-10-08`rb_fix_mul_fix` needs internal/bits.h for `MUL_OVERFLOW_FIXNUM_P`Nobuyoshi Nakada
2024-10-05Introduce `rb_io_blocking_region` which takes `struct rb_io` argument. (#11795)Samuel Williams
This does not change any actual behaviour, but provides a choke point for blocking IO operations. * Update `IO::Buffer` to use `rb_io_blocking_region`. * Update `File` to use `rb_io_blocking_region`. * Update `IO` to use `rb_io_blocking_region`. Notes: Merged-By: ioquatix <[email protected]>
2024-10-03Rename size_pool -> heapMatt Valentine-House
Now that we've inlined the eden_heap into the size_pool, we should rename the size_pool to heap. So that Ruby contains multiple heaps, with different sized objects. The term heap as a collection of memory pages is more in memory management nomenclature, whereas size_pool was a name chosen out of necessity during the development of the Variable Width Allocation features of Ruby. The concept of size pools was introduced in order to facilitate different sized objects (other than the default 40 bytes). They wrapped the eden heap and the tomb heap, and some related state, and provided a reasonably simple way of duplicating all related concerns, to provide multiple pools that all shared the same structure but held different objects. Since then various changes have happend in Ruby's memory layout: * The concept of tomb heaps has been replaced by a global free pages list, with each page having it's slot size reconfigured at the point when it is resurrected * the eden heap has been inlined into the size pool itself, so that now the size pool directly controls the free_pages list, the sweeping page, the compaction cursor and the other state that was previously being managed by the eden heap. Now that there is no need for a heap wrapper, we should refer to the collection of pages containing Ruby objects as a heap again rather than a size pool Notes: Merged: https://github.com/ruby/ruby/pull/11771
2024-10-02Make default parser enum and define getter/setterNobuyoshi Nakada
Notes: Merged: https://github.com/ruby/ruby/pull/11761
2024-09-25Fix memory leak in Ripper for indented heredocsPeter Zhu
The allocated parser string is never freed, which causes a memory leak. The following code leaks memory: Ripper.sexp_raw(DATA.read) __END__ <<~EOF a #{1} a EOF Notes: Merged: https://github.com/ruby/ruby/pull/11669
2024-09-23Unpoison page->freelist before trying to assert on itKJ Tsanaktsidis
Otherwise trying to deref the pointer can cause an ASAN crash, even though the only reason we're dereferencing it is so that we can assert on it. Notes: Merged: https://github.com/ruby/ruby/pull/11660
2024-09-22Reuse dedent_string function in rb_ruby_ripper_dedent_string functionS-H-GAMELINKS
This change is reduce Ruby C API dependency for Universal Parser. Reuse dedent_string functions in rb_ruby_ripper_dedent_string functions and remove dependencies on rb_str_modify and rb_str_set_len from the parser. Notes: Merged: https://github.com/ruby/ruby/pull/11658
2024-09-17Ensure fiber scheduler is woken up when close interrupts readKJ Tsanaktsidis
If one thread is reading and another closes that socket, the close blocks waiting for the read to abort cleanly. This ensures that Ruby is totally done with the file descriptor _BEFORE_ we tell the OS to close and potentially re-use it. When the read is correctly terminated, the close should be unblocked. That currently works if closing is happening on a thread, but if it's happening on a fiber with a fiber scheduler, it does NOT work. This patch ensures that if the close happened in a fiber scheduled thread, that the scheduler is notified that the fiber is unblocked. [Bug #20723] Notes: Merged: https://github.com/ruby/ruby/pull/11614
2024-09-16Don't export unnecessary string functionsPeter Zhu
These functions are not used publicly, so we don't need to export them. Notes: Merged: https://github.com/ruby/ruby/pull/11634
2024-09-12Switch the default parser from parse.y to PrismKevin Newton
This commit switches the default parser to Prism. There are a couple of additional changes related to this that are a part of this as well to make this happen. * Switch the default parser in parse.h * Remove the Prism-specific workflow and add a parse.y-specific workflow to CI so that it continues to be tested * Update a few test exclusions since Prism has the correct behavior but parse.y doesn't per https://bugs.ruby-lang.org/issues/20504. * Skips a couple of tests on RBS which are failing because they are using RubyVM::AbstractSyntaxTree.of. Fixes [Feature #20564] Notes: Merged: https://github.com/ruby/ruby/pull/11497
2024-09-05Optimized instruction for Hash#freezeÉtienne Barrié
If a Hash which is empty or only using literals is frozen, we detect this as a peephole optimization and change the instructions to be `opt_hash_freeze`. [Feature #20684] Co-authored-by: Jean Boussier <[email protected]> Notes: Merged: https://github.com/ruby/ruby/pull/11406
2024-09-05Optimized instruction for Array#freezeÉtienne Barrié
If an Array which is empty or only using literals is frozen, we detect this as a peephole optimization and change the instructions to be `opt_ary_freeze`. [Feature #20684] Co-authored-by: Jean Boussier <[email protected]> Notes: Merged: https://github.com/ruby/ruby/pull/11406
2024-09-05Proof of Concept: Allow to prevent fork from happening in known fork unsafe APIJean Boussier
[Feature #20590] For better of for worse, fork(2) remain the primary provider of parallelism in Ruby programs. Even though it's frowned uppon in many circles, and a lot of literature will simply state that only async-signal safe APIs are safe to use after `fork()`, in practice most APIs work well as long as you are careful about not forking while another thread is holding a pthread mutex. One of the APIs that is known cause fork safety issues is `getaddrinfo`. If you fork while another thread is inside `getaddrinfo`, a mutex may be left locked in the child, with no way to unlock it. I think we could reduce the impact of these problem by preventing in for the most notorious and common cases, by locking around `fork(2)` and known unsafe APIs with a read-write lock. Notes: Merged: https://github.com/ruby/ruby/pull/10864
2024-08-27Add RB_DEFAULT_PARSER preprocessor macroAlan Wu
This way there is one place to change for switching the default. This also allows for building the same commit with different cppflags. Notes: Merged: https://github.com/ruby/ruby/pull/11473 Merged-By: XrXr
2024-08-07Delete unused rb_check_funcall_with_hook()Alan Wu
Notes: Merged: https://github.com/ruby/ruby/pull/11331
2024-07-10Add rb_block_call2, a flexible variant of rb_block_callYusuke Endoh
This function accepts flags: RB_NO_KEYWORDS, RB_PASS_KEYWORDS, RB_PASS_CALLED_KEYWORDS: Works as the same as rb_block_call_kw. RB_BLOCK_NO_USE_PACKED_ARGS: The given block ("bl_proc") does not use "yielded_arg" of rb_block_call_func_t. Instead, the block accesses the yielded arguments via "argc" and "argv". This flag allows the called method to yield arguments without allocating an Array.
2024-07-03Move ruby_load_external_gc_from_argv to gc.hPeter Zhu
2024-07-03[Feature #20470] Split GC into gc_impl.cPeter Zhu
This commit splits gc.c into two files: - gc.c now only contains code not specific to Ruby GC. This includes code to mark objects (which the GC implementation may choose not to use) and wrappers for internal APIs that the implementation may need to use (e.g. locking the VM). - gc_impl.c now contains the implementation of Ruby's GC. This includes marking, sweeping, compaction, and statistics. Most importantly, gc_impl.c only uses public APIs in Ruby and a limited set of functions exposed in gc.c. This allows us to build gc_impl.c independently of Ruby and plug Ruby's GC into itself.
2024-05-31Revert 528c4501f46fbe1e06028d673a777ef124d29829Yusuke Endoh
Recently, `TestRubyLiteral#test_float` fails randomly. ``` 1) Error: TestRubyLiteral#test_float: ArgumentError: SyntaxError#path changed: "(eval at /home/chkbuild/chkbuild/tmp/build/20240527T050036Z/ruby/test/ruby/test_literal.rb:642)"->"(eval at /home/chkbuild/chkbuild/tmp/build/20240527T050036Z/ruby/test/ruby/test_literal.rb:642)" ``` https://rubyci.s3.amazonaws.com/s390x/ruby-master/log/20240527T050036Z.fail.html.gz According to Launchable, the first failure was on Apr 30. This is just when 528c4501f46fbe1e06028d673a777ef124d29829 was committed. I don't know if the change is really the cause, but I want to revert it once to see if the random failure disappears.
2024-05-28Precompute embedded string literals hash codeJean Boussier
With embedded strings we often have some space left in the slot, which we can use to store the string Hash code. It's probably only worth it for string literals, as they are the ones likely to be used as hash keys. We chose to store the Hash code right after the string terminator as to make it easy/fast to compute, and not require one more union in RString. ``` compare-ruby: ruby 3.4.0dev (2024-04-22T06:32:21Z main f77618c1fa) [arm64-darwin23] built-ruby: ruby 3.4.0dev (2024-04-22T10:13:03Z interned-string-ha.. 8a1a32331b) [arm64-darwin23] last_commit=Precompute embedded string literals hash code | |compare-ruby|built-ruby| |:-----------|-----------:|---------:| |symbol | 39.275M| 39.753M| | | -| 1.01x| |dyn_symbol | 37.348M| 37.704M| | | -| 1.01x| |small_lit | 29.514M| 33.948M| | | -| 1.15x| |frozen_lit | 27.180M| 33.056M| | | -| 1.22x| |iseq_lit | 27.391M| 32.242M| | | -| 1.18x| ``` Co-Authored-By: Étienne Barrié <[email protected]>
2024-05-28Stop marking chilled strings as frozenÉtienne Barrié
They were initially made frozen to avoid false positives for cases such as: str = str.dup if str.frozen? But this may cause bugs and is generally confusing for users. [Feature #20205] Co-authored-by: Jean Boussier <[email protected]>
2024-05-23Introduce a specialize instruction for Array#packNobuyoshi Nakada
Instructions for this code: ```ruby # frozen_string_literal: true [a].pack("C") ``` Before this commit: ``` == disasm: #<ISeq:<main>@test.rb:1 (1,0)-(3,13)> 0000 putself ( 3)[Li] 0001 opt_send_without_block <calldata!mid:a, argc:0, FCALL|VCALL|ARGS_SIMPLE> 0003 newarray 1 0005 putobject "C" 0007 opt_send_without_block <calldata!mid:pack, argc:1, ARGS_SIMPLE> 0009 leave ``` After this commit: ``` == disasm: #<ISeq:<main>@test.rb:1 (1,0)-(3,13)> 0000 putself ( 3)[Li] 0001 opt_send_without_block <calldata!mid:a, argc:0, FCALL|VCALL|ARGS_SIMPLE> 0003 putobject "C" 0005 opt_newarray_send 2, :pack 0008 leave ``` Co-authored-by: Maxime Chevalier-Boisvert <[email protected]> Co-authored-by: Aaron Patterson <[email protected]>
2024-05-19`rb_enc_compile_warn` and `rb_enc_compile_warning` are printf formatNobuyoshi Nakada
2024-05-13Constify encoding type in universal parserNobuyoshi Nakada
Fixed warning about discarding modifiers. ``` ../src/ruby_parser.c:677:48: warning: passing 'rb_encoding *' (aka 'const struct OnigEncodingTypeST *') to parameter of type 'void *' discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers] 677 | ast = rb_parser_compile(p, gets, ptr, len, enc, input, line); | ^~~ ../src/internal/parse.h:58:128: note: passing argument to parameter 'fname_enc' here 58 | rb_ast_t *rb_parser_compile(rb_parser_t *p, rb_parser_lex_gets_func *gets, const char *fname_ptr, long fname_len, rb_encoding *fname_enc, rb_parser_input_data input, int line); | ^ ```
2024-05-04Change return value of `gets` function to be `rb_parser_string_t *` instead ↵yui-knk
of `VALUE` This change reduces parser's dependency on ruby object.
2024-05-03Rename `vast` to `ast_value`yui-knk
There is an English word "vast". This commit changes the name to be more clear name to avoid confusion.
2024-04-30Use `rb_parser_string_t *` as `ruby_sourcefile_string`yui-knk
This reduces dependency on VALUE.
2024-04-27Add line_count field to rb_ast_body_tHASUMI Hitoshi
This patch adds `int line_count` field to `rb_ast_body_t` structure. Instead, we no longer cast `script_lines` to Fixnum. ## Background Ref https://github.com/ruby/ruby/pull/10618 In the PR above, we have decoupled IMEMO from `rb_ast_t`. This means we could lift the five-words-restriction of the structure that forced us to unionize `rb_ast_t *` and `FIXNUM` in one field. ## Relating refactor - Remove the second parameter of `rb_ruby_ast_new()` function ## Attention I will remove a code that assigns -1 to line_count, in `rb_binding_add_dynavars()` of vm.c, because I don't think it is necessary. But I will make another PR for this so that we can atomically revert in case I was wrong (See the comment on the code)
2024-04-26Set `SCRIPT_LINES__` outside of parseryui-knk
Parser should not depend on functions defiend on "ruby_parser.c".
2024-04-26[Universal parser] Decouple IMEMO from rb_ast_tHASUMI Hitoshi
This patch removes the `VALUE flags` member from the `rb_ast_t` structure making `rb_ast_t` no longer an IMEMO object. ## Background We are trying to make the Ruby parser generated from parse.y a universal parser that can be used by other implementations such as mruby. To achieve this, it is necessary to exclude VALUE and IMEMO from parse.y, AST, and NODE. ## Summary (file by file) - `rubyparser.h` - Remove the `VALUE flags` member from `rb_ast_t` - `ruby_parser.c` and `internal/ruby_parser.h` - Use TypedData_Make_Struct VALUE which wraps `rb_ast_t` `in ast_alloc()` so that GC can manage it - You can retrieve `rb_ast_t` from the VALUE by `rb_ruby_ast_data_get()` - Change the return type of `rb_parser_compile_XXXX()` functions from `rb_ast_t *` to `VALUE` - rb_ruby_ast_new() which internally `calls ast_alloc()` is to create VALUE vast outside ruby_parser.c - `iseq.c` and `vm_core.h` - Amend the first parameter of `rb_iseq_new_XXXX()` functions from `rb_ast_body_t *` to `VALUE` - This keeps the VALUE of AST on the machine stack to prevent being removed by GC - `ast.c` - Almost all change is replacement `rb_ast_t *ast` with `VALUE vast` (sorry for the big diff) - Fix `node_memsize()` - Now it includes `rb_ast_local_table_link`, `tokens` and script_lines - `compile.c`, `load.c`, `node.c`, `parse.y`, `proc.c`, `ruby.c`, `template/prelude.c.tmpl`, `vm.c` and `vm_eval.c` - Follow-up due to the above changes - `imemo.{c|h}` - If an object with `imemo_ast` appears, considers it a bug Co-authored-by: Nobuyoshi Nakada <[email protected]>
2024-04-24Add ruby_mimcallocPeter Zhu
Many places call ruby_mimmalloc then MEMZERO. This can be reduced by using ruby_mimcalloc instead.
2024-04-23Move encoding object conversion outside of parseryui-knk
Reduce the parser's dependence on `VALUE` and `rb_enc_from_encoding`.
2024-04-23Refactor parser compile functionsyui-knk
Refactor parser compile functions to reduce the dependence on ruby functions. This commit includes these changes 1. Refactor `gets`, `input` and `gets_` of `parser_params` Parser needs two different data structure to get next line, function (`gets`) and input data (`input`). However `gets_` is used for both function (`call`) and input data (`ptr`). `call` is used for managing general callback function when `rb_ruby_parser_compile_generic` is used. `ptr` is used for managing the current pointer on String when `parser_compile_string` is used. This commit changes parser to used only `gets` and `input` then removes `gets_`. 2. Move parser_compile functions and `gets` functions from parse.y to ruby_parser.c This change reduces the dependence on ruby functions from parser. 3. Change ruby_parser and ripper to take care of `VALUE input` GC mark Move the responsibility of calling `rb_gc_mark` for `VALUE input` from parser to ruby_parser and ripper. `input` is arbitrary data pointer from the viewpoint of parser. 4. Introduce rb_parser_compile_array function Caller of `rb_parser_compile_generic` needs to take care about GC because ruby_parser doesn’t know about the detail of `lex_gets` and `input`. Introduce `rb_parser_compile_array` to reduce the complexity of ast.c.
2024-04-20Remove unused functionyui-knk
2024-04-20Parser and universal parser share wrapper functionsyui-knk
2024-04-18Remove unused rb_size_pool_slot_sizePeter Zhu
2024-04-18Add a hint of `ASCII-8BIT` being `BINARY`Jean Boussier
[Feature #18576] Since outright renaming `ASCII-8BIT` is deemed to backward incompatible, the next best thing would be to only change its `#inspect`, particularly in exception messages.
2024-04-15Initialize external GC LibraryMatt Valentine-House
Co-Authored-By: Peter Zhu <[email protected]>
2024-04-15[Universal parser] DeVALUE of p->debug_lines and ast->body.script_linesHASUMI Hitoshi
This patch is part of universal parser work. ## Summary - Decouple VALUE from members below: - `(struct parser_params *)->debug_lines` - `(rb_ast_t *)->body.script_lines` - Instead, they are now `rb_parser_ary_t *` - They can also be a `(VALUE)FIXNUM` as before to hold line count - `ISEQ_BODY(iseq)->variable.script_lines` remains VALUE - In order to do this, - Add `VALUE script_lines` param to `rb_iseq_new_with_opt()` - Introduce `rb_parser_build_script_lines_from()` to convert `rb_parser_ary_t *` into `VALUE` ## Other details - Extend `rb_parser_ary_t *`. It previously could only store `rb_parser_ast_token *`, now can store script_lines, too - Change tactics of building the top-level `SCRIPT_LINES__` in `yycompile0()` - Before: While parsing, each line of the script is added to `SCRIPT_LINES__[path]` - After: After `yyparse(p)`, `SCRIPT_LINES__[path]` will be built from `p->debug_lines` - Remove the second parameter of `rb_parser_set_script_lines()` to make it simple - Introduce `script_lines_free()` to be called from `rb_ast_free()` because the GC no longer takes care of the script_lines - Introduce `rb_parser_string_deep_copy()` in parse.y to maintain script_lines when `rb_ruby_parser_free()` called - With regard to this, please see *Future tasks* below ## Future tasks - Decouple IMEMO from `rb_ast_t *` - This lifts the five-members-restriction of Ruby object, - So we will be able to move the ownership of the `lex.string_buffer` from parser to AST - Then we remove `rb_parser_string_deep_copy()` to make the whole thing simple
2024-04-15Emit `warn` event for duplicated hash keys on ripperyui-knk
Need to use `rb_warn` macro instead of calling `rb_compile_warn` directly to emit `warn` event on ripper.
2024-04-11compile.c: use rb_enc_interned_str to reduce allocationsJean Boussier
The `rb_fstring(rb_enc_str_new())` pattern is inneficient because: - It passes a mutable string to `rb_fstring` so if it has to be interned it will first be duped. - It an equivalent interned string already exists, we allocated the string for nothing. With `rb_enc_interned_str` we either directly get the pre-existing string with 0 allocations, or efficiently directly intern the one we create without first duping it.
2024-04-07Revert "Enumerator should use a non-blocking fiber. (#10478)" (#10480)Samuel Williams
This reverts commit dfa0897de89251a631a67460b941cd24a14c9b55. This commit accidentally included some change in `parse.h`. Reverting and re-applying the relevant changes.
2024-04-07Enumerator should use a non-blocking fiber. (#10478)Samuel Williams
2024-04-07Remove undefined function's prototype declarationyui-knk
2024-04-07Fix ripper to dispatch warning event for duplicated when clauseyui-knk
Need to separate `check_literal_when` function for parser and ripper otherwise warning event is not dispatched because parser `rb_warning1` is used in ripper.
2024-04-05[Bug #20342] Consider wrapped load in `main` methodsNobuyoshi Nakada
2024-04-04Merge rb_objspace_alloc and Init_heap.Matt Valentine-House
Co-Authored-By: Peter Zhu <[email protected]>