Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/java.base/share/classes/java/lang/Byte.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package java.lang;

import jdk.internal.misc.CDS;
import jdk.internal.misc.PreviewFeatures;
import jdk.internal.value.DeserializeConstructor;
import jdk.internal.vm.annotation.IntrinsicCandidate;
import jdk.internal.vm.annotation.Stable;
Expand Down Expand Up @@ -142,11 +143,23 @@ private ByteCache() {}
/**
* Returns a {@code Byte} instance representing the specified
* {@code byte} value.
* If a new {@code Byte} instance is not required, this method
* should generally be used in preference to the constructor
* {@link #Byte(byte)}, as this method is likely to yield
* significantly better space and time performance since
* all byte values are cached.
* <div class="preview-block">
* <div class="preview-comment">
* <p>
* - When preview features are NOT enabled, {@code Byte} is an identity class.
* If a new {@code Byte} instance is not required, this method
* should generally be used in preference to the constructor
* {@link #Byte(byte)}, as this method is likely to yield
* significantly better space and time performance since
* all byte values are cached.
* </p>
* <p>
* - When preview features are enabled, {@code Byte} is a {@linkplain Class#isValue value class}.
* The {@code valueOf} behavior is the same as invoking the constructor,
* whether cached or not.
* </p>
* </div>
* </div>
*
* @param b a byte value.
* @return a {@code Byte} instance representing {@code b}.
Expand All @@ -156,7 +169,7 @@ private ByteCache() {}
@DeserializeConstructor
public static Byte valueOf(byte b) {
final int offset = 128;
return ByteCache.cache[(int)b + offset];
return (!PreviewFeatures.isEnabled()) ? ByteCache.cache[(int)b + offset] : new Byte(b);
}

/**
Expand Down
36 changes: 25 additions & 11 deletions src/java.base/share/classes/java/lang/Character.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package java.lang;

import jdk.internal.misc.CDS;
import jdk.internal.misc.PreviewFeatures;
import jdk.internal.value.DeserializeConstructor;
import jdk.internal.vm.annotation.IntrinsicCandidate;
import jdk.internal.vm.annotation.Stable;
Expand Down Expand Up @@ -9271,15 +9272,26 @@ private CharacterCache(){}
/**
* Returns a {@code Character} instance representing the specified
* {@code char} value.
* If a new {@code Character} instance is not required, this method
* should generally be used in preference to the constructor
* {@link #Character(char)}, as this method is likely to yield
* significantly better space and time performance by caching
* frequently requested values.
*
* This method will always cache values in the range {@code
* '\u005Cu0000'} to {@code '\u005Cu007F'}, inclusive, and may
* cache other values outside of this range.
* <div class="preview-block">
* <div class="preview-comment">
* <p>
* - When preview features are NOT enabled, {@code Character} is an identity class.
* If a new {@code Character} instance is not required, this method
* should generally be used in preference to the constructor
* {@link #Character(char)}, as this method is likely to yield
* significantly better space and time performance by caching
* frequently requested values.
* This method will always cache values in the range {@code
* '\u005Cu0000'} to {@code '\u005Cu007F'}, inclusive, and may
* cache other values outside of this range.
* </p>
* <p>
* - When preview features are enabled, {@code Character} is a {@linkplain Class#isValue value class}.
* The {@code valueOf} behavior is the same as invoking the constructor,
* whether cached or not.
* </p>
* </div>
* </div>
*
* @param c a char value.
* @return a {@code Character} instance representing {@code c}.
Expand All @@ -9288,8 +9300,10 @@ private CharacterCache(){}
@IntrinsicCandidate
@DeserializeConstructor
public static Character valueOf(char c) {
if (c <= 127) { // must cache
return CharacterCache.cache[(int)c];
if (!PreviewFeatures.isEnabled()) {
if (c <= 127) { // must cache
return CharacterCache.cache[(int) c];
}
}
return new Character(c);
}
Expand Down
35 changes: 25 additions & 10 deletions src/java.base/share/classes/java/lang/Integer.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package java.lang;

import jdk.internal.misc.CDS;
import jdk.internal.misc.PreviewFeatures;
import jdk.internal.misc.VM;
import jdk.internal.util.DecimalDigits;
import jdk.internal.value.DeserializeConstructor;
Expand Down Expand Up @@ -996,14 +997,26 @@ private IntegerCache() {}

/**
* Returns an {@code Integer} instance representing the specified
* {@code int} value. If a new {@code Integer} instance is not
* required, this method should generally be used in preference to
* the constructor {@link #Integer(int)}, as this method is likely
* to yield significantly better space and time performance by
* caching frequently requested values.
*
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
* {@code int} value.
* <div class="preview-block">
* <div class="preview-comment">
* <p>
* - When preview features are NOT enabled, {@code Integer} is an identity class.
* If a new {@code Integer} instance is not
* required, this method should generally be used in preference to
* the constructor {@link #Integer(int)}, as this method is likely
* to yield significantly better space and time performance by
* caching frequently requested values.
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
* </p>
* <p>
* - When preview features are enabled, {@code Integer} is a {@linkplain Class#isValue value class}.
* The {@code valueOf} behavior is the same as invoking the constructor,
* whether cached or not.
* </p>
* </div>
* </div>
*
* @param i an {@code int} value.
* @return an {@code Integer} instance representing {@code i}.
Expand All @@ -1012,8 +1025,10 @@ private IntegerCache() {}
@IntrinsicCandidate
@DeserializeConstructor
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
if (!PreviewFeatures.isEnabled()) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
}
return new Integer(i);
}

Expand Down
34 changes: 24 additions & 10 deletions src/java.base/share/classes/java/lang/Long.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.Optional;

import jdk.internal.misc.CDS;
import jdk.internal.misc.PreviewFeatures;
import jdk.internal.value.DeserializeConstructor;
import jdk.internal.util.DecimalDigits;
import jdk.internal.vm.annotation.ForceInline;
Expand Down Expand Up @@ -989,14 +990,25 @@ private LongCache() {}
/**
* Returns a {@code Long} instance representing the specified
* {@code long} value.
* If a new {@code Long} instance is not required, this method
* should generally be used in preference to the constructor
* {@link #Long(long)}, as this method is likely to yield
* significantly better space and time performance by caching
* frequently requested values.
*
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
* <div class="preview-block">
* <div class="preview-comment">
* <p>
* - When preview features are NOT enabled, {@code Long} is an identity class.
* If a new {@code Long} instance is not required, this method
* should generally be used in preference to the constructor
* {@link #Long(long)}, as this method is likely to yield
* significantly better space and time performance by caching
* frequently requested values.
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
* </p>
* <p>
* - When preview features are enabled, {@code Long} is a {@linkplain Class#isValue value class}.
* The {@code valueOf} behavior is the same as invoking the constructor,
* whether cached or not.
* </p>
* </div>
* </div>
*
* @param l a long value.
* @return a {@code Long} instance representing {@code l}.
Expand All @@ -1006,8 +1018,10 @@ private LongCache() {}
@DeserializeConstructor
public static Long valueOf(long l) {
final int offset = 128;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend moving this into the preview feature block too.

if (l >= -128 && l <= 127) { // will cache
return LongCache.cache[(int)l + offset];
if (!PreviewFeatures.isEnabled()) {
if (l >= -128 && l <= 127) { // will cache
return LongCache.cache[(int) l + offset];
}
}
return new Long(l);
}
Expand Down
34 changes: 24 additions & 10 deletions src/java.base/share/classes/java/lang/Short.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package java.lang;

import jdk.internal.misc.CDS;
import jdk.internal.misc.PreviewFeatures;
import jdk.internal.value.DeserializeConstructor;
import jdk.internal.vm.annotation.IntrinsicCandidate;
import jdk.internal.vm.annotation.Stable;
Expand Down Expand Up @@ -269,14 +270,25 @@ private ShortCache() {}
/**
* Returns a {@code Short} instance representing the specified
* {@code short} value.
* If a new {@code Short} instance is not required, this method
* should generally be used in preference to the constructor
* {@link #Short(short)}, as this method is likely to yield
* significantly better space and time performance by caching
* frequently requested values.
*
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
* <div class="preview-block">
* <div class="preview-comment">
* <p>
* - When preview features are NOT enabled, {@code Short} is an identity class.
* If a new {@code Short} instance is not required, this method
* should generally be used in preference to the constructor
* {@link #Short(short)}, as this method is likely to yield
* significantly better space and time performance by caching
* frequently requested values.
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
* </p>
* <p>
* - When preview features are enabled, {@code Short} is a {@linkplain Class#isValue value class}.
* The {@code valueOf} behavior is the same as invoking the constructor,
* whether cached or not.
* </p>
* </div>
* </div>
*
* @param s a short value.
* @return a {@code Short} instance representing {@code s}.
Expand All @@ -287,8 +299,10 @@ private ShortCache() {}
public static Short valueOf(short s) {
final int offset = 128;
int sAsInt = s;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same remark, I recommend moving this into the preview feature check block.

if (sAsInt >= -128 && sAsInt <= 127) { // must cache
return ShortCache.cache[sAsInt + offset];
if (!PreviewFeatures.isEnabled()) {
if (sAsInt >= -128 && sAsInt <= 127) { // must cache
return ShortCache.cache[sAsInt + offset];
}
}
return new Short(s);
}
Expand Down