Class: Ruby2CExtension::Plugins::IVarCache

Inherits:
Ruby2CExtension::Plugin show all
Includes:
Util
Defined in:
lib/ruby2cext/plugins/ivar_cache.rb

Instance Attribute Summary

Attributes inherited from Ruby2CExtension::Plugin

#compiler

Instance Method Summary collapse

Methods included from Util

#args_arity, #deduce_type, #split_args, #values

Constructor Details

#initialize(compiler) ⇒ IVarCache

Returns a new instance of IVarCache.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ruby2cext/plugins/ivar_cache.rb', line 12

def initialize(compiler)
    super(compiler)
    @cache_index = Hash.new { |h,k| h[k] = h.size }
    compiler.add_preprocessor(:ivar) { |cfun, node|
        hash = node.last
        vid = hash[:vid]
        key = [vid, cfun.__id__]
        index = @cache_index[key]
        entry = "ivar_cache[#{index}]"
        "cache_ivar_get(&#{entry}, #{cfun.get_self}, #{cfun.sym(vid)})"
    }
    compiler.add_preprocessor(:iasgn) { |cfun, node|
        hash = node.last
        vid = hash[:vid]
        key = [vid, cfun.__id__]
        index = @cache_index[key]
        entry = "ivar_cache[#{index}]"
        "cache_ivar_set(&#{entry}, #{cfun.get_self}, #{cfun.sym(vid)}, #{cfun.comp(hash[:value])})"
    }
end

Instance Method Details

#global_c_codeObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ruby2cext/plugins/ivar_cache.rb', line 33

def global_c_code
    code = %{
        typedef struct {
            unsigned num_bins;
            unsigned bin_pos;
        } ivar_cache_entry;
        static ivar_cache_entry ivar_cache[#{@cache_index.size}];
        static void init_ivar_cache() {
            ivar_cache_entry* entry;
            for (entry = &ivar_cache[#{@cache_index.size-1}]; entry >= ivar_cache; --entry) {
                entry->num_bins = 0;
                entry->bin_pos = 0;
            }
        }
        typedef struct st_table_entry st_table_entry;
        struct st_table_entry {
            unsigned int hash;
            st_data_t key;
            st_data_t record;
            st_table_entry *next;
        };
        static st_table_entry *st_find_collided(st_table_entry **prev, ID id) {
            st_table_entry *cur = *prev;
            while (cur = cur->next) {
                if (cur->hash == id) {
                    (*prev)->next = cur->next;
                    cur->next = *prev;
                    *prev = cur;
                    return cur;
                }
                prev = &(*prev)->next;
            }
            return 0;
        }
        static VALUE cache_ivar_get(ivar_cache_entry* entry,
                                           VALUE obj, ID id)
        {
            st_table* table = ROBJECT(obj)->iv_tbl;
            unsigned bin_pos;
            st_table_entry *cur;
            switch (TYPE(obj)) {
                case T_OBJECT:
                case T_CLASS:
                case T_MODULE:
                    break;
                default:
                    return rb_ivar_get(obj, id);
            }
            if (!table) return Qnil;
            if (entry->num_bins==table->num_bins) {
                bin_pos = entry->bin_pos;
            } else {
                entry->num_bins = table->num_bins;
                bin_pos = entry->bin_pos = id % table->num_bins;
            }
            cur = table->bins[bin_pos];
            if (!cur) return Qnil;
            if (cur->hash == id) return cur->record;
            cur = st_find_collided(&table->bins[bin_pos], id);
            return cur ? cur->record : Qnil;
        }
        static VALUE cache_ivar_set(ivar_cache_entry* entry,
                                           VALUE obj, ID id, VALUE val)
        {
            st_table* table;
            unsigned bin_pos;
            st_table_entry *cur;
            if (!OBJ_TAINTED(obj) && rb_safe_level() >= 4 || OBJ_FROZEN(obj))
                return rb_ivar_set(obj, id, val);
            switch (TYPE(obj)) {
                case T_OBJECT:
                case T_CLASS:
                case T_MODULE:
                    break;
                default:
                    return rb_ivar_set(obj, id, val);
            }
            table = ROBJECT(obj)->iv_tbl;
            if (!table) {
                table = ROBJECT(obj)->iv_tbl = st_init_numtable();
            }
            if (entry->num_bins==table->num_bins) {
                bin_pos = entry->bin_pos;
            } else {
                entry->num_bins = table->num_bins;
                bin_pos = entry->bin_pos = id % table->num_bins;
            }
            cur = table->bins[bin_pos];
            if (!cur) {
                st_add_direct(table, id, val);
                return val;
            }
            if (cur->hash == id) return (cur->record = val);
            cur = st_find_collided(&table->bins[bin_pos], id);
            if (cur) {
                return (cur->record = val);
            } else {
                st_add_direct(table, id, val);
                return val;
            }
        }
    }
    code
end

#init_c_codeObject



138
139
140
141
142
# File 'lib/ruby2cext/plugins/ivar_cache.rb', line 138

def init_c_code
    %{
        init_ivar_cache();
    }
end