Skip to content

Commit

Permalink
Add specialized Byte/CharTrieOfOneSingleChar.
Browse files Browse the repository at this point in the history
  • Loading branch information
wrandelshofer committed Oct 19, 2024
1 parent 7cdd157 commit 3c2f3fe
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .idea/runConfigurations/Main_Dev_formatted_de_CH.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/runConfigurations/Main_Java17_canada_hex.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package ch.randelshofer.fastdoubleparser.bte;

import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
Expand Down Expand Up @@ -39,20 +40,29 @@ static ByteTrie copyOf(Set<String> set, boolean ignoreCase) {
case 0:
return new ByteTrieOfNone();
case 1:
String str = set.iterator().next();
if (ignoreCase) {
String str = set.iterator().next();
switch (str.length()) {
case 0:
return new ByteTrieOfNone();
case 1:
LinkedHashSet<String> newSet = new LinkedHashSet<>();
newSet.add(str.toLowerCase());
newSet.add(str.toUpperCase());
return newSet.size() == 1 ? new ByteTrieOfOne(newSet) : new ByteTrieOfFew(newSet);
if (newSet.size() == 1) {
if (newSet.iterator().next().getBytes(StandardCharsets.UTF_8).length == 1) {
return new ByteTrieOfOneSingleByte(newSet);
}
return new ByteTrieOfOne(newSet);
}
return new ByteTrieOfFew(newSet);
default:
return new ByteTrieOfOneIgnoreCase(set);
}
}
if (set.iterator().next().getBytes(StandardCharsets.UTF_8).length == 1) {
return new ByteTrieOfOneSingleByte(set);
}
return new ByteTrieOfOne(set);
default:
if (ignoreCase) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ public ByteTrieOfOne(Set<String> set) {
chars = set.iterator().next().getBytes(StandardCharsets.UTF_8);
}

public ByteTrieOfOne(byte[] chars) {
this.chars = chars;
}


@Override
public int match(byte[] str) {
Expand All @@ -27,6 +23,10 @@ public int match(byte[] str) {

@Override
public int match(byte[] str, int startIndex, int endIndex) {
/*
int mismatch = Arrays.mismatch(chars, 0, chars.length, str, startIndex,startIndex+ Math.min(endIndex - startIndex, chars.length));
return mismatch<0?chars.length:0;
*/
int i = 0;
int limit = Math.min(endIndex - startIndex, chars.length);
while (i < limit && str[i + startIndex] == chars[i]) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* @(#)ByteTrieOfOne.java
* Copyright © 2024 Werner Randelshofer, Switzerland. MIT License.
*/
package ch.randelshofer.fastdoubleparser.bte;

import java.nio.charset.StandardCharsets;
import java.util.Set;

class ByteTrieOfOneSingleByte implements ByteTrie {
private final byte ch;

public ByteTrieOfOneSingleByte(Set<String> set) {
if (set.size() != 1) throw new IllegalArgumentException("set size must be 1, size=" + set.size());
byte[] chars = set.iterator().next().getBytes(StandardCharsets.UTF_8);
if (chars.length != 1) throw new IllegalArgumentException("char size must be 1, size=" + set.size());
ch = chars[0];
}

public ByteTrieOfOneSingleByte(byte ch) {
this.ch = ch;
}


@Override
public int match(byte[] str) {
return match(str, 0, str.length);
}

@Override
public int match(byte[] str, int startIndex, int endIndex) {
return startIndex < endIndex && str[startIndex] == ch ? 1 : 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public static CharTrie of(Set<String> set, boolean ignoreCase) {
case 0:
return new CharTrieOfNone();
case 1:
if (set.iterator().next().length() == 1) {
return ignoreCase ? new CharTrieOfOneIgnoreCase(set) : new CharTrieOfOneSingleChar(set);
}
return ignoreCase ? new CharTrieOfOneIgnoreCase(set) : new CharTrieOfOne(set);
default:
return ignoreCase ? new CharTrieOfFewIgnoreCase(set) : new CharTrieOfFew(set);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,16 @@ public int match(char[] str) {

@Override
public int match(char[] str, int startIndex, int endIndex) {
/*
int mismatch = Arrays.mismatch(chars, 0, chars.length, str, startIndex,startIndex+ Math.min(endIndex - startIndex, chars.length));
return mismatch<0?chars.length:0;
*/
int i = 0;
int limit = Math.min(endIndex - startIndex, chars.length);
while (i < limit && str[i + startIndex] == chars[i]) {
i++;
}
return i == chars.length ? chars.length : 0;

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* @(#)ByteTrieOfOne.java
* Copyright © 2024 Werner Randelshofer, Switzerland. MIT License.
*/
package ch.randelshofer.fastdoubleparser.chr;

import java.util.Set;

class CharTrieOfOneSingleChar implements CharTrie {
private final char ch;

public CharTrieOfOneSingleChar(Set<String> set) {
if (set.size() != 1) throw new IllegalArgumentException("set size must be 1, size=" + set.size());
char[] chars = set.iterator().next().toCharArray();
if (chars.length != 1) throw new IllegalArgumentException("char size must be 1, size=" + set.size());
ch = chars[0];
}

public CharTrieOfOneSingleChar(char ch) {
this.ch = ch;
}

@Override
public int match(CharSequence str, int startIndex, int endIndex) {
return startIndex < endIndex && str.charAt(startIndex) == ch ? 1 : 0;
}

@Override
public int match(char[] str) {
return match(str, 0, str.length);
}

@Override
public int match(char[] str, int startIndex, int endIndex) {
return startIndex < endIndex && str[startIndex] == ch ? 1 : 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,21 @@ public void loadFile(String filename) throws IOException {
}

private VarianceStatistics measure(Supplier<? extends Number> func, int numberOfTrials,
double confidenceLevel, double confidenceIntervalWidth, int minTrials) {
double confidenceLevel, double confidenceIntervalWidth, int minTrials, String name) {
long t1;
double confidenceWidth;
long t2;
double elapsed;
VarianceStatistics stats = new VarianceStatistics();

// sleep to cooldown the processor
try {
System.out.println(name);
Thread.sleep(100);
} catch (InterruptedException e) {
// ignore
}

// measure
int trials = 0;
do {
Expand Down Expand Up @@ -308,6 +316,7 @@ private void printStatsMarkdown(List<String> lines, double volumeMB, String name
String reference = functions.get(name).reference;
boolean isBaseline = reference.equals(name);
String speedupOrBaseline = isBaseline ? "=" : "*";
Character first = baselines.isEmpty() ? null : baselines.values().iterator().next();
System.out.printf("|%-40s|%7.2f|%4.1f %%|%7.2f|%9.2f|%7.2f%s%s|%-8s|\n",
name,
volumeMB * 1e9 / stats.getAverage(),
Expand All @@ -316,7 +325,7 @@ private void printStatsMarkdown(List<String> lines, double volumeMB, String name
stats.getAverage() / lines.size(),
speedup,
speedupOrBaseline,
baselines.getOrDefault(reference, baselines.values().iterator().next()),
baselines.getOrDefault(reference, first),
System.getProperty("java.version")
);
}
Expand All @@ -333,7 +342,7 @@ private void process(List<String> lines, Map<String, BenchmarkFunction> function
for (Map.Entry<String, BenchmarkFunction> entry : entries) {
System.out.print(".");
System.out.flush();
VarianceStatistics warmup = measure(entry.getValue().supplier, WARMUP_NUMBER_OF_TRIALS, WARMUP_CONFIDENCE_LEVEL, WARMUP_CONFIDENCE_INTERVAL_WIDTH, WARMUP_MIN_TRIALS);
VarianceStatistics warmup = measure(entry.getValue().supplier, WARMUP_NUMBER_OF_TRIALS, WARMUP_CONFIDENCE_LEVEL, WARMUP_CONFIDENCE_INTERVAL_WIDTH, WARMUP_MIN_TRIALS, entry.getKey());
warmupResults.put(entry.getKey(), warmup);
}
System.out.println();
Expand All @@ -352,7 +361,7 @@ private void process(List<String> lines, Map<String, BenchmarkFunction> function
System.out.print(".");
System.out.flush();

VarianceStatistics stats = measure(entry.getValue().supplier, MEASUREMENT_NUMBER_OF_TRIALS, MEASUREMENT_CONFIDENCE_LEVEL, MEASUREMENT_CONFIDENCE_INTERVAL_WIDTH, 1);
VarianceStatistics stats = measure(entry.getValue().supplier, MEASUREMENT_NUMBER_OF_TRIALS, MEASUREMENT_CONFIDENCE_LEVEL, MEASUREMENT_CONFIDENCE_INTERVAL_WIDTH, 1, entry.getKey());
results.put(entry.getKey(), stats);
}
System.out.println();
Expand Down

0 comments on commit 3c2f3fe

Please sign in to comment.