Skip to content

Speed up calling map, filter, sort, and the other callables that need to know whether the sandbox is enabled - #5036

Open
Kocal wants to merge 1 commit into
twigphp:3.xfrom
Kocal:perf/compile-time-sandbox-extension-check
Open

Kocal wants to merge 1 commit into
twigphp:3.xfrom
Kocal:perf/compile-time-sandbox-extension-check

Conversation

@Kocal

@Kocal Kocal commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

Each call site of a callable declared with needs_is_sandboxed compiled to $this->env->hasExtension(SandboxExtension::class) && ...->isSandboxed($this->source), so hasExtension() ran on every call even without the sandbox.

The registered extensions are part of the compiled template cache key, so the compiler now writes false when the sandbox extension is not registered, and only the isSandboxed() call when it is. GetAttrExpression already relies on the same fact, and this changes the generated code.

A template with 600 such calls goes from 0.479-0.492 -> 0.407-0.437 ms per render, with no measurable change on a template with 4 calls.

…t need to know whether the sandbox is enabled

Each call site of a callable declared with `needs_is_sandboxed` compiled to `$this->env->hasExtension(SandboxExtension::class) && ...->isSandboxed($this->source)`, so `hasExtension()` ran on every call even without the sandbox.

The registered extensions are part of the compiled template cache key, so the compiler now writes `false` when the sandbox extension is not registered, and only the `isSandboxed()` call when it is. `GetAttrExpression` already relies on the same fact, and this changes the generated code.

A template with 600 such calls goes from 0.479-0.492 -> 0.407-0.437 ms per render, with no measurable change on a template with 4 calls.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants