Implementing Core Set
summary
Ruby 4.0 replaces the stdlib Set with a native SET_TABLE-backed implementation that removes per-entry hash values and yields about a 33% memory reduction for large sets. The add?/add method becomes a single C table access under the GVL, fixing a race and improving speed; insertion uses set_insert_WB and RB_OBJECT_WRITTEN write-barriers to keep GC safe. Work copies ~1/3 of hash table code and rewrites the other 2/3 as C methods; compatibility is preserved via a set_subclass_compatible module and an alternate core_set subclass for users who need the faster, non-compat mode.
AI-written summary · about
chapters (14)
- 0:00Introduction
- 1:06Set history and issues
- 3:31New set implementation
- 8:04Reimplementing set methods
- 11:52Set initialization in C
- 14:17Memory optimization
- 16:01Garbage collection support
- 17:03Thread safety fixes
- 19:08String handling
- 20:31Set table usage elsewhere
- 21:13Set subclass compatibility
- 23:58Memory usage for small sets
- 26:47Memory optimization challenges
- 28:11Conclusion and book








