Skip to content
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

More optimizations #7

Merged
merged 4 commits into from
Oct 10, 2023
Merged
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
38 changes: 25 additions & 13 deletions src/main/java/com/teragrep/blf_01/ConcatenatedToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,40 @@

package com.teragrep.blf_01;

import java.nio.ByteBuffer;
import java.util.ArrayList;

public class ConcatenatedToken {

ArrayList<Token> tokens;
public ConcatenatedToken(ArrayList<Token> tokens) {
this.tokens = tokens;
private ByteBuffer concatenatedBuffer;

public ConcatenatedToken() {
this.concatenatedBuffer = ByteBuffer.allocateDirect(256*1024);
}

byte[] concatenate() {
int size = 0;
byte[] concatenate(ArrayList<Token> tokens) {
concatenatedBuffer.clear();
for (Token token : tokens) {
size = size + token.bytes.length;
if (concatenatedBuffer.position() + token.bytes.length >= concatenatedBuffer.capacity()) {
int size = 0;
for (Token tokenForSize : tokens) {
size = size + tokenForSize.bytes.length;
}
concatenatedBuffer = extendBuffer(concatenatedBuffer, size);
}
concatenatedBuffer.put(token.bytes);
}
byte[] bytes = new byte[size];

int pos = 0;
for (Token token : tokens) {
System.arraycopy(token.bytes, 0, bytes, pos, token.bytes.length);
pos = pos + token.bytes.length;
}
concatenatedBuffer.flip();
byte[] rv = new byte[concatenatedBuffer.remaining()];
concatenatedBuffer.get(rv);
return rv;
}

return bytes;
private ByteBuffer extendBuffer(ByteBuffer byteBuffer, int size) {
ByteBuffer newBuffer = ByteBuffer.allocateDirect(byteBuffer.capacity() + size);
byteBuffer.flip();
newBuffer.put(byteBuffer);
return newBuffer;
}
}
6 changes: 4 additions & 2 deletions src/main/java/com/teragrep/blf_01/Entanglement.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ public class Entanglement {
private final ArrayList<Token> allTokens;
private final ArrayList<Token> forwardScanTokens;
private final ArrayList<Token> backwardsScanTokens;
private final ConcatenatedToken concatenatedToken;
public Entanglement() {
this.endWindowScanTokens = new ArrayList<>(512);
this.allTokens = new ArrayList<>(512);
this.forwardScanTokens = new ArrayList<>(512);
this.backwardsScanTokens = new ArrayList<>(512);
this.concatenatedToken = new ConcatenatedToken();
}

public ArrayList<Token> entangle(ArrayList<Token> tokens) {
Expand All @@ -86,7 +88,7 @@ private ArrayList<Token> startWindowScan(ArrayList<Token> tokenList) {
// +++++ subtask endWindowScan
allTokens.addAll(endWindowScan(forwardScanTokens));
// -----
Token concatenated = new Token(new ConcatenatedToken(forwardScanTokens).concatenate());
Token concatenated = new Token(concatenatedToken.concatenate(forwardScanTokens));
allTokens.add(concatenated);
// -----
}
Expand All @@ -108,7 +110,7 @@ private ArrayList<Token> endWindowScan(ArrayList<Token> tokenList) {
while (backwardIterator.hasPrevious()) {
backwardsScanTokens.add(0, backwardIterator.previous());
}
endWindowScanTokens.add(new Token(new ConcatenatedToken(backwardsScanTokens).concatenate()));
endWindowScanTokens.add(new Token(concatenatedToken.concatenate(backwardsScanTokens)));
// ----- task
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/teragrep/blf_01/ConcatenatedTokenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ public void testConcatenation() {
Token token3 = new Token("cd");
tokenList.add(token3);

ConcatenatedToken concatenatedToken = new ConcatenatedToken(tokenList);
ConcatenatedToken concatenatedToken = new ConcatenatedToken();

byte[] expected = "ab.cd".getBytes(StandardCharsets.UTF_8);
Assertions.assertArrayEquals(expected,concatenatedToken.concatenate());
Assertions.assertArrayEquals(expected,concatenatedToken.concatenate(tokenList));
}
}