summaryrefslogtreecommitdiff
path: root/benchmark
diff options
context:
space:
mode:
authorJohn Hawthorn <john@hawthorn.email>2022-01-25 19:16:57 -0800
committerJohn Hawthorn <john@hawthorn.email>2022-02-23 19:57:42 -0800
commitb13a7c8e36e9b00b5c6668846f31be4e25523111 (patch)
tree9de58995b2e66027b83cf13aeacc3eb781ddd848 /benchmark
parent764e4fa850de749790e5ed11c8a4ab86a4499ac0 (diff)
downloadruby-b13a7c8e36e9b00b5c6668846f31be4e25523111.tar.gz
Constant time class to class ancestor lookup
Previously when checking ancestors, we would walk all the way up the ancestry chain checking each parent for a matching class or module. I believe this was especially unfriendly to CPU cache since for each step we need to check two cache lines (the class and class ext). This check is used quite often in: * case statements * rescue statements * Calling protected methods * Class#is_a? * Module#=== * Module#<=> I believe it's most common to check a class against a parent class, to this commit aims to improve that (unfortunately does not help checking for an included Module). This is done by storing on each class the number and an array of all parent classes, in order (BasicObject is at index 0). Using this we can check whether a class is a subclass of another in constant time since we know the location to expect it in the hierarchy.
Diffstat (limited to 'benchmark')
-rw-r--r--benchmark/module_eqq.yml27
1 files changed, 27 insertions, 0 deletions
diff --git a/benchmark/module_eqq.yml b/benchmark/module_eqq.yml
new file mode 100644
index 0000000000..a561fb86dc
--- /dev/null
+++ b/benchmark/module_eqq.yml
@@ -0,0 +1,27 @@
+prelude: |
+ class SimpleClass; end
+ class MediumClass
+ 10.times { include Module.new }
+ end
+ class LargeClass
+ 100.times { include Module.new }
+ end
+ class HugeClass
+ 300.times { include Module.new }
+ end
+ SimpleObj = SimpleClass.new
+ MediumObj = MediumClass.new
+ LargeObj = LargeClass.new
+ HugeObj = HugeClass.new
+benchmark:
+ simple_class_eqq_simple_obj: |
+ SimpleClass === SimpleObj
+ medium_class_eqq_simple_obj: |
+ MediumClass === SimpleObj
+ simple_class_eqq_medium_obj: |
+ SimpleClass === MediumObj
+ simple_class_eqq_large_obj: |
+ SimpleClass === LargeObj
+ simple_class_eqq_huge_obj: |
+ SimpleClass === HugeObj
+loop_count: 20000000