librelist archives

« back to archive

[PATCH] use rb_fstring from Ruby 2.1.0dev if available

[PATCH] use rb_fstring from Ruby 2.1.0dev if available

From:
Eric Wong
Date:
2013-10-12 @ 03:18
This should be safe after r43210 from ruby trunk
---
 ext/kcar/extconf.rb |  1 +
 ext/kcar/kcar.rl    | 12 +++++++++++-
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/ext/kcar/extconf.rb b/ext/kcar/extconf.rb
index 4f19c4a..1ac6847 100644
--- a/ext/kcar/extconf.rb
+++ b/ext/kcar/extconf.rb
@@ -7,6 +7,7 @@ have_macro("SIZEOF_OFF_T", "ruby.h") or 
check_sizeof("off_t", "sys/types.h")
 have_macro("SIZEOF_LONG", "ruby.h") or check_sizeof("long", "sys/types.h")
 have_func("rb_str_set_len", "ruby.h")
 have_func("rb_str_modify", "ruby.h")
+have_func("rb_fstring", "ruby.h")
 
 # -fPIC is needed for Rubinius, MRI already uses it regardless
 with_cflags($CFLAGS + " -fPIC ") do
diff --git a/ext/kcar/kcar.rl b/ext/kcar/kcar.rl
index e812279..7d3f9ae 100644
--- a/ext/kcar/kcar.rl
+++ b/ext/kcar/kcar.rl
@@ -192,6 +192,16 @@ static void write_cont_value(struct http_parser *hp,
   rb_str_buf_cat(hp->cont, vptr, end + 1);
 }
 
+#ifndef HAVE_RB_FSTRING
+static VALUE my_fstring(VALUE str)
+{
+  OBJ_FREEZE(str);
+
+  return str;
+}
+#define rb_fstring(str) my_fstring((str))
+#endif /* !HAVE_RB_FSTRING */
+
 static void write_value(VALUE hdr, struct http_parser *hp,
                         const char *buffer, const char *p)
 {
@@ -273,7 +283,7 @@ static void write_value(VALUE hdr, struct http_parser *hp,
 
     if (NIL_P(e)) {
       /* new value, freeze it since it speeds up MRI slightly */
-      OBJ_FREEZE(f);
+      f = rb_fstring(f);
 
       if (hclass == rb_cHash)
         rb_hash_aset(hdr, f, v);
-- 
1.8.4

Re: [PATCH] use rb_fstring from Ruby 2.1.0dev if available

From:
Eric Wong
Date:
2013-10-12 @ 06:59
Eric Wong <normalperson@yhbt.net> wrote:
> This should be safe after r43210 from ruby trunk

Ugh, that was a horrible commit message.

This uses the frozen string cache in Ruby 2.1.0 if available.  The
frozen string cache implements the:

	"literal string"f

syntax found in Ruby 2.1.0 (this Ruby syntax is not set-in-stone, but
the idea of the frozen string cache seems to be popular in ruby-core
right now and unlikely to go away).

Strings in the frozen string cache benefit from best-effort
deduplication, but can still be GC-ed (unlike Symbols).  Obviously they
are strings, too, so they're usable for Rack headers.