[macruby-changes] [5208] MacRuby/trunk/pack.c

source_changes at macosforge.org source_changes at macosforge.org
Fri Jan 28 15:18:39 PST 2011


Revision: 5208
          http://trac.macosforge.org/projects/ruby/changeset/5208
Author:   lsansonetti at apple.com
Date:     2011-01-28 15:18:39 -0800 (Fri, 28 Jan 2011)
Log Message:
-----------
fix some performance problems in #pack with 'm' or 'u' flags, by pre-allocating the result bytestring and use a stack buffer in the encoding loop

Modified Paths:
--------------
    MacRuby/trunk/pack.c

Modified: MacRuby/trunk/pack.c
===================================================================
--- MacRuby/trunk/pack.c	2011-01-28 21:42:46 UTC (rev 5207)
+++ MacRuby/trunk/pack.c	2011-01-28 23:18:39 UTC (rev 5208)
@@ -924,6 +924,20 @@
 	    else {
 		len = len / 3 * 3;
 	    }
+
+	    // Try to guess the final size of the bytestring and pre-allocate
+	    // its content, to avoid too much frequent memory reallocations
+	    // during the encoding loop.
+	    do {
+	    	const long datalen = rb_bstr_length(data);
+		const long newdatalen = datalen
+		    + ((plen / len)
+			    * (len * 4 / 3 + 6));
+		rb_bstr_resize(data, newdatalen);
+		rb_bstr_set_length(data, datalen);
+	    }
+	    while (false);
+
 	    while (plen > 0) {
 		long todo;
 
@@ -1055,7 +1069,7 @@
 static void
 encodes(VALUE data, const char *s, long len, int type, int tail_lf)
 {
-    char *buff = (char *)malloc(len * 4 / 3 + 6);
+    char buff[4096];
     long i = 0;
     const char *trans = type == 'u' ? uu_table : b64_table;
     int padding;
@@ -1097,7 +1111,6 @@
 	buff[i++] = '\n';
     }
     rb_bstr_concat(data, (const UInt8 *)buff, i);
-    free(buff);
 }
 static const char hex_table[] = "0123456789ABCDEF";
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20110128/ff56363c/attachment.html>


More information about the macruby-changes mailing list