mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 14:54:52 +02:00
8240094: Optimize empty substring handling
Reviewed-by: redestad, igerasim, jlaskey
This commit is contained in:
parent
257de28b2c
commit
f729514ebd
4 changed files with 23 additions and 18 deletions
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 1994, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -1819,7 +1819,7 @@ public final class String
|
||||||
* @param src the characters being searched.
|
* @param src the characters being searched.
|
||||||
* @param srcCoder coder handles the mapping between bytes/chars
|
* @param srcCoder coder handles the mapping between bytes/chars
|
||||||
* @param srcCount count of the source string.
|
* @param srcCount count of the source string.
|
||||||
* @param tgt the characters being searched for.
|
* @param tgtStr the characters being searched for.
|
||||||
* @param fromIndex the index to begin searching from.
|
* @param fromIndex the index to begin searching from.
|
||||||
*/
|
*/
|
||||||
static int lastIndexOf(byte[] src, byte srcCoder, int srcCount,
|
static int lastIndexOf(byte[] src, byte srcCoder, int srcCount,
|
||||||
|
@ -1900,10 +1900,10 @@ public final class String
|
||||||
public String substring(int beginIndex, int endIndex) {
|
public String substring(int beginIndex, int endIndex) {
|
||||||
int length = length();
|
int length = length();
|
||||||
checkBoundsBeginEnd(beginIndex, endIndex, length);
|
checkBoundsBeginEnd(beginIndex, endIndex, length);
|
||||||
int subLen = endIndex - beginIndex;
|
|
||||||
if (beginIndex == 0 && endIndex == length) {
|
if (beginIndex == 0 && endIndex == length) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
int subLen = endIndex - beginIndex;
|
||||||
return isLatin1() ? StringLatin1.newString(value, beginIndex, subLen)
|
return isLatin1() ? StringLatin1.newString(value, beginIndex, subLen)
|
||||||
: StringUTF16.newString(value, beginIndex, subLen);
|
: StringUTF16.newString(value, beginIndex, subLen);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -630,17 +630,11 @@ final class StringLatin1 {
|
||||||
|
|
||||||
public static String stripLeading(byte[] value) {
|
public static String stripLeading(byte[] value) {
|
||||||
int left = indexOfNonWhitespace(value);
|
int left = indexOfNonWhitespace(value);
|
||||||
if (left == value.length) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
return (left != 0) ? newString(value, left, value.length - left) : null;
|
return (left != 0) ? newString(value, left, value.length - left) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String stripTrailing(byte[] value) {
|
public static String stripTrailing(byte[] value) {
|
||||||
int right = lastIndexOfNonWhitespace(value);
|
int right = lastIndexOfNonWhitespace(value);
|
||||||
if (right == 0) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
return (right != value.length) ? newString(value, 0, right) : null;
|
return (right != value.length) ? newString(value, 0, right) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -764,6 +758,9 @@ final class StringLatin1 {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String newString(byte[] val, int index, int len) {
|
public static String newString(byte[] val, int index, int len) {
|
||||||
|
if (len == 0) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
return new String(Arrays.copyOfRange(val, index, index + len),
|
return new String(Arrays.copyOfRange(val, index, index + len),
|
||||||
LATIN1);
|
LATIN1);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -1016,18 +1016,12 @@ final class StringUTF16 {
|
||||||
public static String stripLeading(byte[] value) {
|
public static String stripLeading(byte[] value) {
|
||||||
int length = value.length >>> 1;
|
int length = value.length >>> 1;
|
||||||
int left = indexOfNonWhitespace(value);
|
int left = indexOfNonWhitespace(value);
|
||||||
if (left == length) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
return (left != 0) ? newString(value, left, length - left) : null;
|
return (left != 0) ? newString(value, left, length - left) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String stripTrailing(byte[] value) {
|
public static String stripTrailing(byte[] value) {
|
||||||
int length = value.length >>> 1;
|
int length = value.length >>> 1;
|
||||||
int right = lastIndexOfNonWhitespace(value);
|
int right = lastIndexOfNonWhitespace(value);
|
||||||
if (right == 0) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
return (right != length) ? newString(value, 0, right) : null;
|
return (right != length) ? newString(value, 0, right) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1132,6 +1126,9 @@ final class StringUTF16 {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String newString(byte[] val, int index, int len) {
|
public static String newString(byte[] val, int index, int len) {
|
||||||
|
if (len == 0) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
if (String.COMPACT_STRINGS) {
|
if (String.COMPACT_STRINGS) {
|
||||||
byte[] buf = compress(val, index, len);
|
byte[] buf = compress(val, index, len);
|
||||||
if (buf != null) {
|
if (buf != null) {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -28,6 +28,9 @@ import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@BenchmarkMode(Mode.AverageTime)
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||||
|
@Fork(value = 3)
|
||||||
|
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
|
||||||
|
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
|
||||||
@State(Scope.Benchmark)
|
@State(Scope.Benchmark)
|
||||||
public class StringSubstring {
|
public class StringSubstring {
|
||||||
|
|
||||||
|
@ -42,4 +45,12 @@ public class StringSubstring {
|
||||||
public String from26toEnd1() {
|
public String from26toEnd1() {
|
||||||
return s.substring(26, s.length());
|
return s.substring(26, s.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An empty substring should not allocate a new String
|
||||||
|
*/
|
||||||
|
@Benchmark
|
||||||
|
public String empty() {
|
||||||
|
return s.substring(17, 17);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue