From: Eric Wong <e@80x24.org>
To: spew@80x24.org
Subject: [PATCH] string.c: ensure String#freeze resizes internal buffer
Date: Sat, 4 Jul 2015 05:25:34 +0000 [thread overview]
Message-ID: <1435987534-9787-1-git-send-email-e@80x24.org> (raw)
rb_str_freeze may resize oversized buffers to save memory, so favor
it over rb_obj_freeze. This is useful because IO methods do not
prematurely shrink buffers, as they are likely to be overwritten
with full data.
* string.c (Init_String): use rb_str_freeze for String#freeze
---
string.c | 2 +-
test/ruby/test_optimization.rb | 19 +++++++++++++++++++
2 files changed, 20 insertions(+), 1 deletion(-)
diff --git a/string.c b/string.c
index 34c77a5..15d4220 100644
--- a/string.c
+++ b/string.c
@@ -9143,7 +9143,7 @@ Init_String(void)
rb_define_method(rb_cString, "byteslice", rb_str_byteslice, -1);
rb_define_method(rb_cString, "scrub", str_scrub, -1);
rb_define_method(rb_cString, "scrub!", str_scrub_bang, -1);
- rb_define_method(rb_cString, "freeze", rb_obj_freeze, 0);
+ rb_define_method(rb_cString, "freeze", rb_str_freeze, 0);
rb_define_method(rb_cString, "to_i", rb_str_to_i, -1);
rb_define_method(rb_cString, "to_f", rb_str_to_f, 0);
diff --git a/test/ruby/test_optimization.rb b/test/ruby/test_optimization.rb
index 4a5484e..e2efc9d 100644
--- a/test/ruby/test_optimization.rb
+++ b/test/ruby/test_optimization.rb
@@ -1,4 +1,5 @@
require 'test/unit'
+require 'objspace'
class TestRubyOptimization < Test::Unit::TestCase
@@ -122,6 +123,24 @@ class TestRubyOptimization < Test::Unit::TestCase
assert_redefine_method('String', 'freeze', 'assert_nil "foo".freeze')
end
+ def test_string_freeze_saves_memory
+ n = 16384
+ data = '.'.freeze
+ r, w = IO.pipe
+ w.write data
+
+ s = r.readpartial(n, '')
+ assert_operator ObjectSpace.memsize_of(s), :>=, n,
+ 'IO buffer NOT resized prematurely because will likely be reused'
+
+ s.freeze
+ assert_equal ObjectSpace.memsize_of(data), ObjectSpace.memsize_of(s),
+ 'buffer resized on freeze since it cannot be written to again'
+ ensure
+ r.close if r
+ w.close if w
+ end
+
def test_string_eq_neq
%w(== !=).each do |m|
assert_redefine_method('String', m, <<-end)
--
EW
reply other threads:[~2015-07-04 5:25 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=1435987534-9787-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).