aboutsummaryrefslogtreecommitdiffstats
path: root/internal
Commit message (Collapse)AuthorAgeFilesLines
* Free everything at shutdownAdam Hess2023-12-077-0/+21
| | | | | | | when the RUBY_FREE_ON_SHUTDOWN environment variable is set, manually free memory at shutdown. Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org> Co-authored-by: Peter Zhu <peter@peterzhu.ca>
* Re-embed when removing Object instance variablesPeter Zhu2023-12-061-0/+1
| | | | | | | | | | | Objects with the same shape must always have the same "embeddedness" (either embedded or heap allocated) because YJIT assumes so. However, using remove_instance_variable, it's possible that some objects are embedded and some are heap allocated because it does not re-embed heap allocated objects. This commit changes remove_instance_variable to re-embed Object instance variables when it becomes small enough.
* Pin embedded shared stringsPeter Zhu2023-12-011-1/+0
| | | | | | | Embedded shared strings cannot be moved because strings point into the slot of the shared string. There may be code using the RSTRING_PTR on the stack, which would pin the string but not pin the shared string, causing it to move.
* Fix compaction for generic ivarsPeter Zhu2023-11-242-1/+4
| | | | | | When generic instance variable has a shape, it is marked movable. If it it transitions to too complex, it needs to update references otherwise it may have incorrect references.
* Don't try compacting ivars on Classes that are "too complex"Aaron Patterson2023-11-201-0/+39
| | | | | Too complex classes use a hash table to store ivs, and should always pin their IVs. We shouldn't touch those classes in compaction.
* Refactor rb_obj_evacuate_ivs_to_hash_tableJean Boussier2023-11-171-1/+2
| | | | | | | | | | | | | | | | | | That function is a bit too low level to called from multiple places. It's always used in tandem with `rb_shape_set_too_complex` and both have to know how the object is laid out to update the `iv_ptr`. So instead we can provide two higher level function: - `rb_obj_copy_ivs_to_hash_table` to prepare a `st_table` from an arbitrary oject. - `rb_obj_convert_to_too_complex` to assign the new `st_table` to the old object, and safely free the old `iv_ptr`. Unfortunately both can't be combined into one, because `rb_obj_copy_ivar` need `rb_obj_copy_ivs_to_hash_table` to copy from one object to another.
* rb_evict_ivars_to_hash: get rid of the sahpe paramaterJean Boussier2023-11-161-1/+1
| | | | | | | | | | It's only used to allocate the table with the right size, but in some case we were passing `rb_shape_get_shape_by_id(SHAPE_OBJ_TOO_COMPLEX)` which `next_iv_index` is a bit undefined. So overall we're better to just allocate a table the size of the existing object, it should be close enough in the vast majority of cases, and that's already a de-optimizaton path anyway.
* Refactor rb_shape_transition_shape_capa outJean Boussier2023-11-081-1/+0
| | | | | | | | | | | | | | | | Right now the `rb_shape_get_next` shape caller need to first check if there is capacity left, and if not call `rb_shape_transition_shape_capa` before it can call `rb_shape_get_next`. And on each of these it needs to checks if we got a TOO_COMPLEX back. All this logic is duplicated in the interpreter, YJIT and RJIT. Instead we can have `rb_shape_get_next` do the capacity transition when needed. The caller can compare the old and new shapes capacity to know if resizing is needed. It also can check for TOO_COMPLEX only once.
* Export functions used for builtinsNobuyoshi Nakada2023-11-081-2/+2
|
* Suppress array-bounds warnings from gcc 13Nobuyoshi Nakada2023-11-071-1/+8
|
* Use shape capacity transitions for generic ivarsPeter Zhu2023-11-031-1/+0
| | | | | This commit changes generic ivars to respect the capacity transition in shapes rather than growing the capacity independently.
* geniv objects can become too complexAaron Patterson2023-10-241-0/+1
|
* rb_shape_transition_shape_capa: use optimal sizes transitionsJean Boussier2023-10-231-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Previously the growth was 3(embed), 6, 12, 24, ... With this change it's now 3(embed), 8, 16, 32, 64, ... by default. However, since power of two isn't the best size for all allocators, if `malloc_usable_size` is vailable, we use it to discover the best offset. On Linux/glibc 2.35 for instance, the growth will be 3(embed), 7, 15, 31 to avoid wasting 8B per object. Test program: ```c size_t test(size_t slots) { size_t allocated = slots * VALUE_SIZE; void *test_ptr = malloc(allocated); size_t wasted = malloc_usable_size(test_ptr) - allocated; free(test_ptr); fprintf(stderr, "slots = %lu, wasted_bytes = %lu\n", slots, wasted); return wasted; } int main(int argc, char *argv[]) { size_t best_padding = 0; size_t padding = 0; for (padding = 0; padding <= 2; padding++) { size_t wasted = test(8 - padding); if (wasted == 0) { best_padding = padding; break; } } size_t index = 0; fprintf(stderr, "=============== naive ================\n"); size_t list_size = 4; for (index = 0; index < 10; index++) { test(list_size); list_size *= 2; } fprintf(stderr, "=============== auto-padded (-%lu) ================\n", best_padding); list_size = 4; for (index = 0; index < 10; index ++) { test(list_size - best_padding); list_size *= 2; } fprintf(stderr, "\n\n"); return 0; } ``` ``` ===== glibc ====== slots = 8, wasted_bytes = 8 slots = 7, wasted_bytes = 0 =============== naive ================ slots = 4, wasted_bytes = 8 slots = 8, wasted_bytes = 8 slots = 16, wasted_bytes = 8 slots = 32, wasted_bytes = 8 slots = 64, wasted_bytes = 8 slots = 128, wasted_bytes = 8 slots = 256, wasted_bytes = 8 slots = 512, wasted_bytes = 8 slots = 1024, wasted_bytes = 8 slots = 2048, wasted_bytes = 8 =============== auto-padded (-1) ================ slots = 3, wasted_bytes = 0 slots = 7, wasted_bytes = 0 slots = 15, wasted_bytes = 0 slots = 31, wasted_bytes = 0 slots = 63, wasted_bytes = 0 slots = 127, wasted_bytes = 0 slots = 255, wasted_bytes = 0 slots = 511, wasted_bytes = 0 slots = 1023, wasted_bytes = 0 slots = 2047, wasted_bytes = 0 ``` ``` ========== jemalloc ======= slots = 8, wasted_bytes = 0 =============== naive ================ slots = 4, wasted_bytes = 0 slots = 8, wasted_bytes = 0 slots = 16, wasted_bytes = 0 slots = 32, wasted_bytes = 0 slots = 64, wasted_bytes = 0 slots = 128, wasted_bytes = 0 slots = 256, wasted_bytes = 0 slots = 512, wasted_bytes = 0 slots = 1024, wasted_bytes = 0 slots = 2048, wasted_bytes = 0 =============== auto-padded (-0) ================ slots = 4, wasted_bytes = 0 slots = 8, wasted_bytes = 0 slots = 16, wasted_bytes = 0 slots = 32, wasted_bytes = 0 slots = 64, wasted_bytes = 0 slots = 128, wasted_bytes = 0 slots = 256, wasted_bytes = 0 slots = 512, wasted_bytes = 0 slots = 1024, wasted_bytes = 0 slots = 2048, wasted_bytes = 0 ```
* Avoid the pointer hack in RCLASS_EXTYusuke Endoh2023-10-151-1/+6
| | | | | | | | | | | | | | | ... because GCC 13 warns it. ``` In file included from class.c:24: In function ‘RCLASS_SET_ALLOCATOR’, inlined from ‘class_alloc’ at class.c:251:5, inlined from ‘rb_module_s_alloc’ at class.c:1045:17: internal/class.h:159:43: warning: array subscript 0 is outside array bounds of ‘rb_classext_t[0]’ {aka ‘struct rb_classext_struct[]’} [-Warray-bounds=] 159 | RCLASS_EXT(klass)->as.class.allocator = allocator; | ^ ``` https://rubyci.s3.amazonaws.com/arch/ruby-master/log/20231015T030003Z.log.html.gz
* Shorten `rb_strterm_literal_t` membersNobuyoshi Nakada2023-10-141-3/+3
|
* Manage `rb_strterm_t` without imemoNobuyoshi Nakada2023-10-142-2/+0
|
* Remove unions in `rb_strterm` structs for alignmentNobuyoshi Nakada2023-10-141-31/+7
|
* M:N thread scheduler for RactorsKoichi Sasada2023-10-121-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This patch introduce M:N thread scheduler for Ractor system. In general, M:N thread scheduler employs N native threads (OS threads) to manage M user-level threads (Ruby threads in this case). On the Ruby interpreter, 1 native thread is provided for 1 Ractor and all Ruby threads are managed by the native thread. From Ruby 1.9, the interpreter uses 1:1 thread scheduler which means 1 Ruby thread has 1 native thread. M:N scheduler change this strategy. Because of compatibility issue (and stableness issue of the implementation) main Ractor doesn't use M:N scheduler on default. On the other words, threads on the main Ractor will be managed with 1:1 thread scheduler. There are additional settings by environment variables: `RUBY_MN_THREADS=1` enables M:N thread scheduler on the main ractor. Note that non-main ractors use the M:N scheduler without this configuration. With this configuration, single ractor applications run threads on M:1 thread scheduler (green threads, user-level threads). `RUBY_MAX_CPU=n` specifies maximum number of native threads for M:N scheduler (default: 8). This patch will be reverted soon if non-easy issues are found. [Bug #19842]
* Make popcount bit-masks stricterNobuyoshi Nakada2023-10-051-6/+6
| | | | | Each bit run is upto the right shift count, so the each mask does not need more upper bits.
* Move IO#readline to RubyAaron Patterson2023-09-281-0/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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]
* [Feature #19790] Rename BUGREPORT_PATH as CRASH_REPORTNobuyoshi Nakada2023-09-251-1/+1
|
* Add `--bugreport-path` optionNobuyoshi Nakada2023-09-251-0/+2
| | | | It has precedence over the environment variable `RUBY_BUGREPORT_PATH`.
* Dump backtraces to an arbitrary streamNobuyoshi Nakada2023-09-251-2/+2
|
* Add rb_hash_free for the GC to usePeter Zhu2023-09-241-0/+1
|
* Stop exposing FrozenCore in headersNobuyoshi Nakada2023-09-191-2/+1
| | | | | Revert commit "Directly allocate FrozenCore as an ICLASS", 813a5f4fc46a24ca1695d23c159250b9e1080ac7.
* Fix crash in WeakMap during compactionPeter Zhu2023-09-061-0/+11
| | | | | WeakMap can crash during compaction because the st_insert could allocate memory.
* Introduce rb_gc_remove_weakPeter Zhu2023-09-051-0/+1
| | | | | | If we're during incremental marking, then Ruby code can execute that deallocates certain memory buffers that have been called with rb_gc_mark_weak, which can cause use-after-free bugs.
* Use end of char boundary in start_with?John Hawthorn2023-09-011-0/+9
| | | | | | | | | | | | | | | | Previously we used the next character following the found prefix to determine if the match ended on a broken character. This had caused surprising behaviour when a valid character was followed by a UTF-8 continuation byte. This commit changes the behaviour to instead look for the end of the last character in the prefix. [Bug #19784] Co-authored-by: ywenc <ywenc@github.com> Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
* Prevent rb_gc_mark_values from pinning objectsMatt Valentine-House2023-08-311-0/+1
| | | | | | | | | | | | | | | | This is an internal only function not exposed to the C extension API. It's only use so far is from rb_vm_mark, where it's used to mark the values in the vm->trap_list.cmd array. There shouldn't be any reason why these cannot move. This commit allows them to move by updating their references during the reference updating step of compaction. To do this we've introduced another internal function rb_gc_update_values as a partner to rb_gc_mark_values. This allows us to refactor rb_gc_mark_values to not pin
* Introduce `at_char_boundary` functionNobuyoshi Nakada2023-08-261-0/+6
|
* Implement weak references in the GCPeter Zhu2023-08-251-0/+2
| | | | | | | | | | | | [Feature #19783] This commit adds support for weak references in the GC through the function `rb_gc_mark_weak`. Unlike strong references, weak references does not mark the object, but rather lets the GC know that an object refers to another one. If the child object is freed, the pointer from the parent object is overwritten with `Qundef`. Co-Authored-By: Jean Boussier <byroot@ruby-lang.org>
* Move SCRIPT_LINES__ away from parse.yNobuyoshi Nakada2023-08-252-2/+2
|
* do not redefine a typedef卜部昌平2023-08-251-2/+2
| | | | duplicated typedef declaration was not allowed in C99.
* do not redefine a typedef卜部昌平2023-08-251-1/+2
| | | | duplicated typedef declaration was not allowed in C99.
* Check that __builtin_mul_overflow can handle long longJeremy Evans2023-08-241-2/+6
| | | | | | Fixes [Bug #17646] Patch from xtkoba (Tee KOBAYASHI)
* Move the PC regardless of the leaf flag (#8232)Takashi Kokubun2023-08-161-1/+0
| | | Co-authored-by: Alan Wu <alansi.xingwu@shopify.com>
* Fix range of `--backtrace-limit`Nobuyoshi Nakada2023-08-111-1/+1
| | | | Also an option command line should have precedence over `RUBYOPT`.
* Move `posix_signal` declaration internal with prefix `ruby_`Nobuyoshi Nakada2023-07-171-0/+4
|
* Implement Process.warmupJean Boussier2023-07-171-0/+1
| | | | | | | | | | | | [Feature #18885] For now, the optimizations performed are: - Run a major GC - Compact the heap - Promote all surviving objects to oldgen Other optimizations may follow.
* YJIT: Make ratio_in_yjit always available (#8064)Takashi Kokubun2023-07-131-0/+5
|
* Remove unused references to the transient heapPeter Zhu2023-07-132-5/+0
|
* Remove RARRAY_CONST_PTR_TRANSIENTPeter Zhu2023-07-131-2/+2
| | | | RARRAY_CONST_PTR now does the same things as RARRAY_CONST_PTR_TRANSIENT.
* Set backtrace length limit at lastNobuyoshi Nakada2023-07-131-0/+1
| | | | | Command line options should have higher precedence than the same options in shebang and `RUBYOPT`.
* Shrink `ruby_cmdline_options_t` a bitNobuyoshi Nakada2023-07-131-1/+2
|
* [Feature #19730] Remove transient heapPeter Zhu2023-07-133-76/+0
|
* Stop allocating unused backref strings at `defined?`Nobuyoshi Nakada2023-06-271-0/+1
|
* Use `rb_reg_nth_defined` instead of `rb_match_nth_defined`Nobuyoshi Nakada2023-06-271-1/+0
|
* Declare `RHASH_AR_TABLE` and `RHASH_ST_TABLE` return non-nullNobuyoshi Nakada2023-06-231-0/+2
|
* Prefer `0` over `NULL` as function pointersNobuyoshi Nakada2023-06-231-1/+1
| | | SunC warns use of `NULL`, pointer to data as function pointers.
* Allow setting the name of a class or module. (#7483)Samuel Williams2023-06-211-0/+16
| | | | Introduce `Module#set_temporary_name` for setting identifiers for otherwise anonymous modules/classes.