summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-12-04 15:17:21 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-12-04 15:17:21 +0000
commitf149326740c4d684de70564ecec88263e01bd3d3 (patch)
tree31ff135dc06ac243b6086e0e8fd8aeb7663cbe01 /string.c
parentbec3fed1350c3fc8e4bc34c8d3e30ef031bd77f5 (diff)
* string.c (sym_to_proc): caches Symbol procs, based on a patch from
Shumpei Akai <admin AT flexfrank.net>. [ruby-dev:37265] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@20521 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r--string.c27
1 files changed, 25 insertions, 2 deletions
diff --git a/string.c b/string.c
index d931cd9ca4..641018ab4d 100644
--- a/string.c
+++ b/string.c
@@ -6915,9 +6915,32 @@ sym_call(VALUE args, VALUE sym, int argc, VALUE *argv)
static VALUE
sym_to_proc(VALUE sym)
{
- return rb_proc_new(sym_call, (VALUE)SYM2ID(sym));
-}
+ static VALUE sym_proc_cache = Qfalse;
+ enum {SYM_PROC_CACHE_SIZE = 67};
+ VALUE proc;
+ long id, index;
+ VALUE *aryp;
+
+ if (!sym_proc_cache) {
+ rb_gc_register_mark_object(sym_proc_cache);
+ sym_proc_cache = rb_ary_new2(SYM_PROC_CACHE_SIZE * 2);
+ rb_ary_store(sym_proc_cache, SYM_PROC_CACHE_SIZE*2 - 1, Qnil);
+ }
+
+ id = SYM2ID(sym);
+ index = (id % SYM_PROC_CACHE_SIZE) << 1;
+ aryp = RARRAY_PTR(sym_proc_cache);
+ if (aryp[index] == sym) {
+ return aryp[index + 1];
+ }
+ else {
+ proc = rb_proc_new(sym_call, (VALUE)id);
+ aryp[index] = sym;
+ aryp[index + 1] = proc;
+ return proc;
+ }
+}
static VALUE
sym_succ(VALUE sym)