Skip to content

perf: index fixed string lists in the in operator - #72

Open
siddarthaboddu wants to merge 1 commit into
jamsesso:mainfrom
siddarthaboddu:codex/index-fixed-string-in
Open

siddarthaboddu wants to merge 1 commit into
jamsesso:mainfrom
siddarthaboddu:codex/index-fixed-string-in

Conversation

@siddarthaboddu

Copy link
Copy Markdown

A filter can evaluate the same list of allowed codes once for every record. For lists containing at least 16 literal strings, build a membership index when parsing the rule and reuse it for string or null lookups. Returning a mutable iterator or sublist discards the index so subsequent evaluations observe edits.

Performance

Single-threaded JMH 1.37 on Java 21. Both builds use cached rules and identical inputs. A filter operation processes all 256 records. The last two rows measure one lookup in a Java list supplied as data.

Workload Main (49995a7) Candidate
256 records × 256 fixed codes, half match 10.36 ms 147.2 µs
256 records × 256 fixed codes, all missing the code field 10.43 ms 137.4 µs
Caller-supplied 4K list, first item matches 27.1 µs 28.0 µs
Caller-supplied 4K list, value absent 30.9 µs 30.6 µs

Compatibility and tradeoffs

The same 119 JUnit tests passed on main and the candidate on Java 8 and 21. All 843 differential cases matched on each JVM, including exception types, messages and paths. Twenty parsed-rule mutation cases passed on both builds. Tests also cover custom operators, localized paths and exceptions from later data-list reads. The index uses additional memory for the lifetime of an eligible cached rule. The intended speedup is for repeated lookups in fixed string lists.

Reproduce the filter benchmark

Compile the following class with JMH 1.37 and its annotation processor against each revision. Run on Java 21 with the same heap and GC profiler.

java -cp "<benchmark classes and dependencies>" org.openjdk.jmh.Main 'bench.FilterCodes.*' -t 1 -f 2 -wi 2 -i 3 -w 400ms -r 400ms -jvmArgs '-Xms512m -Xmx512m' -prof gc
package bench;

import io.github.jamsesso.jsonlogic.JsonLogic;
import org.openjdk.jmh.annotations.*;
import java.util.*;
import java.util.concurrent.TimeUnit;

@State(Scope.Thread)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class FilterCodes {
  private final JsonLogic logic = new JsonLogic();
  private final List<Map<String,String>> rows = new ArrayList<>();
  private final List<Map<String,String>> missingRows = new ArrayList<>();
  private String rule;

  @Setup public void setup() throws Exception {
    StringJoiner codes = new StringJoiner(",");
    for (int i = 0; i < 256; i++) {
      codes.add("\"code" + i + "\"");
      rows.add(Collections.singletonMap("code", i % 2 == 0 ? "code" + i : "other"));
      missingRows.add(Collections.emptyMap());
    }
    rule = "{\"filter\":[{\"var\":\"\"},{\"in\":[{\"var\":\"code\"},[" + codes + "]]}]}";
    if (mixed().size() != 128 || !missing().isEmpty()) throw new AssertionError();
  }

  @Benchmark public List<?> mixed() throws Exception { return (List<?>) logic.apply(rule, rows); }
  @Benchmark public List<?> missing() throws Exception { return (List<?>) logic.apply(rule, missingRows); }
}

Build a membership index for lists of at least 16 string literals so repeated evaluations can reuse it. Discard the index when an iterator or sublist can mutate the parsed list.

Cover localized error paths, custom operations, missing codes, and edits to parsed rules.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant