Draft
Conversation
Introduce RequestTimer for per-request phase tracking (init, backend, process, total) exposed via Server-Timing response headers. Add benchmark tooling with --profile mode for collecting timing data. Document phased optimization plan covering streaming architecture, code-level fixes, and open design questions for team review.
Introduce RequestTimer for per-request phase tracking (init, backend, process, total) exposed via Server-Timing response headers. Add benchmark tooling with --profile mode for collecting timing data. Document phased optimization plan covering streaming architecture, code-level fixes, and open design questions for team review.
RequestTimer and Server-Timing header were premature — WASM guest profiling via profile.sh gives better per-function visibility without runtime overhead. Also strips dead --profile mode from benchmark.sh.
build.rs already resolves trusted-server.toml + env vars at compile time and embeds the result. Replace Settings::from_toml() with direct toml::from_str() to skip the config crate pipeline on every request. Profiling confirms: ~5-8% → ~3.3% CPU per request.
- OPTIMIZATION.md: profiling results, CPU breakdown, phased optimization plan covering streaming fixes, config crate elimination, and stream_to_client() architecture - scripts/profile.sh: WASM guest profiling via --profile-guest with Firefox Profiler-compatible output - scripts/benchmark.sh: TTFB analysis, cold start detection, endpoint latency breakdown, and load testing with save/compare support
build.rs already merges trusted-server.toml with TRUSTED_SERVER__* env vars at compile time and embeds the resolved output. from_toml() was re-running the entire config crate pipeline at runtime — redundant since the embedded TOML is already fully resolved. from_toml() now uses direct toml::from_str deserialization. Env var merging moved to build.rs::merge_toml() where it belongs. Profiling confirmed: ~4.8% → ~2.7% CPU per request.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Optimize
Settings::from_toml()by removing redundantconfigcrate env var scanning at runtime. WASM guest profiling confirmed: ~4.8% → ~2.7% CPU per request.Why this is safe
build.rsalready resolves all configuration at compile time:trusted-server.tomlTRUSTED_SERVER__*env vars via theconfigcratetarget/trusted-server-out.tomlcargo:rerun-if-env-changedfor all possible env varsAt runtime,
get_settings()loads this pre-resolved TOML viainclude_bytes!. The oldfrom_toml()was re-running the entireconfigcrate pipeline (env source scanning, config merging, deserialization) on every request — completely redundant since the embedded TOML already contains the final values. On Fastly Compute (WASM sandbox), there are no environment variables available at runtime anyway.What changed
from_toml()now uses directtoml::from_str()deserialization — noconfigcrate, no env var scanningbuild.rs::merge_toml()now owns the env var merging logic inline using theconfigcrate directly, instead of delegating tofrom_toml()configcrate import insettings.rsis now#[cfg(test)]— not compiled into the production binaryWhy some tests use
from_toml_with_env()build.rsruns once before tests start, producing a single statictarget/trusted-server-out.toml. But 7 tests need to verify that differentTRUSTED_SERVER__*env vars correctly override different TOML values per test (e.g., overridingpublisher.origin_urlin one test,integrations.prebid.biddersin another). They can't rely onbuild.rsfor this because:build.rsruns once, not per-testtemp_envthatbuild.rsnever seesSo these 7 tests call
from_toml_with_env()directly — a#[cfg(test)]helper that preserves the original env var merging behavior. This lets them test the merging contract in isolation without going throughbuild.rs.All other tests (20+) use the new fast
from_toml()and continue passing — they only test TOML parsing, not env var overrides.Files changed
settings.rsfrom_toml()→toml::from_str; added#[cfg(test)] from_toml_with_env();configimport →#[cfg(test)]build.rsfrom_toml())Cargo.tomltomlto[dependencies]Test plan
cargo test— 352 tests passget_settings()BENCH_URL=<staging> ./scripts/benchmark.shand compare against baselinebuild.rsenv var merging matches originalfrom_toml()behavior