Skip to content

Commit c0f42a7

Browse files
committed
use ByteArrayLittleEndian
1 parent 18904a9 commit c0f42a7

File tree

2 files changed

+12
-58
lines changed

2 files changed

+12
-58
lines changed

src/java.base/share/classes/java/lang/StringLatin1.java

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
import java.util.function.IntConsumer;
3333
import java.util.stream.Stream;
3434
import java.util.stream.StreamSupport;
35-
import jdk.internal.misc.Unsafe;
3635
import jdk.internal.util.ArraysSupport;
36+
import jdk.internal.util.ByteArrayLittleEndian;
3737
import jdk.internal.vm.annotation.IntrinsicCandidate;
3838
import jdk.internal.vm.annotation.Stable;
3939

@@ -78,8 +78,6 @@ final class StringLatin1 {
7878
0x3039, 0x3139, 0x3239, 0x3339, 0x3439, 0x3539, 0x3639, 0x3739, 0x3839, 0x3939
7979
};
8080

81-
private static final Unsafe UNSAFE = Unsafe.getUnsafe();
82-
8381
public static char charAt(byte[] value, int index) {
8482
checkIndex(index, value.length);
8583
return (char)(value[index] & 0xff);
@@ -150,15 +148,13 @@ static int getChars(int i, int index, byte[] buf) {
150148
r = (q * 100) - i;
151149
i = q;
152150
charPos -= 2;
153-
assert charPos >= 0 && charPos < buf.length : "Trusted caller missed bounds check";
154-
UNSAFE.putShortUnaligned(buf, Unsafe.ARRAY_BYTE_BASE_OFFSET + charPos, PACKED_DIGITS[r], false);
151+
ByteArrayLittleEndian.setShort(buf, charPos, PACKED_DIGITS[r]);
155152
}
156153

157154
// We know there are at most two digits left at this point.
158155
if (i < -9) {
159156
charPos -= 2;
160-
assert charPos >= 0 && charPos < buf.length : "Trusted caller missed bounds check";
161-
UNSAFE.putShortUnaligned(buf, Unsafe.ARRAY_BYTE_BASE_OFFSET + charPos, PACKED_DIGITS[-i], false);
157+
ByteArrayLittleEndian.setShort(buf, charPos, PACKED_DIGITS[-i]);
162158
} else {
163159
buf[--charPos] = (byte)('0' - i);
164160
}
@@ -200,12 +196,7 @@ static int getChars(long i, int index, byte[] buf) {
200196
while (i <= Integer.MIN_VALUE) {
201197
q = i / 100;
202198
charPos -= 2;
203-
assert charPos >= 0 && charPos < buf.length : "Trusted caller missed bounds check";
204-
UNSAFE.putShortUnaligned(
205-
buf,
206-
Unsafe.ARRAY_BYTE_BASE_OFFSET + charPos,
207-
PACKED_DIGITS[(int)((q * 100) - i)],
208-
false);
199+
ByteArrayLittleEndian.setShort(buf, charPos, PACKED_DIGITS[(int)((q * 100) - i)]);
209200
i = q;
210201
}
211202

@@ -215,24 +206,14 @@ static int getChars(long i, int index, byte[] buf) {
215206
while (i2 <= -100) {
216207
q2 = i2 / 100;
217208
charPos -= 2;
218-
assert charPos >= 0 && charPos < buf.length : "Trusted caller missed bounds check";
219-
UNSAFE.putShortUnaligned(
220-
buf,
221-
Unsafe.ARRAY_BYTE_BASE_OFFSET + charPos,
222-
PACKED_DIGITS[(q2 * 100) - i2],
223-
false);
209+
ByteArrayLittleEndian.setShort(buf, charPos, PACKED_DIGITS[(q2 * 100) - i2]);
224210
i2 = q2;
225211
}
226212

227213
// We know there are at most two digits left at this point.
228214
if (i2 < -9) {
229215
charPos -= 2;
230-
assert charPos >= 0 && charPos < buf.length : "Trusted caller missed bounds check";
231-
UNSAFE.putShortUnaligned(
232-
buf,
233-
Unsafe.ARRAY_BYTE_BASE_OFFSET + charPos,
234-
PACKED_DIGITS[-i2],
235-
false);
216+
ByteArrayLittleEndian.setShort(buf, charPos, PACKED_DIGITS[-i2]);
236217
} else {
237218
buf[--charPos] = (byte)('0' - i2);
238219
}

src/java.base/share/classes/java/lang/StringUTF16.java

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
import java.util.function.IntConsumer;
3333
import java.util.stream.Stream;
3434
import java.util.stream.StreamSupport;
35-
import jdk.internal.misc.Unsafe;
3635
import jdk.internal.util.ArraysSupport;
36+
import jdk.internal.util.ByteArrayLittleEndian;
3737
import jdk.internal.vm.annotation.DontInline;
3838
import jdk.internal.vm.annotation.ForceInline;
3939
import jdk.internal.vm.annotation.IntrinsicCandidate;
@@ -1514,8 +1514,6 @@ public static int lastIndexOfLatin1(byte[] src, int srcCount,
15141514
}
15151515
}
15161516

1517-
private static final Unsafe UNSAFE = Unsafe.getUnsafe();
1518-
15191517
static final int MAX_LENGTH = Integer.MAX_VALUE >> 1;
15201518

15211519
// Used by trusted callers. Assumes all necessary bounds checks have
@@ -1550,12 +1548,7 @@ static int getChars(int i, int index, byte[] buf) {
15501548
int inflated = ((packed & 0xFF00) << 8) | (packed & 0xFF);
15511549

15521550
charPos -= 2;
1553-
assert charPos >= 0 && charPos < buf.length : "Trusted caller missed bounds check";
1554-
UNSAFE.putIntUnaligned(
1555-
buf,
1556-
Unsafe.ARRAY_BYTE_BASE_OFFSET + (charPos << 1),
1557-
inflated,
1558-
false);
1551+
ByteArrayLittleEndian.setInt(buf, charPos << 1, inflated);
15591552
}
15601553

15611554
// We know there are at most two digits left at this point.
@@ -1564,12 +1557,7 @@ static int getChars(int i, int index, byte[] buf) {
15641557
int inflated = ((packed & 0xFF00) << 8) | (packed & 0xFF);
15651558

15661559
charPos -= 2;
1567-
assert charPos >= 0 && charPos < buf.length : "Trusted caller missed bounds check";
1568-
UNSAFE.putIntUnaligned(
1569-
buf,
1570-
Unsafe.ARRAY_BYTE_BASE_OFFSET + (charPos << 1),
1571-
inflated,
1572-
false);
1560+
ByteArrayLittleEndian.setInt(buf, charPos << 1, inflated);
15731561
} else {
15741562
putChar(buf, --charPos, '0' - i);
15751563
}
@@ -1607,12 +1595,7 @@ static int getChars(long i, int index, byte[] buf) {
16071595
int inflated = ((packed & 0xFF00) << 8) | (packed & 0xFF);
16081596

16091597
charPos -= 2;
1610-
assert charPos >= 0 && charPos < buf.length : "Trusted caller missed bounds check";
1611-
UNSAFE.putIntUnaligned(
1612-
buf,
1613-
Unsafe.ARRAY_BYTE_BASE_OFFSET + (charPos << 1),
1614-
inflated,
1615-
false);
1598+
ByteArrayLittleEndian.setInt(buf, charPos << 1, inflated);
16161599
i = q;
16171600
}
16181601

@@ -1626,12 +1609,7 @@ static int getChars(long i, int index, byte[] buf) {
16261609
int inflated = ((packed & 0xFF00) << 8) | (packed & 0xFF);
16271610

16281611
charPos -= 2;
1629-
assert charPos >= 0 && charPos < buf.length : "Trusted caller missed bounds check";
1630-
UNSAFE.putIntUnaligned(
1631-
buf,
1632-
Unsafe.ARRAY_BYTE_BASE_OFFSET + (charPos << 1),
1633-
inflated,
1634-
false);
1612+
ByteArrayLittleEndian.setInt(buf, charPos << 1, inflated);
16351613
i2 = q2;
16361614
}
16371615

@@ -1642,12 +1620,7 @@ static int getChars(long i, int index, byte[] buf) {
16421620
int packed = (int) StringLatin1.PACKED_DIGITS[-i2];
16431621
int inflated = ((packed & 0xFF00) << 8) | (packed & 0xFF);
16441622

1645-
assert charPos >= 0 && charPos < buf.length : "Trusted caller missed bounds check";
1646-
UNSAFE.putIntUnaligned(
1647-
buf,
1648-
Unsafe.ARRAY_BYTE_BASE_OFFSET + (charPos << 1),
1649-
inflated,
1650-
false);
1623+
ByteArrayLittleEndian.setInt(buf, charPos << 1, inflated);
16511624
} else {
16521625
putChar(buf, --charPos, '0' - i2);
16531626
}

0 commit comments

Comments
 (0)