-
Notifications
You must be signed in to change notification settings - Fork 47
3) Add a dedicated fragment splitter for the cl100k encoding - 3.8s to 2.1s #77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
89d6212
Add a dedicated fragment splitter for the cl100k encoding
5d177b6
Avoid primitive boxing
9f56cf5
Add IntArrayList to store tokens without boxing
58b8de1
Replace internal TreeMap with faster LinkedHashMap iteration without …
36c5fa7
Simplify TokenEncoder.encode when we're attempting to encode the whol…
72e7aad
Update non-standard csv examples
0411aaa
Remove validRanks, there are simpler ways to keep track of iteration …
d04d660
Polish TokenEncoderLarge
7813452
Cover remaining primitive list methods with tests fully
d64b2f3
Merge branch 'main' into optimize-token-splitter
l0rinc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 29 additions & 27 deletions
56
benchmark/src/jmh/java/com/knuddels/jtokkit/AbstractMultiThreadedBenchmark.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,46 +1,48 @@ | ||
| package com.knuddels.jtokkit; | ||
|
|
||
| import com.knuddels.jtokkit.api.Encoding; | ||
| import com.knuddels.jtokkit.api.IntArrayList; | ||
| import org.openjdk.jmh.annotations.Scope; | ||
| import org.openjdk.jmh.annotations.Setup; | ||
| import org.openjdk.jmh.annotations.State; | ||
| import org.openjdk.jmh.annotations.TearDown; | ||
|
|
||
| import java.util.List; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.stream.Collectors; | ||
| import org.openjdk.jmh.annotations.Scope; | ||
| import org.openjdk.jmh.annotations.Setup; | ||
| import org.openjdk.jmh.annotations.State; | ||
| import org.openjdk.jmh.annotations.TearDown; | ||
|
|
||
| @State(Scope.Thread) | ||
| public abstract class AbstractMultiThreadedBenchmark extends AbstractBenchmark { | ||
|
|
||
| private final int threads; | ||
| private ExecutorService executor; | ||
| private final int threads; | ||
| private ExecutorService executor; | ||
|
|
||
| public AbstractMultiThreadedBenchmark(final int threads) { | ||
| this.threads = threads; | ||
| } | ||
| public AbstractMultiThreadedBenchmark(int threads) { | ||
| this.threads = threads; | ||
| } | ||
|
|
||
| @Setup | ||
| public void setup() { | ||
| executor = Executors.newFixedThreadPool(threads); | ||
| } | ||
| @Setup | ||
| public void setup() { | ||
| executor = Executors.newFixedThreadPool(threads); | ||
| } | ||
|
|
||
| @TearDown | ||
| public void tearDown() { | ||
| executor.shutdown(); | ||
| } | ||
| @TearDown | ||
| public void tearDown() { | ||
| executor.shutdown(); | ||
| } | ||
|
|
||
| @Override | ||
| protected List<List<Integer>> encodeAll(final Encoding encoding, final List<String> fileContents) { | ||
| final var futures = fileContents.stream() | ||
| .map(it -> CompletableFuture.supplyAsync(() -> encoding.encode(it), executor)) | ||
| .collect(Collectors.toList()); | ||
| @Override | ||
| protected List<IntArrayList> encodeAll(Encoding encoding, List<String> fileContents) { | ||
| var futures = fileContents.stream() | ||
| .map(it -> CompletableFuture.supplyAsync(() -> encoding.encode(it), executor)) | ||
| .toList(); | ||
|
|
||
| CompletableFuture.allOf(futures.toArray(CompletableFuture[]::new)).join(); | ||
| CompletableFuture.allOf(futures.toArray(CompletableFuture[]::new)).join(); | ||
|
|
||
| return futures.stream() | ||
| .map(CompletableFuture::join) | ||
| .collect(Collectors.toList()); | ||
| } | ||
| return futures.stream() | ||
| .map(CompletableFuture::join) | ||
| .collect(Collectors.toList()); | ||
| } | ||
| } |
64 changes: 64 additions & 0 deletions
64
benchmark/src/jmh/java/com/knuddels/jtokkit/Cl100kParserBenchmark.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package com.knuddels.jtokkit; | ||
|
|
||
|
|
||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.infra.Blackhole; | ||
|
|
||
| public class Cl100kParserBenchmark { | ||
| @Benchmark | ||
| public void benchmarkIsLetter(BenchmarkingState state, Blackhole bh) { | ||
| for (var fileContent : state.fileContents) { | ||
| fileContent.codePoints().forEachOrdered(cp -> bh.consume(Cl100kParser.isLetter(cp))); | ||
| } | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void benchmarkIsLetterOrNumeric(BenchmarkingState state, Blackhole bh) { | ||
| for (var fileContent : state.fileContents) { | ||
| fileContent.codePoints().forEachOrdered(cp -> bh.consume(Cl100kParser.isLetterOrNumeric(cp))); | ||
| } | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void benchmarkIsNewline(BenchmarkingState state, Blackhole bh) { | ||
| for (var fileContent : state.fileContents) { | ||
| fileContent.codePoints().forEachOrdered(cp -> bh.consume(Cl100kParser.isNewline(cp))); | ||
| } | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void benchmarkIsNotNewlineOrLetterOrNumeric(BenchmarkingState state, Blackhole bh) { | ||
| for (var fileContent : state.fileContents) { | ||
| fileContent.codePoints().forEachOrdered(cp -> bh.consume(Cl100kParser.isNotNewlineOrLetterOrNumeric(cp))); | ||
| } | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void benchmarkIsNotWhitespaceOrLetterOrNumeric(BenchmarkingState state, Blackhole bh) { | ||
| for (var fileContent : state.fileContents) { | ||
| fileContent.codePoints().forEachOrdered(cp -> bh.consume(Cl100kParser.isNotWhitespaceOrLetterOrNumeric(cp))); | ||
| } | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void benchmarkIsNumeric(BenchmarkingState state, Blackhole bh) { | ||
| for (var fileContent : state.fileContents) { | ||
| fileContent.codePoints().forEachOrdered(cp -> bh.consume(Cl100kParser.isNumeric(cp))); | ||
| } | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void benchmarkIsWhitespace(BenchmarkingState state, Blackhole bh) { | ||
| for (var fileContent : state.fileContents) { | ||
| fileContent.codePoints().forEachOrdered(cp -> bh.consume(Cl100kParser.isWhitespace(cp))); | ||
| } | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void benchmarkToUtf8Conversion(BenchmarkingState state, Blackhole bh) { | ||
| var dst = new ByteArrayList(); | ||
| for (var fileContent : state.fileContents) { | ||
| bh.consume(Cl100kParser.addUtf8Bytes(fileContent, 0, fileContent.length(), dst)); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
lib/src/main/java/com/knuddels/jtokkit/ByteArrayList.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| package com.knuddels.jtokkit; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| public class ByteArrayList { | ||
tox-p marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private byte[] array; | ||
| private int size = 0; | ||
|
|
||
| public ByteArrayList() { | ||
| this(10); | ||
| } | ||
|
|
||
| public ByteArrayList(int size) { | ||
| array = new byte[size]; | ||
| } | ||
|
|
||
| public void clear() { | ||
tox-p marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| size = 0; | ||
| } | ||
|
|
||
| public void add(byte element) { | ||
| if (size >= array.length) { | ||
| resize(); | ||
| } | ||
| array[size++] = element; | ||
| } | ||
|
|
||
| public byte get(int index) { | ||
| return array[index]; | ||
| } | ||
|
|
||
| public int set(int index, byte element) { | ||
| int old = array[index]; | ||
| array[index] = element; | ||
| return old; | ||
| } | ||
|
|
||
| private void resize() { | ||
| ensureCapacity(Math.max(1, array.length) * 2); | ||
tox-p marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public void ensureCapacity(int targetSize) { | ||
| if (targetSize <= size) { | ||
| return; | ||
| } | ||
| byte[] newArray = new byte[targetSize]; | ||
| if (size > 0) { | ||
| System.arraycopy(array, 0, newArray, 0, size); | ||
| } | ||
| array = newArray; | ||
| } | ||
|
|
||
| public int size() { | ||
| return size; | ||
| } | ||
|
|
||
| public boolean isEmpty() { | ||
| return size == 0; | ||
| } | ||
|
|
||
| public byte[] toArray() { | ||
| return Arrays.copyOf(array, size); | ||
tox-p marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public List<Byte> boxed() { | ||
| List<Byte> list = new ArrayList<>(size); | ||
| for (int i = 0; i < size; i++) { | ||
| list.add(array[i]); | ||
| } | ||
| return list; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } else if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| ByteArrayList that = (ByteArrayList) o; | ||
| if (size != that.size) { | ||
| return false; | ||
| } | ||
| for (int i = 0; i < size; i++) { | ||
| if (array[i] != that.array[i]) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
tox-p marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| int result = 1; | ||
| for (int i = 0; i < size; i++) { | ||
| result = 31 * result + array[i]; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return boxed().toString(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.