Skip to content

Commit

Permalink
numeric.c: micro optimizations
Browse files Browse the repository at this point in the history
* 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.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53910 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nobu committed Feb 24, 2016
1 parent 7c14876 commit 53ec85b
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions numeric.c
Original file line number Diff line number Diff line change
Expand Up @@ -733,8 +733,11 @@ flo_to_s(VALUE flt)
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");

Expand Down Expand Up @@ -2863,7 +2866,7 @@ fix_uminus(VALUE num)
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;

Expand All @@ -2877,15 +2880,14 @@ rb_fix2str(VALUE x, int base)
val = -val;
neg = 1;
}
*--b = '\0';
do {
*--b = ruby_digitmap[(int)(val % base)];
} while (val /= base);
if (neg) {
*--b = '-';
}

return rb_usascii_str_new2(b);
return rb_usascii_str_new(b, e - b);
}

/*
Expand Down

0 comments on commit 53ec85b

Please sign in to comment.