[13.x] Speed up wildcard attribute lookups during validation

Merged
AF
7d ago
+123-1

Summary

Building on the rule-expansion improvements in #60908, this speeds up wildcard lookups during validation.

getPrimaryAttribute() searches expanded attribute lists on every lookup. With cross-field rules such as same:items.*.field1, large batches search the same lists many times.

This change builds a membership set for each wildcard group when first needed, then reuses it. Wildcard precedence and strict key matching stay the same. The cache is cleared when rules or data change, and method signatures remain unchanged.

This helps wildcard rules that reference other fields, including same, different, required_if, required_with and after.

Benchmarks

Synthetic workload with 17 fields per row and 16 cross-field same rules:

RowsBeforeAfterTime reduction
4,0009.80s1.80s82%
8,00054.53s3.69s93%

Both versions already include #60908. Timings cover validator construction and validation on PHP 8.5.8 with CLI OPcache enabled. Results are the median of three fresh-process runs with a small warmup.

The cache uses an extra 2.66 MiB at 4,000 rows and 5.32 MiB at 8,000 rows. Peak PHP memory stayed unchanged in this workload because rule expansion already used more memory.

A separate benchmark using a bulk SMS application's validation rules improved by approximately 21% at 20,000 entries, saving around 4.7 seconds. While 21% may look modest, every second matters when processing payloads this large.