ruby-changes:41836
From: nobu <ko1@a...>
Date: Wed, 24 Feb 2016 11:20:08 +0900 (JST)
Subject: [ruby-changes:41836] nobu:r53910 (trunk): numeric.c: micro optimizations
nobu 2016-02-24 11:20:45 +0900 (Wed, 24 Feb 2016) New Revision: 53910 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=53910 Log: numeric.c: micro optimizations * numeric.c (flo_to_s, rb_fix2str): use rb_usascii_str_new instead of rb_usascii_str_new_cstr, when the length can be calculated. Modified files: trunk/numeric.c Index: numeric.c =================================================================== --- numeric.c (revision 53909) +++ numeric.c (revision 53910) @@ -733,8 +733,11 @@ flo_to_s(VALUE flt) https://github.com/ruby/ruby/blob/trunk/numeric.c#L733 char *p, *e; int sign, decpt, digs; - if (isinf(value)) - return rb_usascii_str_new2(value < 0 ? "-Infinity" : "Infinity"); + if (isinf(value)) { + static const char minf[] = "-Infinity"; + const int pos = (value > 0); /* skip "-" */ + return rb_usascii_str_new(minf+pos, strlen(minf)-pos); + } else if (isnan(value)) return rb_usascii_str_new2("NaN"); @@ -2863,7 +2866,7 @@ fix_uminus(VALUE num) https://github.com/ruby/ruby/blob/trunk/numeric.c#L2866 VALUE rb_fix2str(VALUE x, int base) { - char buf[SIZEOF_VALUE*CHAR_BIT + 2], *b = buf + sizeof buf; + char buf[SIZEOF_VALUE*CHAR_BIT + 1], *const e = buf + sizeof buf, *b = e; long val = FIX2LONG(x); int neg = 0; @@ -2877,7 +2880,6 @@ rb_fix2str(VALUE x, int base) https://github.com/ruby/ruby/blob/trunk/numeric.c#L2880 val = -val; neg = 1; } - *--b = '\0'; do { *--b = ruby_digitmap[(int)(val % base)]; } while (val /= base); @@ -2885,7 +2887,7 @@ rb_fix2str(VALUE x, int base) https://github.com/ruby/ruby/blob/trunk/numeric.c#L2887 *--b = '-'; } - return rb_usascii_str_new2(b); + return rb_usascii_str_new(b, e - b); } /* -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/