Skip to content

Instantly share code, notes, and snippets.

@shyouhei
Last active October 24, 2019 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shyouhei/ac1e9648c9de70f2d23abe0d63b21909 to your computer and use it in GitHub Desktop.
Save shyouhei/ac1e9648c9de70f2d23abe0d63b21909 to your computer and use it in GitHub Desktop.
From edb99670b714b12525f36d6f9b174a72a75026b3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3?=
<shyouhei@ruby-lang.org>
Date: Wed, 23 Oct 2019 17:10:10 +0900
Subject: [PATCH] optimize CLASS_OF
---
include/ruby/ruby.h | 28 +++++++++++++++++++---------
1 file changed, 19 insertions(+), 9 deletions(-)
diff --git a/include/ruby/ruby.h b/include/ruby/ruby.h
index b144fa0111..cde68b6f28 100644
--- a/include/ruby/ruby.h
+++ b/include/ruby/ruby.h
@@ -2094,17 +2094,27 @@ RUBY_EXTERN VALUE rb_stdin, rb_stdout, rb_stderr;
static inline VALUE
rb_class_of(VALUE obj)
{
- if (RB_IMMEDIATE_P(obj)) {
- if (RB_FIXNUM_P(obj)) return rb_cInteger;
- if (RB_FLONUM_P(obj)) return rb_cFloat;
- if (obj == RUBY_Qtrue) return rb_cTrueClass;
- if (RB_STATIC_SYM_P(obj)) return rb_cSymbol;
+ if (obj == RUBY_Qfalse) {
+ return rb_cFalseClass;
}
- else if (!RB_TEST(obj)) {
- if (obj == RUBY_Qnil) return rb_cNilClass;
- if (obj == RUBY_Qfalse) return rb_cFalseClass;
+ else if (obj == RUBY_Qnil) {
+ return rb_cNilClass;
+ }
+ else if (obj == RUBY_Qtrue) {
+ return rb_cTrueClass;
+ }
+ else if (!(obj & RUBY_IMMEDIATE_MASK)) {
+ return RBASIC_CLASS(obj);
+ }
+ else if (RB_FIXNUM_P(obj)) {
+ return rb_cInteger;
+ }
+ else if (RB_STATIC_SYM_P(obj)) {
+ return rb_cSymbol;
+ }
+ else {
+ return rb_cFloat;
}
- return RBASIC(obj)->klass;
}
static inline int
--
2.17.1
Calculating -------------------------------------
ours@clang-10 ours@gcc-9 ours@clang-9 master@clang-10 master@gcc-9 master@clang-9
Optcarrot Lan_Master.nes 39.986 44.025 39.773 34.184 42.911 35.919 fps
Comparison:
Optcarrot Lan_Master.nes
ours@gcc-9: 44.0 fps
master@gcc-9: 42.9 fps - 1.03x slower
ours@clang-10: 40.0 fps - 1.10x slower
ours@clang-9: 39.8 fps - 1.11x slower
master@clang-9: 35.9 fps - 1.23x slower
master@clang-10: 34.2 fps - 1.29x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment