From: Eric Wong <e@80x24.org>
To: spew@80x24.org
Subject: [PATCH 1/2] variable.c: extract common functions for generic ivar indices
Date: Sat, 23 May 2015 01:24:43 +0000 [thread overview]
Message-ID: <1432344284-10395-1-git-send-email-e@80x24.org> (raw)
* variable.c (iv_index_tbl_make): extract from rb_ivar_set
(iv_index_tbl_extend): ditto
(iv_index_tbl_newsize): ditto
(rb_ivar_set): use extracted functions
---
variable.c | 63 ++++++++++++++++++++++++++++++++++++++++++--------------------
1 file changed, 43 insertions(+), 20 deletions(-)
diff --git a/variable.c b/variable.c
index a593437..edb30f4 100644
--- a/variable.c
+++ b/variable.c
@@ -1163,6 +1163,45 @@ rb_attr_get(VALUE obj, ID id)
return rb_ivar_lookup(obj, id, Qnil);
}
+static st_table *
+iv_index_tbl_make(VALUE obj)
+{
+ VALUE klass = rb_obj_class(obj);
+ st_table *iv_index_tbl = RCLASS_IV_INDEX_TBL(klass);
+
+ if (!iv_index_tbl) {
+ iv_index_tbl = RCLASS_IV_INDEX_TBL(klass) = st_init_numtable();
+ }
+
+ return iv_index_tbl;
+}
+
+static int
+iv_index_tbl_extend(st_table *iv_index_tbl, ID id, st_data_t *index)
+{
+ if (st_lookup(iv_index_tbl, (st_data_t)id, index)) {
+ return 0;
+ }
+ *index = iv_index_tbl->num_entries;
+ if (*index >= INT_MAX) {
+ rb_raise(rb_eArgError, "too many instance variables");
+ }
+ st_add_direct(iv_index_tbl, (st_data_t)id, *index);
+ return 1;
+}
+
+static long
+iv_index_tbl_newsize(st_table *iv_index_tbl, st_data_t index, int ivar_extended)
+{
+ long newsize = (index+1) + (index+1)/4; /* (index+1)*1.25 */
+
+ if (!ivar_extended &&
+ iv_index_tbl->num_entries < (st_index_t)newsize) {
+ newsize = iv_index_tbl->num_entries;
+ }
+ return newsize;
+}
+
VALUE
rb_ivar_set(VALUE obj, ID id, VALUE val)
{
@@ -1175,21 +1214,8 @@ rb_ivar_set(VALUE obj, ID id, VALUE val)
if (SPECIAL_CONST_P(obj)) goto generic;
switch (BUILTIN_TYPE(obj)) {
case T_OBJECT:
- iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj);
- if (!iv_index_tbl) {
- VALUE klass = rb_obj_class(obj);
- iv_index_tbl = RCLASS_IV_INDEX_TBL(klass);
- if (!iv_index_tbl) {
- iv_index_tbl = RCLASS_IV_INDEX_TBL(klass) = st_init_numtable();
- }
- }
- ivar_extended = 0;
- if (!st_lookup(iv_index_tbl, (st_data_t)id, &index)) {
- index = iv_index_tbl->num_entries;
- if (index >= INT_MAX) rb_raise(rb_eArgError, "too many instance variables");
- st_add_direct(iv_index_tbl, (st_data_t)id, index);
- ivar_extended = 1;
- }
+ iv_index_tbl = iv_index_tbl_make(obj);
+ ivar_extended = iv_index_tbl_extend(iv_index_tbl, id, &index);
len = ROBJECT_NUMIV(obj);
if (len <= (long)index) {
VALUE *ptr = ROBJECT_IVPTR(obj);
@@ -1202,11 +1228,8 @@ rb_ivar_set(VALUE obj, ID id, VALUE val)
}
else {
VALUE *newptr;
- long newsize = (index+1) + (index+1)/4; /* (index+1)*1.25 */
- if (!ivar_extended &&
- iv_index_tbl->num_entries < (st_index_t)newsize) {
- newsize = iv_index_tbl->num_entries;
- }
+ long newsize = iv_index_tbl_newsize(iv_index_tbl, index,
+ ivar_extended);
if (RBASIC(obj)->flags & ROBJECT_EMBED) {
newptr = ALLOC_N(VALUE, newsize);
--
EW
next reply other threads:[~2015-05-23 1:24 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-05-23 1:24 Eric Wong [this message]
2015-05-23 1:24 ` [PATCH 2/2] variable.c: use indices for generic ivars Eric Wong
-- strict thread matches above, loose matches on Subject: below --
2015-05-21 2:51 [PATCH 1/2] variable.c: extract common functions for generic ivar indices Eric Wong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1432344284-10395-1-git-send-email-e@80x24.org \
--to=e@80x24.org \
--cc=spew@80x24.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).