Cached scripts duplicate inline module data before execution

perfloop-oss/windmill · Avoidable Copy

https://perfloop.ai/t/oss/case_2q04r17sne

Verdict

VERIFIED · settled 2026-10-25 · merged as windmill-labs/windmill#12087

What happened: The paired measurements met the required improvement.

Hypothesis

Queued scripts copy their stored inline-module map before passing it by reference into language execution. The copy includes source text, path keys, and optional module lock strings. It may add allocation, CPU work, and peak memory for large module sets. Those costs and input sizes have not been measured.

handle_code_execution_job retains fetched ScriptData through Arc values on the stored-script routes. At line 5892, modules_from_data.clone().or_else(...) clones a present module map. ScriptModule derives Clone and owns content and optional lock Strings, as defined in backend/windmill-types/src/scripts.rs:19-25. The resulting map is passed as &modules to run_language_executor. The extra copy therefore grows with the map's owned key, content, and lock bytes even though the stored data remains alive during execution.

The removable copy is limited to calls that reach module resolution with stored modules present. Earlier exits do not pay it. The preview fallback deserializes a new map and legitimately needs ownership. The existing stored-modules-first rule and preview-only fallback are security-sensitive and must remain unchanged.

The first useful check is a focused ownership and allocation comparison using a byte-counted stored module fixture, followed by the preview fallback. Confirm that borrowing survives the awaited executor call, preserves the selected module contents, and removes the additional payload-sized allocations. Measure allocation and CPU separately from filesystem setup and process launch. Use backend/tests/caller_modules_strip.rs as a security regression control, not as evidence of performance prevalence. Any downstream ownership requirement that forces the same copy would reduce or refute the proposed saving; production module sizes and cadence remain unknown.

Change to test: Borrow modules from the retained ScriptData when present, owning a map only for the permitted preview-argument fallback. Carry that borrowed-or-owned view through executor interfaces while preserving stored-module precedence and the prohibition on caller-supplied modules for deployed jobs.

Where it lives

perfloop-oss/windmill · backend/windmill-worker/src/worker.rs

Evidence

cached Bun ScriptHash job with 16 inline modules · 9 sample pairs

metric baseline candidate paired median change confidence range required result
job_phase_requested_allocated_bytes 18582494 10191722 −45.2% (−8393529) −8404780 to −8389684 gain > 0; gain > 0.05 × |baseline median|; arm medians separated PASSED
module_selection_requested_allocated_bytes 8393719 0 −100% (−8393719) −8393719 to −8393719 gain > 0; gain > 0.05 × |baseline median|; arm medians separated PASSED
module_selection_peak_live_requested_bytes 8393719 0 −100% (−8393719) −8393719 to −8393719 gain > 0; gain > 0.05 × |baseline median|; arm medians separated PASSED

1,000 stored-module selections in a cached Bun job · 11 sample pairs

metric baseline candidate paired median change confidence range required result
module_selection_thread_cpu_ns_per_1000 570000000 0 −100% (−570000000) −620000000 to −560000000 gain > 0; gain > 0.05 × |baseline median|; arm medians separated PASSED

Checks: 1 of 1 passed. Verification: no defect found.

Timeline