Skip to content

Commit

Permalink
Use cached ByteList for infinity and nan Float#to_s
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Apr 1, 2024
1 parent 069ac96 commit bb5e46e
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions core/src/main/java/org/jruby/RubyFloat.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,10 @@ public static IRubyObject induced_from(ThreadContext context, IRubyObject recv,
public IRubyObject to_s() {
final Ruby runtime = metaClass.runtime;
if (Double.isInfinite(value)) {
return RubyString.newString(runtime, value < 0 ? "-Infinity" : "Infinity");
return RubyString.newStringShared(runtime, value < 0 ? NEGATIVE_INFINITY_TO_S_BYTELIST : POSITIVE_INFINITY_TO_S_BYTELIST);
}
if (Double.isNaN(value)) {
return RubyString.newString(runtime, "NaN");
return RubyString.newStringShared(runtime, NAN_TO_S_BYTELIST);
}

ByteList buf = new ByteList();
Expand Down Expand Up @@ -287,6 +287,10 @@ public IRubyObject to_s() {
return runtime.newString(buf);
}

public static final ByteList POSITIVE_INFINITY_TO_S_BYTELIST = new ByteList(ByteList.plain("Infinity"), USASCIIEncoding.INSTANCE, false);
public static final ByteList NEGATIVE_INFINITY_TO_S_BYTELIST = new ByteList(ByteList.plain("-Infinity"), USASCIIEncoding.INSTANCE, false);
public static final ByteList NAN_TO_S_BYTELIST = new ByteList(ByteList.plain("NaN"), USASCIIEncoding.INSTANCE, false);

/** flo_coerce
*
*/
Expand Down

0 comments on commit bb5e46e

Please sign in to comment.