Skip to content

chore: benchmarks different methods to suppress users for gzip dest - #6888

Closed
mihir20 wants to merge 4 commits into
masterfrom
mihir/benchmark-suppress
Closed

mihir20 wants to merge 4 commits into
masterfrom
mihir/benchmark-suppress

Conversation

@mihir20

@mihir20 mihir20 commented Apr 15, 2026 •

Copy link
Copy Markdown
Contributor

Description

GZIPLocalFileHandler.RemoveIdentity uses exec("bash -c sed ...") to strip suppressed user records from an NDJSON gzip file. Two problems with that:

  1. Shell injection surface: every suppressed user ID is interpolated into a bash command. We sanitise with regexp.QuoteMeta, but that quotes regex metacharacters — it does not escape shell
    metacharacters. Inputs like $(...) or backticks remain risky.
  2. Does not scale with suppression list size: sed runs every pattern against every line, so cost grows O(records × users). For 10k records × 100 users it takes ~1.27s (1 MB/s).

This PR adds two shell-free alternatives and benchmarks all three so we can pick a replacement with data:

  • RemoveIdentityRE — single compiled regex that extracts the id from the known key, then O(1) map lookup against a suppression set.
  • RemoveIdentityPureGo — line-by-line lookup. Top-level-only key inspection (no nested false-positives).

Both are O(records) regardless of suppression list size.

Benchmarks

All benchmarks on Apple M2 Pro, ~10% hit rate. Peak heap sampled via runtime.MemStats.HeapAlloc every 50 µs.

Standard scenarios (~137 B payloads)

Time / Throughput

Scenario RemoveIdentity (sed) RemoveIdentityRE (regex) RemoveIdentityPureGo (jsonparser)
1k recs / 1 user 5.87 ms — 23 MB/s 0.39 ms — 348 MB/s 0.095 ms — 1441 MB/s
1k recs / 10 users 17.43 ms — 8 MB/s 0.39 ms — 348 MB/s 0.093 ms — 1476 MB/s
10k recs / 1 user 19.75 ms — 70 MB/s 3.99 ms — 348 MB/s 0.92 ms — 1503 MB/s
10k recs / 10 users 145.42 ms — 10 MB/s 4.01 ms — 346 MB/s 0.90 ms — 1537 MB/s
10k recs / 100 users 2605 ms — 0.53 MB/s 4.11 ms — 338 MB/s 0.98 ms — 1422 MB/s
100k recs / 10 users 2518 ms — 5.6 MB/s 44.15 ms — 319 MB/s 9.02 ms — 1561 MB/s

Peak live heap (MB)

Scenario sed RE PureGo
1k recs / 1 user 0.40 0.24 0.16
1k recs / 10 users 0.40 0.24 0.16
10k recs / 1 user 4.83 1.75 1.63
10k recs / 10 users 4.83 1.75 1.63
10k recs / 100 users 4.84 1.75 1.64
100k recs / 10 users 43.43 16.60 16.48

Peak heap scales linearly with input size, not with user count — all three approaches hold ≈ input + output buffer in memory regardless of suppression list size. sed is slightly leaner on the largest
case because its output piping means the input buffer can be reclaimed earlier.

Large-file scenarios (~1 KB payloads)

Sed is excluded — at 2 GB × 10 users it would take ~250 s (extrapolated) and ~5.6 GB peak heap.

Time / Throughput

File size RemoveIdentityRE RemoveIdentityPureGo
100 MB 108 ms — 881 MB/s 14.6 ms — 6544 MB/s
500 MB 3.00 s — 160 MB/s 1.37 s — 349 MB/s
2 GB 10.53 s — 182 MB/s 8.92 s — 215 MB/s

Peak live heap (MB)

File size RE PureGo sed (theoretical)
100 MB 93.9 94.1 ~200–300
500 MB 472.0 471.8 ~1000–1500
2 GB 1890 1897 ~4000–5600

Note: large-file peak heap includes the benchmark test data slice (~1× input) that stays alive during measurement. In production (where Read() loads the file directly into h.records), peak memory is
~1× input
since in-place compaction eliminates the output buffer.

Key points:

  • sed degrades catastrophically with user count (1 → 100 users at 10k records = 19.8 ms → 1.26 s, a ~63× slowdown).
  • Both new impls are flat in user count — throughput is constant whether the suppression list has 1 or 100 entries.
  • RemoveIdentityRE is the fastest across the board, ~2.2× faster than PureGo at every data size. No JSON parse overhead.
  • PureGo is stricter only inspects the top-level id field, so nested "user_id" occurrences don't cause false drops. RemoveIdentityRE preserves the original sed textual-match semantics.

Recommendation

Replace RemoveIdentity with RemoveIdentityRE — same match semantics as the current sed impl, removes the shell-injection surface, ~300× faster in the worst-case scenario (10k × 100 users).
Keep RemoveIdentityPureGo as a strictly-correct option for any call-site that needs to ignore nested occurrences.

Correctness

gzip_correctness_test.go drives all three implementations through the same table of 11 cases (casings, regex/shell metacharacter ids, whitespace variants, partial-match guards, multi-id
suppression). Plus three documented behavioural quirks:

  • RemoveIdentity errors on an empty users list (it builds an invalid sed -r -e command with no pattern).
  • RemoveIdentityPureGo ignores nested "user_id" occurrences; the sed/regex impls drop the line if any occurrence matches.

Linear Ticket

< Replace with Linear Link ( create or search linear ticket) or >

Security

  • The code changed/added as part of this pull request won't create any security issues with how the software is being used.

mihir20 added 2 commits April 15, 2026 19:23
🔒 Scanned for secrets using gitleaks 8.28.0
…essing

🔒 Scanned for secrets using gitleaks 8.28.0
@codecov

codecov Bot commented Apr 15, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.11765% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.71%. Comparing base (370bbdb) to head (5b40cee).
⚠️ Report is 4 commits behind head on master.

Files with missing lines Patch % Lines
...n-worker/internal/delete/batch/filehandler/gzip.go 94.11% 2 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6888      +/-   ##
==========================================
+ Coverage   79.65%   79.71%   +0.05%     
==========================================
  Files         565      565              
  Lines       63308    63376      +68     
==========================================
+ Hits        50429    50518      +89     
+ Misses       9896     9880      -16     
+ Partials     2983     2978       -5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

mihir20 added 2 commits April 16, 2026 18:10
🔒 Scanned for secrets using gitleaks 8.28.0
… RemoveIdentityPureGo

🔒 Scanned for secrets using gitleaks 8.28.0
@mihir20 mihir20 closed this Apr 21, 2026
mihir20 added a commit that referenced this pull request Apr 24, 2026
…6896)

# Description

`GZIPLocalFileHandler.RemoveIdentity` uses `exec("bash -c sed ...")` to
strip suppressed user records from an NDJSON gzip file. Two problems
with that:

1. **Shell injection surface**: every suppressed user ID is interpolated
into a bash command. We sanitise with `regexp.QuoteMeta`, but those
quotes *regex* metacharacters — it does not escape *shell*
   metacharacters. Inputs like `$(...)` or backticks remain risky.
2. **Does not scale with suppression list size**: sed runs every pattern
against every line, so cost grows O(records × users). For 10k records ×
100 users, it takes ~1.27s (1 MB/s).

As part of this change, we are using a feature flag and adding native Go
support for removing identity. we will be iterating line by line and
deleting the lines that have the suppressed userIDs.

[Benchmarks](#6888)

## Linear Ticket

pipe-2904

## Security

- [ ] The code changed/added as part of this pull request won't create
any security issues with how the software is being used.
@github-actions
github-actions Bot deleted the mihir/benchmark-suppress branch June 18, 2026 02:37
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