Skip to content

chore(stdlib): Resolve bool and int constants during compilation#1803

Open
JakubOnderka wants to merge 2 commits into
vectordotdev:mainfrom
JakubOnderka:stdlib-resolve
Open

chore(stdlib): Resolve bool and int constants during compilation#1803
JakubOnderka wants to merge 2 commits into
vectordotdev:mainfrom
JakubOnderka:stdlib-resolve

Conversation

@JakubOnderka

@JakubOnderka JakubOnderka commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

Summary

For simple method, resolving function parameters during runtime that are usually not defined or constants can take significant amount of time. This patch checks if these parameters are constants and if yes, resolve then in compilation phase. The advantage is also that variable type in that case is checked during compilation.

Benchmark results

compact

vrl_stdlib/functions/compact/array
                        time:   [55.689 ns 55.746 ns 55.812 ns]
                        thrpt:  [17.917 Melem/s 17.938 Melem/s 17.957 Melem/s]
                 change:
                        time:   [−36.734% −35.852% −34.619%] (p = 0.00 < 0.05)
                        thrpt:  [+52.950% +55.889% +58.063%]
                        Performance has improved.
vrl_stdlib/functions/compact/map
                        time:   [212.83 ns 213.84 ns 215.70 ns]
                        thrpt:  [4.6360 Melem/s 4.6765 Melem/s 4.6986 Melem/s]
                 change:
                        time:   [−18.066% −17.031% −15.958%] (p = 0.00 < 0.05)
                        thrpt:  [+18.988% +20.527% +22.049%]
                        Performance has improved.

del

vrl_stdlib/functions/del/default
                        time:   [120.36 ns 122.95 ns 125.23 ns]
                        thrpt:  [7.9853 Melem/s 8.1331 Melem/s 8.3081 Melem/s]
                 change:
                        time:   [−8.0325% −6.6988% −5.2953%] (p = 0.00 < 0.05)
                        thrpt:  [+5.5914% +7.1798% +8.7340%]
                        Performance has improved.
vrl_stdlib/functions/del/compact
                        time:   [204.08 ns 205.46 ns 207.03 ns]
                        thrpt:  [4.8302 Melem/s 4.8670 Melem/s 4.9001 Melem/s]
                 change:
                        time:   [−7.5981% −6.6643% −5.7761%] (p = 0.00 < 0.05)
                        thrpt:  [+6.1301% +7.1401% +8.2228%]
                        Performance has improved.

replace

vrl_stdlib/functions/replace/string
                        time:   [50.102 ns 50.335 ns 50.655 ns]
                        thrpt:  [19.741 Melem/s 19.867 Melem/s 19.959 Melem/s]
                 change:
                        time:   [−10.736% −10.389% −10.026%] (p = 0.00 < 0.05)
                        thrpt:  [+11.144% +11.594% +12.028%]
                        Performance has improved.
vrl_stdlib/functions/replace/regex
                        time:   [109.49 ns 109.68 ns 109.88 ns]
                        thrpt:  [9.1007 Melem/s 9.1176 Melem/s 9.1335 Melem/s]
                 change:
                        time:   [−10.824% −9.0012% −7.5097%] (p = 0.00 < 0.05)
                        thrpt:  [+8.1194% +9.8916% +12.137%]
                        Performance has improved.

log

vrl_stdlib/functions/log/literal
                        time:   [10.571 ns 10.591 ns 10.618 ns]
                        thrpt:  [94.180 Melem/s 94.418 Melem/s 94.596 Melem/s]
                 change:
                        time:   [−23.837% −23.483% −22.978%] (p = 0.00 < 0.05)
                        thrpt:  [+29.832% +30.689% +31.297%]
                        Performance has improved.

starts_with

vrl_stdlib/functions/starts_with/case_sensitive
                        time:   [16.927 ns 16.992 ns 17.089 ns]
                        thrpt:  [58.517 Melem/s 58.850 Melem/s 59.079 Melem/s]
                 change:
                        time:   [−29.057% −28.704% −28.273%] (p = 0.00 < 0.05)
                        thrpt:  [+39.417% +40.260% +40.957%]
                        Performance has improved.
vrl_stdlib/functions/starts_with/case_insensitive
                        time:   [22.146 ns 22.178 ns 22.212 ns]
                        thrpt:  [45.020 Melem/s 45.089 Melem/s 45.156 Melem/s]
                 change:
                        time:   [−27.875% −27.625% −27.423%] (p = 0.00 < 0.05)
                        thrpt:  [+37.785% +38.169% +38.648%]
                        Performance has improved.

find

vrl_stdlib/functions/find/str_matching
                        time:   [20.990 ns 21.342 ns 21.713 ns]
                        thrpt:  [46.055 Melem/s 46.856 Melem/s 47.642 Melem/s]
                 change:
                        time:   [−12.793% −11.362% −9.9652%] (p = 0.00 < 0.05)
                        thrpt:  [+11.068% +12.819% +14.670%]
                        Performance has improved.
vrl_stdlib/functions/find/str_too_long
                        time:   [17.727 ns 17.994 ns 18.279 ns]
                        thrpt:  [54.709 Melem/s 55.574 Melem/s 56.410 Melem/s]
                 change:
                        time:   [−15.983% −14.959% −13.915%] (p = 0.00 < 0.05)
                        thrpt:  [+16.164% +17.591% +19.024%]
                        Performance has improved.
vrl_stdlib/functions/find/regex_matching_start
                        time:   [47.567 ns 47.938 ns 48.348 ns]
                        thrpt:  [20.683 Melem/s 20.860 Melem/s 21.023 Melem/s]
                 change:
                        time:   [−5.0520% −4.2456% −3.3554%] (p = 0.00 < 0.05)
                        thrpt:  [+3.4719% +4.4339% +5.3208%]
                        Change within noise threshold.

contains

vrl_stdlib/functions/contains/case_sensitive
                        time:   [34.275 ns 34.357 ns 34.448 ns]
                        thrpt:  [29.029 Melem/s 29.106 Melem/s 29.176 Melem/s]
                 change:
                        time:   [−17.199% −16.570% −15.999%] (p = 0.00 < 0.05)
                        thrpt:  [+19.047% +19.861% +20.771%]
                        Performance has improved.
vrl_stdlib/functions/contains/case_insensitive
                        time:   [55.680 ns 56.032 ns 56.472 ns]
                        thrpt:  [17.708 Melem/s 17.847 Melem/s 17.960 Melem/s]
                 change:
                        time:   [−11.270% −10.369% −9.4892%] (p = 0.00 < 0.05)
                        thrpt:  [+10.484% +11.569% +12.702%]
                        Performance has improved.

contains_all

vrl_stdlib/functions/contains_all/case_sensitive
                        time:   [58.320 ns 58.572 ns 58.901 ns]
                        thrpt:  [16.978 Melem/s 17.073 Melem/s 17.147 Melem/s]
                 change:
                        time:   [−10.723% −10.144% −9.6083%] (p = 0.00 < 0.05)
                        thrpt:  [+10.630% +11.289% +12.011%]
                        Performance has improved.
vrl_stdlib/functions/contains_all/case_insensitive
                        time:   [78.815 ns 80.079 ns 81.745 ns]
                        thrpt:  [12.233 Melem/s 12.488 Melem/s 12.688 Melem/s]
                 change:
                        time:   [−32.477% −27.657% −23.524%] (p = 0.00 < 0.05)
                        thrpt:  [+30.760% +38.231% +48.097%]
                        Performance has improved.

ends_with

vrl_stdlib/functions/ends_with/case_sensitive
                        time:   [32.750 ns 33.122 ns 33.556 ns]
                        thrpt:  [29.801 Melem/s 30.191 Melem/s 30.535 Melem/s]
                 change:
                        time:   [−17.446% −16.664% −15.714%] (p = 0.00 < 0.05)
                        thrpt:  [+18.644% +19.997% +21.133%]
                        Performance has improved.
vrl_stdlib/functions/ends_with/case_insensitive
                        time:   [57.567 ns 58.363 ns 59.238 ns]
                        thrpt:  [16.881 Melem/s 17.134 Melem/s 17.371 Melem/s]
                 change:
                        time:   [−4.7430% −3.5296% −2.3262%] (p = 0.00 < 0.05)
                        thrpt:  [+2.3816% +3.6588% +4.9792%]
                        Change within noise threshold.

split

vrl_stdlib/functions/split/string
                        time:   [97.414 ns 97.544 ns 97.669 ns]
                        thrpt:  [10.239 Melem/s 10.252 Melem/s 10.266 Melem/s]
                 change:
                        time:   [−6.8591% −6.0057% −5.3581%] (p = 0.00 < 0.05)
                        thrpt:  [+5.6614% +6.3894% +7.3642%]
                        Performance has improved.
vrl_stdlib/functions/split/regex
                        time:   [100.40 ns 101.05 ns 102.09 ns]
                        thrpt:  [9.7952 Melem/s 9.8958 Melem/s 9.9600 Melem/s]
                 change:
                        time:   [−4.9707% −4.6059% −4.1117%] (p = 0.00 < 0.05)
                        thrpt:  [+4.2880% +4.8283% +5.2307%]
                        Change within noise threshold.

parse_json

vrl_stdlib/functions/parse_json/map
                        time:   [86.202 ns 86.285 ns 86.363 ns]
                        thrpt:  [11.579 Melem/s 11.590 Melem/s 11.601 Melem/s]
                 change:
                        time:   [−9.1121% −8.2558% −7.5367%] (p = 0.00 < 0.05)
                        thrpt:  [+8.1510% +8.9987% +10.026%]
                        Performance has improved.

truncate

vrl_stdlib/functions/truncate/ellipsis
                        time:   [51.877 ns 51.959 ns 52.046 ns]
                        thrpt:  [19.214 Melem/s 19.246 Melem/s 19.277 Melem/s]
                 change:
                        time:   [−9.7180% −9.4334% −9.1434%] (p = 0.00 < 0.05)
                        thrpt:  [+10.064% +10.416% +10.764%]
                        Performance has improved.
vrl_stdlib/functions/truncate/no_suffix
                        time:   [49.554 ns 49.707 ns 49.859 ns]
                        thrpt:  [20.056 Melem/s 20.118 Melem/s 20.180 Melem/s]
                 change:
                        time:   [−12.231% −11.872% −11.517%] (p = 0.00 < 0.05)
                        thrpt:  [+13.016% +13.471% +13.935%]
                        Performance has improved.

chunks

vrl_stdlib/functions/chunks/literal
                        time:   [61.683 ns 62.292 ns 62.948 ns]
                        thrpt:  [15.886 Melem/s 16.054 Melem/s 16.212 Melem/s]
                 change:
                        time:   [−11.093% −9.5756% −8.1253%] (p = 0.00 < 0.05)
                        thrpt:  [+8.8439% +10.590% +12.477%]

Change Type

  • Bug fix
  • New feature
  • Non-functional (chore, refactoring, docs)
  • Performance

Is this a breaking change?

  • Yes
  • No

How did you test this PR?

cargo test
./scripts/vrl_tests.sh

Does this PR include user facing changes?

  • Yes. Please add a changelog fragment based on
    our guidelines.
  • No. A maintainer will apply the "no-changelog" label to this PR.

Checklist

@JakubOnderka JakubOnderka force-pushed the stdlib-resolve branch 7 times, most recently from 1bcee71 to 0537a0e Compare June 5, 2026 19:05
@JakubOnderka JakubOnderka changed the title chore(stdlib): Resolve bool and int constants during compilation for chore(stdlib): Resolve bool and int constants during compilation Jun 5, 2026
@JakubOnderka JakubOnderka force-pushed the stdlib-resolve branch 2 times, most recently from 6b9db1b to 032bba2 Compare June 7, 2026 07:04
@JakubOnderka JakubOnderka force-pushed the stdlib-resolve branch 2 times, most recently from fa620ed to 0219ff0 Compare June 12, 2026 07:28
@JakubOnderka JakubOnderka marked this pull request as ready for review June 12, 2026 07:31
@JakubOnderka JakubOnderka requested a review from a team as a code owner June 12, 2026 07:31

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 211f5a29c7

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/stdlib/chunks.rs
Comment thread src/stdlib/del.rs
Comment thread src/compiler/function.rs
Comment on lines +413 to +414
Ok(match expr.resolve_constant(state) {
Some(cnst) => Self::Const(cnst.try_boolean()?),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Do not freeze loop-carried closure variables

When an optimized parameter is a variable that is constant at compile time but mutated inside an iterator closure, e.g. flag = true; for_each([1, 2]) -> |_, _| { contains("A", "a", case_sensitive: flag); flag = false }, this stores true in the compiled function and every iteration keeps using it. for_each closures preserve parent-scope mutations between iterations, so the second iteration should read the updated variable value; keep variable-backed expressions dynamic in repeated closures (the i64 helper below has the same issue).

Useful? React with 👍 / 👎.

@pront pront left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @JakubOnderka, this is great idea.

Can you run the following VRL program and compare the playground result with the result on your branch?

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.

2 participants