summaryrefslogtreecommitdiff
path: root/st.c
diff options
context:
space:
mode:
authornormal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-07-18 04:30:08 +0000
committernormal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-07-18 04:30:08 +0000
commit4f473905e14a7dbfdd29248f7a0673022dfaa699 (patch)
tree4e89edc3585eb7714e9e4ff42d7573a69f311119 /st.c
parentd04c085b3c39d7e3893620c8c64aaf89269d2d2c (diff)
newhash insn reuses existing keys
This gives the newhash VM instruction the same string reuse capabilities as rb_hash_aset. * st.c (str_key): new wrapper function to call rb_fstring_existing (rb_hash_bulk_insert): use str_key * test/ruby/test_optimization.rb (test_hash_reuse_fstring): ensure key reuse for newhash instructions git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59355 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'st.c')
-rw-r--r--st.c20
1 files changed, 18 insertions, 2 deletions
diff --git a/st.c b/st.c
index c2848b69b6..c983c0291b 100644
--- a/st.c
+++ b/st.c
@@ -2092,6 +2092,23 @@ st_rehash(st_table *tab)
}
#ifdef RUBY
+
+static VALUE
+str_key(VALUE key)
+{
+ VALUE k;
+
+ if (RB_OBJ_FROZEN(key)) {
+ return key;
+ }
+ if ((k = rb_fstring_existing(key)) != Qnil) {
+ return k;
+ }
+ else {
+ return rb_str_new_frozen(key);
+ }
+}
+
/* Mimics ruby's { foo => bar } syntax. This function is placed here
because it touches table internals and write barriers at once. */
void
@@ -2114,8 +2131,7 @@ rb_hash_bulk_insert(long argc, const VALUE *argv, VALUE hash)
for (i = 0; i < argc; /* */) {
VALUE key = argv[i++];
VALUE val = argv[i++];
- st_data_t k = (rb_obj_class(key) == rb_cString) ?
- rb_str_new_frozen(key) : key;
+ st_data_t k = (rb_obj_class(key) == rb_cString) ? str_key(key) : key;
st_table_entry e;
e.hash = do_hash(k, tab);
e.key = k;