perf: return earlier when possible (#427)

This commit is contained in:
Jim Man 2023-06-27 04:28:29 +03:00 committed by GitHub
parent b4c0619abb
commit 12f3f97552
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -83,9 +83,10 @@ final class CustomFilterGroup extends StringFilterGroup {
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;
@ -103,7 +104,6 @@ class ByteArrayFilterGroup extends FilterGroup<byte[]> {
// 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]) {
@ -130,7 +130,8 @@ class ByteArrayFilterGroup extends FilterGroup<byte[]> {
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;
@ -181,7 +182,8 @@ abstract class FilterGroupList<V, T extends FilterGroup<V>> implements Iterable<
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()) {
@ -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.
*/
@ -232,34 +235,36 @@ abstract class Filter {
@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;