[#636] doc/NEWS — Matt Armstrong <matt@...>

22 messages 2002/12/15

Re: [patch] doc/NEWS

From: nobu.nokada@...
Date: 2002-12-16 22:57:13 UTC
List: ruby-core #654
Hi,

At Tue, 17 Dec 2002 03:40:37 +0900,
Matt Armstrong wrote:
> Actually I think the "elegant" solution to the problem is lazy string
> copies.  E.g.
> 
>     s2 = s1[50..-1] # Does not actually copy data until s1 (or s2)
>                     # is modified.  s1 and s2 must know they share
>                     # data.
>     s2 =~ /\Afoo/
> 
> I remember mention of some work being done on this, but there were
> implementation problems.

A String instance always has '\0' terminator not included in
its length.  So tried just tail sharing.


Index: string.c
===================================================================
RCS file: /cvs/ruby/src/ruby/string.c,v
retrieving revision 1.125
diff -u -2 -p -r1.125 string.c
--- string.c	10 Dec 2002 06:23:40 -0000	1.125
+++ string.c	16 Dec 2002 22:54:13 -0000
@@ -475,6 +475,16 @@ rb_str_substr(str, beg, len)
     if (len == 0) return rb_str_new5(str,0,0);
 
-    str2 = rb_str_new5(str,RSTRING(str)->ptr+beg, len);
-    OBJ_INFECT(str2, str);
+    if (len > sizeof(struct RString)/2 &&
+	beg + len == RSTRING(str)->len &&
+	!FL_TEST(str, STR_ASSOC)) {
+	if (!FL_TEST(str, ELTS_SHARED)) str = rb_str_new4(str);
+	str2 = rb_str_new3(str);
+	RSTRING(str2)->ptr += beg;
+	RSTRING(str2)->len = len;
+    }
+    else {
+	str2 = rb_str_new5(str,RSTRING(str)->ptr+beg, len);
+	OBJ_INFECT(str2, str);
+    }
 
     return str2;


-- 
Nobu Nakada

In This Thread