summaryrefslogtreecommitdiff
path: root/class.c
AgeCommit message (Collapse)Author
2024-11-20Fix a bug in rb_include_module that stops nested inclusion into module ↵Jeremy Evans
subclasses This bug was present since the code was originally added by me in 3556a834a2847e52162d1d3302d4c64390df1694. Fixes [Bug #20871] Notes: Merged: https://github.com/ruby/ruby/pull/12125
2024-09-19Remove an unused variableTakashi Kokubun
2024-09-19Fix method caching bug when including/prepend module A that prepends module BJeremy Evans
Fix by always adding the generated iclass to the subclasses list, otherwise the method cache for the iclass is not cleared when the method in the module is overwritten. Fixes [Bug #20716] Notes: Merged: https://github.com/ruby/ruby/pull/11582
2024-04-16Eliminate usage of OBJ_FREEZE_RAWJean Boussier
Previously it would bypass the `FL_ABLE` check, but since shapes introduction, it started having a different behavior than `OBJ_FREEZE`, as it would onyl set the `FL_FREEZE` flag, but not update the shape. I have no indication of this causing a bug yet, but it seems like a trap waiting to happen.
2024-03-25YJIT: Propagate Array, Hash, and String classes (#10323)Takashi Kokubun
2024-03-19[DOC] Unify Doxygen formats (#10285)Takashi Kokubun
2024-03-19Implement chilled stringsÉtienne Barrié
[Feature #20205] As a path toward enabling frozen string literals by default in the future, this commit introduce "chilled strings". From a user perspective chilled strings pretend to be frozen, but on the first attempt to mutate them, they lose their frozen status and emit a warning rather than to raise a `FrozenError`. Implementation wise, `rb_compile_option_struct.frozen_string_literal` is no longer a boolean but a tri-state of `enabled/disabled/unset`. When code is compiled with frozen string literals neither explictly enabled or disabled, string literals are compiled with a new `putchilledstring` instruction. This instruction is identical to `putstring` except it marks the String with the `STR_CHILLED (FL_USER3)` and `FL_FREEZE` flags. Chilled strings have the `FL_FREEZE` flag as to minimize the need to check for chilled strings across the codebase, and to improve compatibility with C extensions. Notes: - `String#freeze`: clears the chilled flag. - `String#-@`: acts as if the string was mutable. - `String#+@`: acts as if the string was mutable. - `String#clone`: copies the chilled flag. Co-authored-by: Jean Boussier <[email protected]>
2024-03-06Refactor VM root modulesJean Boussier
This `st_table` is used to both mark and pin classes defined from the C API. But `vm->mark_object_ary` already does both much more efficiently. Currently a Ruby process starts with 252 rooted classes, which uses `7224B` in an `st_table` or `2016B` in an `RArray`. So a baseline of 5kB saved, but since `mark_object_ary` is preallocated with `1024` slots but only use `405` of them, it's a net `7kB` save. `vm->mark_object_ary` is also being refactored. Prior to this changes, `mark_object_ary` was a regular `RArray`, but since this allows for references to be moved, it was marked a second time from `rb_vm_mark()` to pin these objects. This has the detrimental effect of marking these references on every minors even though it's a mostly append only list. But using a custom TypedData we can save from having to mark all the references on minor GC runs. Addtionally, immediate values are now ignored and not appended to `vm->mark_object_ary` as it's just wasted space.
2024-03-06Move FL_SINGLETON to FL_USER1Jean Boussier
This frees FL_USER0 on both T_MODULE and T_CLASS. Note: prior to this, FL_SINGLETON was never set on T_MODULE, so checking for `FL_SINGLETON` without first checking that `FL_TYPE` was `T_CLASS` was valid. That's no longer the case.
2024-03-06[DOC] Add comment about RUBY_FL_SINGLETONPeter Zhu
2024-03-06[DOC] Fix indentation in comment in class.cPeter Zhu
2024-03-01Don't pin named structs defined in RubyJean Boussier
[Bug #20311] `rb_define_class_under` assumes it's called from C and that the reference might be held in a C global variable, so it adds the class to the VM root. In the case of `Struct.new('Name')` it's wasteful and make the struct immortal.
2024-02-15Do not include a backtick in error messages and backtracesYusuke Endoh
[Feature #16495]
2023-12-19Set m_tbl right after allocationPeter Zhu
We should set the m_tbl right after allocation before anything that can trigger GC to avoid clone_p from becoming old and needing to fire write barriers. Co-authored-by: Aaron Patterson <[email protected]>
2023-09-23[Bug #19901]Adam Hess
fix leak in module clone Co-authored-by: Peter Zhu <[email protected]>
2023-09-19Stop exposing FrozenCore in headersNobuyoshi Nakada
Revert commit "Directly allocate FrozenCore as an ICLASS", 813a5f4fc46a24ca1695d23c159250b9e1080ac7.
2023-07-13Store object age in a bitmapMatt Valentine-House
Closes [Feature #19729] Previously 2 bits of the flags on each RVALUE are reserved to store the number of GC cycles that each object has survived. This commit introduces a new bit array on the heap page, called age_bits, to store that information instead. This patch still reserves one of the age bits in the flags (the old FL_PROMOTED0 bit, now renamed FL_PROMOTED). This is set to 0 for young objects and 1 for old objects, and is used as a performance optimisation for the write barrier. Fetching the age_bits from the heap page and doing the required math to calculate if the object was old or not would slow down the write barrier. So we keep this bit synced in the flags for fast access. Notes: Merged: https://github.com/ruby/ruby/pull/7938
2023-06-23Prefer `0` over `NULL` as function pointersNobuyoshi Nakada
SunC warns use of `NULL`, pointer to data as function pointers. Notes: Merged: https://github.com/ruby/ruby/pull/7972 Merged-By: nobu <[email protected]>
2023-06-14Directly allocate FrozenCore as an ICLASSPeter Zhu
It's a bad idea to overwrite the flags as the garbage collector may have set other flags. Notes: Merged: https://github.com/ruby/ruby/pull/7940
2023-06-05Revert "Revert "Fix cvar caching when class is cloned""eileencodes
This reverts commit 10621f7cb9a0c70e568f89cce47a02e878af6778. This was reverted because the gc integrity build started failing. We have figured out a fix so I'm reopening the PR. Original commit message: Fix cvar caching when class is cloned The class variable cache that was added in ruby#4544 changed the behavior of class variables on cloned classes. As reported when a class is cloned AND a class variable was set, and the class variable was read from the original class, reading a class variable from the cloned class would return the value from the original class. This was happening because the IC (inline cache) is stored on the ISEQ which is shared between the original and cloned class, therefore they share the cache too. To fix this we are now storing the `cref` in the cache so that we can check if it's equal to the current `cref`. If it's different we don't want to read from the cache. If it's the same we do. Cloned classes don't share the same cref with their original class. This will need to be backported to 3.1 in addition to 3.2 since the bug exists in both versions. We also added a marking function which was missing. Fixes [Bug #19379] Co-authored-by: Aaron Patterson <[email protected]> Notes: Merged: https://github.com/ruby/ruby/pull/7900
2023-06-01Revert "Fix cvar caching when class is cloned"Aaron Patterson
This reverts commit 77d1b082470790c17c24a2f406b4fec5d522636b.
2023-06-01Fix cvar caching when class is clonedeileencodes
The class variable cache that was added in https://github.com/ruby/ruby/pull/4544 changed the behavior of class variables on cloned classes. As reported when a class is cloned AND a class variable was set, and the class variable was read from the original class, reading a class variable from the cloned class would return the value from the original class. This was happening because the IC (inline cache) is stored on the ISEQ which is shared between the original and cloned class, therefore they share the cache too. To fix this we are now storing the `cref` in the cache so that we can check if it's equal to the current `cref`. If it's different we don't want to read from the cache. If it's the same we do. Cloned classes don't share the same cref with their original class. This will need to be backported to 3.1 in addition to 3.2 since the bug exists in both versions. We also added a marking function which was missing. Fixes [Bug #19379] Co-authored-by: Aaron Patterson <[email protected]> Notes: Merged: https://github.com/ruby/ruby/pull/7265
2023-04-20[DOC] Documentation for flags of RClassPeter Zhu
Notes: Merged: https://github.com/ruby/ruby/pull/7734
2023-04-16Make classes embedded on 32 bitPeter Zhu
Classes are now exactly 80 bytes when embedded, which perfectly fits the 3rd size pool on 32 bit systems. Notes: Merged: https://github.com/ruby/ruby/pull/7719
2023-04-16Change RMODULE_ALLOCATED_BUT_NOT_INITIALIZEDPeter Zhu
RCLASS_CLONED no longer uses FL_USER1, so we can make RMODULE_ALLOCATED_BUT_NOT_INITIALIZED use FL_USER1. Notes: Merged: https://github.com/ruby/ruby/pull/7719
2023-04-16Move RCLASS_CLONED to rb_classext_structPeter Zhu
This commit moves RCLASS_CLONED from the flags to the rb_classext_struct. This frees the FL_USER1 bit. Notes: Merged: https://github.com/ruby/ruby/pull/7719
2023-04-06[Feature #19474] Refactor NEWOBJ macrosMatt Valentine-House
NEWOBJ_OF is now our canonical newobj macro. It takes an optional ec Notes: Merged: https://github.com/ruby/ruby/pull/7393
2023-03-08Adjust styles [ci skip]Nobuyoshi Nakada
2023-03-06Stop exporting symbols for MJITTakashi Kokubun
Notes: Merged: https://github.com/ruby/ruby/pull/7459
2023-02-16Move `attached_object` into `rb_classext_struct`Jean Boussier
Given that signleton classes don't have an allocator, we can re-use these bytes to store the attached object in `rb_classext_struct` without making it larger. Notes: Merged: https://github.com/ruby/ruby/pull/7309
2023-02-15Encapsulate RCLASS_ATTACHED_OBJECTJean Boussier
Right now the attached object is stored as an instance variable and all the call sites that either get or set it have to know how it's stored. It's preferable to hide this implementation detail behind accessors so that it is easier to change how it's stored. Notes: Merged: https://github.com/ruby/ruby/pull/7308
2023-02-09Copy cvar table on cloneeileencodes
When a class with a class variable is cloned we need to also copy the cvar cache table from the original table to the clone. I found this bug while working on fixing [Bug #19379]. While this does not fix that bug directly it is still a required change to fix another bug revealed by the fix in https://github.com/ruby/ruby/pull/7265 This needs to be backported to 3.2.x and 3.1.x. Co-authored-by: Aaron Patterson <[email protected]> Notes: Merged: https://github.com/ruby/ruby/pull/7275
2023-01-04[DOC] Move the internal document for `Init_class_hierarchy`Nobuyoshi Nakada
It has hidden the document for `Object` class. Notes: Merged: https://github.com/ruby/ruby/pull/7057
2022-12-11[DOC] Clarify Class#subclases behavior quirkszverok
As per discussion in [Feature #18273], explain the non-deterministic nature of the method. Notes: Merged: https://github.com/ruby/ruby/pull/5480
2022-12-08Freeze singleton class, not its originAlan Wu
Previously, when we froze an object, we froze `RCLASS_ORIGIN(object.singleton_class)`, which didn't freeze `object.singleton_class` when it has some prepended modules. Origin iclass are internal objects and users can't interact with them through Kernel#freeze?, Kernel#freeze, or any mutation method that checks the frozen status. So we shouldn't touch the origin iclasses when the frozen status should be visible. [Bug #19169] Notes: Merged: https://github.com/ruby/ruby/pull/6869
2022-12-01Inherit max_iv_count from superclassJohn Hawthorn
In 274870bd5434ab64ac3a3c9db9aa27d262c1d6d6 we gained the ability to make an educated guess at the max_iv_count of a class based on its initialize method. This commit makes subclasses inherit their super's max_iv_count, which makes the estimate work in cases that the subclass does not have an initialize method. Notes: Merged: https://github.com/ruby/ruby/pull/6800
2022-11-16Using UNDEF_P macroS-H-GAMELINKS
Notes: Merged: https://github.com/ruby/ruby/pull/6721
2022-11-10Transition shape when object's capacity changesJemma Issroff
This commit adds a `capacity` field to shapes, and adds shape transitions whenever an object's capacity changes. Objects which are allocated out of a bigger size pool will also make a transition from the root shape to the shape with the correct capacity for their size pool when they are allocated. This commit will allow us to remove numiv from objects completely, and will also mean we can guarantee that if two objects share shapes, their IVs are in the same positions (an embedded and extended object cannot share shapes). This will enable us to implement ivar sets in YJIT using object shapes. Co-Authored-By: Aaron Patterson <[email protected]> Notes: Merged: https://github.com/ruby/ruby/pull/6699
2022-10-31Implement object shapes for T_CLASS and T_MODULE (#6637)John Hawthorn
* Avoid RCLASS_IV_TBL in marshal.c * Avoid RCLASS_IV_TBL for class names * Avoid RCLASS_IV_TBL for autoload * Avoid RCLASS_IV_TBL for class variables * Avoid copying RCLASS_IV_TBL onto ICLASSes * Use object shapes for Class and Module IVs Notes: Merged-By: jhawthorn <[email protected]>
2022-10-21Remove unused class serialJemma Issroff
Before object shapes, we were using class serial to invalidate inline caches. Now that we use shape_id for inline cache keys, the class serial is unnecessary. Co-Authored-By: Aaron Patterson <[email protected]> Notes: Merged: https://github.com/ruby/ruby/pull/6605
2022-10-20Add Class#attached_objectUfuk Kayserilioglu
Implements [Feature #12084] Returns the object for which the receiver is the singleton class, or raises TypeError if the receiver is not a singleton class. Notes: Merged: https://github.com/ruby/ruby/pull/6450
2022-10-14Reuse ins_methods_type_i functionS-H-GAMELINKS
Notes: Merged: https://github.com/ruby/ruby/pull/6534
2022-09-23Remove reference to __classid__John Hawthorn
This used to be used for module names but its uses were removed in b00f280d4b9569e7153365d7e1c522b3d6b3c6cf. Notes: Merged: https://github.com/ruby/ruby/pull/6427
2022-08-24Update Module#instance_methods documentation for visibility changes/aliasesJeremy Evans
Requested by matz in comment on #18435.
2022-07-26Rename rb_ary_tmp_new to rb_ary_hidden_newPeter Zhu
rb_ary_tmp_new suggests that the array is temporary in some way, but that's not true, it just creates an array that's hidden and not on the transient heap. This commit renames it to rb_ary_hidden_new. Notes: Merged: https://github.com/ruby/ruby/pull/6180
2022-07-21Expand tabs [ci skip]Takashi Kokubun
[Misc #18891] Notes: Merged: https://github.com/ruby/ruby/pull/6094
2022-06-06Add Module#undefined_instance_methodsJeremy Evans
Implements [Feature #12655] Co-authored-by: Nobuyoshi Nakada <[email protected]> Notes: Merged: https://github.com/ruby/ruby/pull/5733 Merged-By: jeremyevans <[email protected]>
2022-05-26Remove unused RMODULE_INCLUDED_INTO_REFINEMENT flagJemma Issroff
Notes: Merged: https://github.com/ruby/ruby/pull/5932
2022-05-23Remove unnecessary module flag, add module assertions to other module flagsJemma Issroff
Notes: Merged: https://github.com/ruby/ruby/pull/5930
2022-05-05Fix potential GC issue while iterating over weak refsAlan Wu
While walking over the list of subclasses for `include` and friends, we check whether the subclass is a garbage object. After the check, we allocate objects which might trigger GC and make the subclass garbage, even though before the allocation the subclass was not garbage. This is a sort of time-of-check-time-of-use issue. Fix this by saving the weak reference to a local variable, upgrading it to a strong reference while we do the allocation. It makes the code look slightly nicer even if it doesn't fix any runtime issues. Notes: Merged: https://github.com/ruby/ruby/pull/5890