Skip to content
Permalink
Browse files
8148937: (str) Adapt StringJoiner for Compact Strings
Reviewed-by: redestad, chegar
  • Loading branch information
stsypanov authored and cl4es committed Mar 17, 2021
1 parent a707fcb commit 000012a3b08d5f21d4cb2369d2122adfae1a3e9b
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 22 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -148,12 +148,6 @@ public StringJoiner setEmptyValue(CharSequence emptyValue) {
return this;
}

private static int getChars(String s, char[] chars, int start) {
int len = s.length();
s.getChars(0, len, chars, start);
return len;
}

/**
* Returns the current value, consisting of the {@code prefix}, the values
* added so far separated by the {@code delimiter}, and the {@code suffix},
@@ -170,22 +164,21 @@ public String toString() {
}
final int size = this.size;
final int addLen = prefix.length() + suffix.length();
if (addLen == 0) {
compactElts();
return size == 0 ? "" : elts[0];
if (size == 0) {
if (addLen == 0) {
return "";
}
return prefix + suffix;
}
final String delimiter = this.delimiter;
final char[] chars = new char[len + addLen];
int k = getChars(prefix, chars, 0);
StringBuilder sb = new StringBuilder(len + addLen).append(prefix);
if (size > 0) {
k += getChars(elts[0], chars, k);
sb.append(elts[0]);
for (int i = 1; i < size; i++) {
k += getChars(delimiter, chars, k);
k += getChars(elts[i], chars, k);
sb.append(delimiter).append(elts[i]);
}
}
k += getChars(suffix, chars, k);
return new String(chars);
return sb.append(suffix).toString();
}

/**
@@ -249,15 +242,14 @@ public StringJoiner merge(StringJoiner other) {

private void compactElts() {
if (size > 1) {
final char[] chars = new char[len];
int i = 1, k = getChars(elts[0], chars, 0);
StringBuilder sb = new StringBuilder(len).append(elts[0]);
int i = 1;
do {
k += getChars(delimiter, chars, k);
k += getChars(elts[i], chars, k);
sb.append(delimiter).append(elts[i]);
elts[i] = null;
} while (++i < size);
size = 1;
elts[0] = new String(chars);
elts[0] = sb.toString();
}
}

@@ -0,0 +1,111 @@
/*
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.bench.java.util;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;

import java.util.StringJoiner;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
* Trivial benchmark for String joining with {@link java.util.StringJoiner}.
*/
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(jvmArgsAppend = {"-Xms2g", "-Xmx2g"})
public class StringJoinerBenchmark {

@Benchmark
public String stringJoiner(Data data) {
String[] stringArray = data.stringArray;
return Joiner.joinWithStringJoiner(stringArray);
}

@State(Scope.Thread)
public static class Data {

@Param({"latin", "cyrillic"})
private String mode;

@Param({"8", "32"})
private int length;

@Param({"5", "10"})
private int count;

private String[] stringArray;

@Setup
public void setup() {
stringArray = new String[count];

for (int i = 0; i < count; i++) {
String alphabet = getAlphabet(i, mode);
stringArray[i] = randomString(alphabet, length);
}
}

private String randomString(String alphabet, int length) {
var tl = ThreadLocalRandom.current();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
sb.append(alphabet.charAt(tl.nextInt(alphabet.length())));
}
return sb.toString();
}

private static String getAlphabet(int index, String mode) {
var latin = "abcdefghijklmnopqrstuvwxyz"; //English
StringBuilder sb = new StringBuilder();
latin.codePoints().forEach(cp -> sb.append(cp - 'a' + '\u0430'));
var cyrillic = sb.toString(); // Russian (partial, matching length of latin alphabet)

String alphabet;
switch (mode) {
case "latin" -> alphabet = latin;
case "cyrillic" -> alphabet = cyrillic;
default -> throw new RuntimeException("Illegal mode " + mode);
}
return alphabet;
}
}
}

class Joiner {
public static String joinWithStringJoiner(String[] stringArray) {
StringJoiner joiner = new StringJoiner(",", "[", "]");
for (String str : stringArray) {
joiner.add(str);
}
return joiner.toString();
}
}

0 comments on commit 000012a

Please sign in to comment.