indexOfSlashV

This commit is contained in:
高铁 2025-02-25 19:05:30 +08:00
parent c9e109020e
commit 8c86b8ce0d
4 changed files with 16 additions and 41 deletions

View File

@ -17,8 +17,6 @@ import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.concurrent.TimeUnit;
import static com.alibaba.fastjson2.util.JDKUtils.INDEX_OF_CHAR_LATIN1;
public class BytesAsciiCheck {
static byte[] bytes;
static char[] chars;
@ -94,7 +92,7 @@ public class BytesAsciiCheck {
@Benchmark
public void indexOfSlash(Blackhole bh) throws Throwable {
bh.consume(indexOfSlash(bytes, 0, bytes.length));
bh.consume(com.alibaba.fastjson2.util.IOUtils.indexOfSlash(bytes, 0, bytes.length));
}
@Benchmark
@ -112,17 +110,6 @@ public class BytesAsciiCheck {
bh.consume(str.indexOf('\\'));
}
public static int indexOfSlash(byte[] value, int fromIndex, int max) {
if (INDEX_OF_CHAR_LATIN1 == null) {
return com.alibaba.fastjson2.util.IOUtils.indexOfSlashV(value, fromIndex, max);
}
try {
return (int) INDEX_OF_CHAR_LATIN1.invokeExact(value, (int) '\\', fromIndex, max);
} catch (Throwable e) {
throw new JSONException(e.getMessage());
}
}
private static int indexOfChar(byte[] bytes, int ch, int fromIndex, int toIndex) {
try {
return (int) INDEX_OF_CHAR.invokeExact(bytes, ch, fromIndex, toIndex);

View File

@ -1482,19 +1482,7 @@ final class JSONReaderASCII
private int indexOfSlash(byte[] bytes, int offset, int end) {
int slashIndex = nextEscapeIndex;
if (slashIndex == ESCAPE_INDEX_NOT_SET || (slashIndex != -1 && slashIndex < offset)) {
if (str != null) {
slashIndex = str.indexOf('\\', offset);
} else if (INDEX_OF_CHAR_LATIN1 == null) {
slashIndex = IOUtils.indexOfSlashV(bytes, offset, end);
} else {
try {
slashIndex = (int) INDEX_OF_CHAR_LATIN1.invokeExact(bytes, (int) '\\', offset, end);
}
catch (Throwable e) {
throw new JSONException(e.getMessage());
}
}
nextEscapeIndex = slashIndex;
nextEscapeIndex = slashIndex = IOUtils.indexOfSlash(bytes, offset, end);
}
return slashIndex;
}

View File

@ -1563,6 +1563,17 @@ public class IOUtils {
return indexOfChar(value, '\n', i, max);
}
public static int indexOfSlash(byte[] value, int fromIndex, int max) {
if (INDEX_OF_CHAR_LATIN1 == null) {
return indexOfSlashV(value, fromIndex, max);
}
try {
return (int) INDEX_OF_CHAR_LATIN1.invokeExact(value, (int) '\\', fromIndex, max);
} catch (Throwable e) {
throw new JSONException(e.getMessage());
}
}
public static int indexOfSlashV(byte[] value, int fromIndex, int max) {
int i = fromIndex;
long address = ARRAY_BYTE_BASE_OFFSET + fromIndex;

View File

@ -403,27 +403,16 @@ public class IOUtilsTest {
public void indexOfSlash() {
byte[] bytes = "\\b\\d\\".getBytes(StandardCharsets.UTF_8);
assertEquals(2,
indexOfSlash(
IOUtils.indexOfSlash(
bytes, 1, bytes.length));
assertEquals(0,
indexOfSlash(
IOUtils.indexOfSlash(
bytes, 0, bytes.length));
assertEquals(4,
indexOfSlash(
IOUtils.indexOfSlash(
bytes, 3, bytes.length));
}
public static int indexOfSlash(byte[] value, int fromIndex, int max) {
if (INDEX_OF_CHAR_LATIN1 == null) {
return IOUtils.indexOfSlashV(value, fromIndex, max);
}
try {
return (int) INDEX_OF_CHAR_LATIN1.invokeExact(value, (int) '\\', fromIndex, max);
} catch (Throwable e) {
throw new JSONException(e.getMessage());
}
}
@Test
public void convEndian() throws Throwable {
Random r = new Random();