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
7 changes: 7 additions & 0 deletions changelog/unreleased/SOLR-18315-optimize-utf-conversion.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
title: Replace custom code for UTF8-UTF16 conversions by standard Java API for better performances.
type: changed
authors:
- name: Pierre Salagnac
links:
- name: SOLR-18315
url: https://issues.apache.org/jira/browse/SOLR-18315
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class RequestWriters {
@State(Scope.Benchmark)
public static class BenchState {

@Param({"xml", "binary"})
@Param({"xml", "javabin"})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bug fix here? Nice to see the benchmarks get used and therefore finding oversights like this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, bug fix. I added this benchmark a while ago and I assume I forgot to change this name before merging. If I recall well, the class was renamed while I was working on this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

cool! thanks for responding.

String type;

@Param({"10", "100", "1000", "10000"})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
Expand All @@ -26,7 +27,6 @@
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.util.ByteUtils;
import org.apache.solr.common.util.JavaBinCodec;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.request.SolrQueryRequest;
Expand Down Expand Up @@ -57,10 +57,10 @@ public void testBytesRefWriting() {
}

private void compareStringFormat(String input) {
byte[] bytes1 = new byte[1024];
int len1 = ByteUtils.UTF16toUTF8(input, 0, input.length(), bytes1, 0);
byte[] bytes1 = input.getBytes(StandardCharsets.UTF_8);
int len1 = bytes1.length;
BytesRef bytesref = new BytesRef(input);
System.out.println();

assertEquals(len1, bytesref.length);
for (int i = 0; i < len1; i++) {
assertEquals(input + " not matching char at :" + i, bytesref.bytes[i], bytes1[i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@

package org.apache.solr.common.util;

import java.nio.charset.StandardCharsets;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import org.noggit.CharArr;

/**
* A mutable byte[] backed Utf8CharSequence. This is quite similar to the BytesRef of Lucene Do not
Expand All @@ -36,18 +35,13 @@ public class ByteArrayUtf8CharSequence implements Utf8CharSequence {
protected int offset;
protected int hashCode = Integer.MIN_VALUE;
protected int length;
protected volatile String utf16;
public Function<ByteArrayUtf8CharSequence, String> stringProvider;
protected String utf16;

public ByteArrayUtf8CharSequence(String utf16) {
buf = new byte[Math.multiplyExact(utf16.length(), 3)];
this.utf16 = utf16;
buf = utf16.getBytes(StandardCharsets.UTF_8);
offset = 0;
length = ByteUtils.UTF16toUTF8(utf16, 0, utf16.length(), buf, 0);
if (buf.length > length) {
byte[] copy = new byte[length];
System.arraycopy(buf, 0, copy, 0, length);
buf = copy;
}
length = buf.length;
assert isValid();
}

Expand Down Expand Up @@ -154,15 +148,8 @@ public char charAt(int index) {
}

private String _getStr() {
String utf16 = this.utf16;
if (utf16 == null) {
if (stringProvider != null) {
this.utf16 = utf16 = stringProvider.apply(this);
} else {
CharArr arr = new CharArr();
ByteUtils.UTF8toUTF16(buf, offset, length, arr);
this.utf16 = utf16 = arr.toString();
}
utf16 = new String(buf, offset, length, StandardCharsets.UTF_8);
}
return utf16;
}
Expand Down
230 changes: 0 additions & 230 deletions solr/solrj/src/java/org/apache/solr/common/util/ByteUtils.java

This file was deleted.

Loading
Loading