[前][次][番号順一覧][スレッド一覧]

ruby-changes:32490

From: nobu <ko1@a...>
Date: Sun, 12 Jan 2014 16:49:31 +0900 (JST)
Subject: [ruby-changes:32490] nobu:r44569 (trunk): iseq.c: linear search

nobu	2014-01-12 16:49:26 +0900 (Sun, 12 Jan 2014)

  New Revision: 44569

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=44569

  Log:
    iseq.c: linear search
    
    * iseq.c (iseq_type_from_id): linear search instead of hash lookup for
      small fixed number keys.

  Modified files:
    trunk/iseq.c
Index: iseq.c
===================================================================
--- iseq.c	(revision 44568)
+++ iseq.c	(revision 44569)
@@ -474,6 +474,22 @@ rb_iseq_new_with_bopt(NODE *node, VALUE https://github.com/ruby/ruby/blob/trunk/iseq.c#L474
 #define CHECK_STRING(v)  rb_convert_type((v), T_STRING, "String", "to_str")
 #define CHECK_SYMBOL(v)  rb_convert_type((v), T_SYMBOL, "Symbol", "to_sym")
 static inline VALUE CHECK_INTEGER(VALUE v) {(void)NUM2LONG(v); return v;}
+
+static enum iseq_type
+iseq_type_from_id(const ID typeid)
+{
+    if (typeid == rb_intern("top")) return ISEQ_TYPE_TOP;
+    if (typeid == rb_intern("method")) return ISEQ_TYPE_METHOD;
+    if (typeid == rb_intern("block")) return ISEQ_TYPE_BLOCK;
+    if (typeid == rb_intern("class")) return ISEQ_TYPE_CLASS;
+    if (typeid == rb_intern("rescue")) return ISEQ_TYPE_RESCUE;
+    if (typeid == rb_intern("ensure")) return ISEQ_TYPE_ENSURE;
+    if (typeid == rb_intern("eval")) return ISEQ_TYPE_EVAL;
+    if (typeid == rb_intern("main")) return ISEQ_TYPE_MAIN;
+    if (typeid == rb_intern("defined_guard")) return ISEQ_TYPE_DEFINED_GUARD;
+    return (enum iseq_type)-1;
+}
+
 static VALUE
 iseq_load(VALUE self, VALUE data, VALUE parent, VALUE opt)
 {
@@ -484,8 +500,7 @@ iseq_load(VALUE self, VALUE data, VALUE https://github.com/ruby/ruby/blob/trunk/iseq.c#L500
     VALUE type, body, locals, args, exception;
 
     st_data_t iseq_type;
-    static struct st_table *type_map_cache = 0;
-    struct st_table *type_map = 0;
+    ID typeid;
     rb_iseq_t *iseq;
     rb_compile_option_t option;
     int i = 0;
@@ -525,28 +540,9 @@ iseq_load(VALUE self, VALUE data, VALUE https://github.com/ruby/ruby/blob/trunk/iseq.c#L540
     iseq->self = iseqval;
     iseq->local_iseq = iseq;
 
-    type_map = type_map_cache;
-    if (type_map == 0) {
-	struct st_table *cached_map;
-	type_map = st_init_numtable();
-	st_insert(type_map, ID2SYM(rb_intern("top")), ISEQ_TYPE_TOP);
-	st_insert(type_map, ID2SYM(rb_intern("method")), ISEQ_TYPE_METHOD);
-	st_insert(type_map, ID2SYM(rb_intern("block")), ISEQ_TYPE_BLOCK);
-	st_insert(type_map, ID2SYM(rb_intern("class")), ISEQ_TYPE_CLASS);
-	st_insert(type_map, ID2SYM(rb_intern("rescue")), ISEQ_TYPE_RESCUE);
-	st_insert(type_map, ID2SYM(rb_intern("ensure")), ISEQ_TYPE_ENSURE);
-	st_insert(type_map, ID2SYM(rb_intern("eval")), ISEQ_TYPE_EVAL);
-	st_insert(type_map, ID2SYM(rb_intern("main")), ISEQ_TYPE_MAIN);
-	st_insert(type_map, ID2SYM(rb_intern("defined_guard")), ISEQ_TYPE_DEFINED_GUARD);
-	cached_map = ATOMIC_PTR_CAS(type_map_cache, (struct st_table *)0, type_map);
-	if (cached_map) {
-	    st_free_table(type_map);
-	    type_map = cached_map;
-	}
-    }
-
-    if (st_lookup(type_map, type, &iseq_type) == 0) {
-	ID typeid = SYM2ID(type);
+    typeid = SYM2ID(type);
+    iseq_type = iseq_type_from_id(typeid);
+    if (iseq_type == (enum iseq_type)-1) {
 	VALUE typename = rb_id2str(typeid);
 	if (typename)
 	    rb_raise(rb_eTypeError, "unsupport type: :%"PRIsVALUE, typename);

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]