|
| 1 | +#!/usr/bin/env perl |
| 2 | +use v5.40; |
| 3 | +use strict; |
| 4 | +use warnings; |
| 5 | +use Benchmark qw(:all); |
| 6 | + |
| 7 | +use Params::Filter qw/filter make_filter/; |
| 8 | + |
| 9 | +say "=" x 80; |
| 10 | +say "Benchmark: Optimized filter() vs make_filter() Closure"; |
| 11 | +say "=" x 80; |
| 12 | +say ""; |
| 13 | +say "Testing if optimized functional approach matches closure performance"; |
| 14 | +say ""; |
| 15 | + |
| 16 | +# Test data |
| 17 | +my $test_data = { |
| 18 | + id => 123, |
| 19 | + name => 'Alice', |
| 20 | + |
| 21 | + phone => '555-1234', |
| 22 | + city => 'NYC', |
| 23 | + password => 'secret', |
| 24 | + extra => 'ignored', |
| 25 | +}; |
| 26 | + |
| 27 | +# ============================================================================ |
| 28 | +# Scenario 1: Required-only (no accepted fields) |
| 29 | +# ============================================================================ |
| 30 | +say "=" x 80; |
| 31 | +say "SCENARIO 1: Required-only (no accepted fields)"; |
| 32 | +say "=" x 80; |
| 33 | +say "Config: required=['id','name','email'], accepted=[], excluded=[]"; |
| 34 | +say ""; |
| 35 | + |
| 36 | +my @req1 = qw/id name email/; |
| 37 | +my @ok1 = (); |
| 38 | +my @no1 = (); |
| 39 | + |
| 40 | +my $closure1 = make_filter(\@req1, \@ok1, \@no1); |
| 41 | + |
| 42 | +cmpthese(-5, { |
| 43 | + 'filter() functional' => sub { |
| 44 | + my $result = filter($test_data, \@req1, \@ok1, \@no1); |
| 45 | + }, |
| 46 | + 'make_filter() closure' => sub { |
| 47 | + my $result = $closure1->($test_data); |
| 48 | + }, |
| 49 | +}); |
| 50 | + |
| 51 | +say ""; |
| 52 | + |
| 53 | +# ============================================================================ |
| 54 | +# Scenario 2: Wildcard (accept all except exclusions) |
| 55 | +# ============================================================================ |
| 56 | +say "=" x 80; |
| 57 | +say "SCENARIO 2: Wildcard (accept all except exclusions)"; |
| 58 | +say "=" x 80; |
| 59 | +say "Config: required=['id','name'], accepted=['*'], excluded=['password']"; |
| 60 | +say ""; |
| 61 | + |
| 62 | +my @req2 = qw/id name/; |
| 63 | +my @ok2 = ('*'); |
| 64 | +my @no2 = qw/password extra/; |
| 65 | + |
| 66 | +my $closure2 = make_filter(\@req2, \@ok2, \@no2); |
| 67 | + |
| 68 | +cmpthese(-5, { |
| 69 | + 'filter() functional' => sub { |
| 70 | + my $result = filter($test_data, \@req2, \@ok2, \@no2); |
| 71 | + }, |
| 72 | + 'make_filter() closure' => sub { |
| 73 | + my $result = $closure2->($test_data); |
| 74 | + }, |
| 75 | +}); |
| 76 | + |
| 77 | +say ""; |
| 78 | + |
| 79 | +# ============================================================================ |
| 80 | +# Scenario 3: Accepted-specific (normal case) |
| 81 | +# ============================================================================ |
| 82 | +say "=" x 80; |
| 83 | +say "SCENARIO 3: Accepted-specific (normal case)"; |
| 84 | +say "=" x 80; |
| 85 | +say "Config: required=['id','name','email'], accepted=['phone','city'], excluded=['password']"; |
| 86 | +say ""; |
| 87 | + |
| 88 | +my @req3 = qw/id name email/; |
| 89 | +my @ok3 = qw/phone city/; |
| 90 | +my @no3 = qw/password/; |
| 91 | + |
| 92 | +my $closure3 = make_filter(\@req3, \@ok3, \@no3); |
| 93 | + |
| 94 | +cmpthese(-5, { |
| 95 | + 'filter() functional' => sub { |
| 96 | + my $result = filter($test_data, \@req3, \@ok3, \@no3); |
| 97 | + }, |
| 98 | + 'make_filter() closure' => sub { |
| 99 | + my $result = $closure3->($test_data); |
| 100 | + }, |
| 101 | +}); |
| 102 | + |
| 103 | +say ""; |
| 104 | + |
| 105 | +# ============================================================================ |
| 106 | +# Scenario 4: Multiple filters (simulating high-volume processing) |
| 107 | +# ============================================================================ |
| 108 | +say "=" x 80; |
| 109 | +say "SCENARIO 4: High-volume processing (100K operations)"; |
| 110 | +say "=" x 80; |
| 111 | +say "Applying 3 different filters to same data"; |
| 112 | +say ""; |
| 113 | + |
| 114 | +cmpthese(-5, { |
| 115 | + 'filter() functional' => sub { |
| 116 | + my $r1 = filter($test_data, \@req1, \@ok1, \@no1); |
| 117 | + my $r2 = filter($test_data, \@req2, \@ok2, \@no2); |
| 118 | + my $r3 = filter($test_data, \@req3, \@ok3, \@no3); |
| 119 | + }, |
| 120 | + 'make_filter() closure' => sub { |
| 121 | + my $r1 = $closure1->($test_data); |
| 122 | + my $r2 = $closure2->($test_data); |
| 123 | + my $r3 = $closure3->($test_data); |
| 124 | + }, |
| 125 | +}); |
| 126 | + |
| 127 | +say ""; |
| 128 | + |
| 129 | +# ============================================================================ |
| 130 | +# Correctness verification |
| 131 | +# ============================================================================ |
| 132 | +say "=" x 80; |
| 133 | +say "CORRECTNESS VERIFICATION"; |
| 134 | +say "=" x 80; |
| 135 | +say ""; |
| 136 | + |
| 137 | +my $f1 = filter($test_data, \@req1, \@ok1, \@no1); |
| 138 | +my $c1 = $closure1->($test_data); |
| 139 | + |
| 140 | +say "Scenario 1 (required-only):"; |
| 141 | +say " filter(): ", join(", ", sort keys %$f1); |
| 142 | +say " make_filter(): ", join(", ", sort keys %$c1); |
| 143 | +say " Match: ", (join(" ", sort keys %$f1) eq join(" ", sort keys %$c1) ? "✅" : "❌"); |
| 144 | +say ""; |
| 145 | + |
| 146 | +my $f2 = filter($test_data, \@req2, \@ok2, \@no2); |
| 147 | +my $c2 = $closure2->($test_data); |
| 148 | + |
| 149 | +say "Scenario 2 (wildcard):"; |
| 150 | +say " filter(): ", join(", ", sort keys %$f2); |
| 151 | +say " make_filter(): ", join(", ", sort keys %$c2); |
| 152 | +say " Match: ", (join(" ", sort keys %$f2) eq join(" ", sort keys %$c2) ? "✅" : "❌"); |
| 153 | +say ""; |
| 154 | + |
| 155 | +my $f3 = filter($test_data, \@req3, \@ok3, \@no3); |
| 156 | +my $c3 = $closure3->($test_data); |
| 157 | + |
| 158 | +say "Scenario 3 (accepted-specific):"; |
| 159 | +say " filter(): ", join(", ", sort keys %$f3); |
| 160 | +say " make_filter(): ", join(", ", sort keys %$c3); |
| 161 | +say " Match: ", (join(" ", sort keys %$f3) eq join(" ", sort keys %$c3) ? "✅" : "❌"); |
| 162 | +say ""; |
| 163 | + |
| 164 | +say "=" x 80; |
| 165 | +say "ANALYSIS"; |
| 166 | +say "=" x 80; |
| 167 | +say ""; |
| 168 | +say "Key insights:"; |
| 169 | +say " • Both interfaces now use identical optimization techniques"; |
| 170 | +say " • Pre-computed exclusion hash (O(1) lookups)"; |
| 171 | +say " • Hash slice for required field copying"; |
| 172 | +say " • Non-destructive operations"; |
| 173 | +say " • Single wildcard check"; |
| 174 | +say ""; |
| 175 | +say "Performance differences expected:"; |
| 176 | +say " • filter() has overhead for input parsing"; |
| 177 | +say " • filter() builds error messages each call"; |
| 178 | +say " • make_filter() closures are pre-compiled"; |
| 179 | +say ""; |
| 180 | +say "Use filter() when you need:"; |
| 181 | +say " - Input format flexibility (hashref, arrayref, scalar)"; |
| 182 | +say " - Detailed error messages"; |
| 183 | +say " - One-time filtering operations"; |
| 184 | +say ""; |
| 185 | +say "Use make_filter() when you need:"; |
| 186 | +say " - Maximum performance"; |
| 187 | +say " - Reusable filters"; |
| 188 | +say " - High-frequency filtering (hot code paths)"; |
| 189 | +say ""; |
0 commit comments