Skip to content

Commit

Permalink
perf: return earlier when possible (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimman2003 committed Jun 27, 2023
1 parent b4c0619 commit 12f3f97
Showing 1 changed file with 22 additions and 17 deletions.
Expand Up @@ -83,9 +83,10 @@ public CustomFilterGroup(final SettingsEnum setting, final SettingsEnum filter)
class ByteArrayFilterGroup extends FilterGroup<byte[]> {
// Modified implementation from https://stackoverflow.com/a/1507813
private int indexOf(final byte[] data, final byte[] pattern) {
if (data.length == 0)
return -1;
// Computes the failure function using a boot-strapping process,
// where the pattern is matched against itself.

final int[] failure = new int[pattern.length];

int j = 0;
Expand All @@ -103,7 +104,6 @@ private int indexOf(final byte[] data, final byte[] pattern) {
// KMP matching algorithm.

j = 0;
if (data.length == 0) return -1;

for (int i = 0; i < data.length; i++) {
while (j > 0 && pattern[j] != data[i]) {
Expand All @@ -130,7 +130,8 @@ public ByteArrayFilterGroup(final SettingsEnum setting, final byte[]... filters)
public FilterGroupResult check(final byte[] bytes) {
var matched = false;
for (byte[] filter : filters) {
if (indexOf(bytes, filter) == -1) continue;
if (indexOf(bytes, filter) == -1)
continue;

matched = true;
break;
Expand Down Expand Up @@ -181,7 +182,8 @@ public Spliterator<T> spliterator() {

protected boolean contains(final V stack) {
for (T filterGroup : this) {
if (!filterGroup.isEnabled()) continue;
if (!filterGroup.isEnabled())
continue;

var result = filterGroup.check(stack);
if (result.isFiltered()) {
Expand All @@ -205,7 +207,8 @@ abstract class Filter {
final protected ByteArrayFilterGroupList protobufBufferFilterGroups = new ByteArrayFilterGroupList();

/**
* Check if the given path, identifier or protobuf buffer is filtered by any {@link FilterGroup}.
* Check if the given path, identifier or protobuf buffer is filtered by any
* {@link FilterGroup}.
*
* @return True if filtered, false otherwise.
*/
Expand All @@ -232,34 +235,36 @@ boolean isFiltered(final String path, final String identifier, final byte[] prot
@RequiresApi(api = Build.VERSION_CODES.N)
@SuppressWarnings("unused")
public final class LithoFilterPatch {
private static final Filter[] filters = new Filter[]{
new DummyFilter() // Replaced by patch.
private static final Filter[] filters = new Filter[] {
new DummyFilter() // Replaced by patch.
};

@SuppressWarnings("unused")
public static boolean filter(final StringBuilder pathBuilder, final String identifier, final ByteBuffer protobufBuffer) {
// TODO: Maybe this can be moved to the Filter class, to prevent unnecessary string creation
// because some filters might not need the path.
public static boolean filter(final StringBuilder pathBuilder, final String identifier,
final ByteBuffer protobufBuffer) {
// TODO: Maybe this can be moved to the Filter class, to prevent unnecessary
// string creation
// because some filters might not need the path.
var path = pathBuilder.toString();

// It is assumed that protobufBuffer is empty as well in this case.
if (path.isEmpty()) return false;
if (path.isEmpty())
return false;

LogHelper.printDebug(() -> String.format(
"Searching (ID: %s, Buffer-size: %s): %s",
identifier, protobufBuffer.remaining(), path
));
identifier, protobufBuffer.remaining(), path));

var protobufBufferArray = protobufBuffer.array();

for (var filter : filters) {
var filtered = filter.isFiltered(path, identifier, protobufBufferArray);

LogHelper.printDebug(() ->
String.format("%s (ID: %s): %s", filtered ? "Filtered" : "Unfiltered", identifier, path)
);
LogHelper.printDebug(
() -> String.format("%s (ID: %s): %s", filtered ? "Filtered" : "Unfiltered", identifier, path));

if (filtered) return true;
if (filtered)
return true;
}

return false;
Expand Down

0 comments on commit 12f3f97

Please sign in to comment.