Skip to content

feat: add "expr_noreflectmethod" build tag - #957

Open
vincentbernat wants to merge 1 commit into
expr-lang:masterfrom
vincentbernat:feature/dce-elimination-tag
Open

vincentbernat wants to merge 1 commit into
expr-lang:masterfrom
vincentbernat:feature/dce-elimination-tag

Conversation

@vincentbernat

Copy link
Copy Markdown
Contributor

By enabling this tag, two features are removed:

  • functions in environment
  • Eval() method

Fix #863.
Related to #956.

I still need to work on the tests to make them work with or without the tag.

@vincentbernat
vincentbernat force-pushed the feature/dce-elimination-tag branch from f0cb45a to d0afa56 Compare April 26, 2026 11:13
@vincentbernat

Copy link
Copy Markdown
Contributor Author

I have added a commit to make tests work with the tag (and updated one workflow to demonstrate that).

goos: linux
goarch: amd64
pkg: github.com/expr-lang/expr
cpu: AMD Ryzen 5 5600X 6-Core Processor
                            │ before.txt  │             after.txt              │
                            │   sec/op    │   sec/op     vs base               │
_expr-12                      85.63n ± 1%   77.28n ± 2%   -9.75% (p=0.002 n=6)
_expr_eval-12                 5.781µ ± 1%   5.677µ ± 1%   -1.80% (p=0.004 n=6)
_expr_reuseVm-12              60.66n ± 3%   53.05n ± 4%  -12.55% (p=0.002 n=6)
_len-12                       48.46n ± 4%   47.78n ± 1%   -1.39% (p=0.009 n=6)
_filter-12                    42.43µ ± 2%   43.76µ ± 3%   +3.12% (p=0.002 n=6)
_filterLen-12                 37.82µ ± 2%   38.17µ ± 2%        ~ (p=0.699 n=6)
_filterFirst-12               380.0n ± 1%   386.0n ± 1%   +1.58% (p=0.009 n=6)
_filterLast-12                716.7n ± 2%   677.8n ± 1%   -5.44% (p=0.002 n=6)
_filterMap-12                 3.861µ ± 3%   3.899µ ± 2%        ~ (p=0.558 n=6)
_arrayIndex-12                84.54n ± 2%   84.92n ± 2%        ~ (p=0.699 n=6)
_envStruct-12                 53.07n ± 1%   53.17n ± 2%        ~ (p=0.485 n=6)
_envStruct_noEnv-12           140.5n ± 2%   145.0n ± 1%   +3.28% (p=0.002 n=6)
_envMap-12                    64.05n ± 3%   61.66n ± 2%   -3.72% (p=0.009 n=6)
_callFunc-12                  348.0n ± 6%   350.7n ± 1%        ~ (p=0.394 n=6)
_callMethod-12                365.4n ± 1%   374.2n ± 1%   +2.41% (p=0.002 n=6)
_callField-12                 75.81n ± 2%   75.46n ± 1%        ~ (p=0.394 n=6)
_callFast-12                  76.38n ± 3%   76.01n ± 1%        ~ (p=0.394 n=6)
_callConstExpr-12             35.02n ± 1%   34.74n ± 1%   -0.77% (p=0.045 n=6)
_largeStructAccess-12         138.4n ± 1%   148.0n ± 1%   +6.98% (p=0.002 n=6)
_largeNestedStructAccess-12   148.0n ± 1%   157.0n ± 1%   +6.11% (p=0.002 n=6)
_largeNestedArrayAccess-12    1.073m ± 8%   1.002m ± 6%   -6.61% (p=0.026 n=6)
_sort-12                      3.948µ ± 1%   3.702µ ± 1%   -6.24% (p=0.002 n=6)
_sortBy-12                    11.33µ ± 3%   11.06µ ± 2%   -2.32% (p=0.004 n=6)
_groupBy-12                   8.259µ ± 1%   8.589µ ± 1%   +4.00% (p=0.002 n=6)
_reduce-12                    34.64n ± 1%   34.50n ± 1%        ~ (p=0.197 n=6)
_min-12                       500.6n ± 1%   537.0n ± 1%   +7.28% (p=0.002 n=6)
_max-12                       506.7n ± 1%   535.9n ± 1%   +5.76% (p=0.002 n=6)
_mean-12                      218.1n ± 2%   229.5n ± 2%   +5.20% (p=0.002 n=6)
_median-12                    1.599µ ± 1%   1.593µ ± 2%        ~ (p=0.290 n=6)
geomean                       564.7n        563.4n        -0.22%

pkg: github.com/expr-lang/expr/checker
                                            │ before.txt  │             after.txt             │
                                            │   sec/op    │   sec/op     vs base              │
Checker/name=function_calls-12                6.248µ ± 5%   5.970µ ± 2%  -4.44% (p=0.004 n=6)
Checker/name=unary_and_binary_operations-12   6.167µ ± 3%   6.163µ ± 2%       ~ (p=0.937 n=6)
Checker/name=deep_struct_access-12            16.48µ ± 2%   16.74µ ± 2%  +1.61% (p=0.009 n=6)
geomean                                       8.594µ        8.509µ       -1.00%

pkg: github.com/expr-lang/expr/internal/difflib
                   │ before.txt  │             after.txt             │
                   │   sec/op    │   sec/op     vs base              │
SplitLines100-12     905.9n ± 1%   866.7n ± 1%  -4.32% (p=0.002 n=6)
SplitLines10000-12   81.46µ ± 1%   81.59µ ± 1%       ~ (p=0.485 n=6)
geomean              8.590µ        8.409µ       -2.11%

pkg: github.com/expr-lang/expr/internal/testify/assert
              │ before.txt  │             after.txt             │
              │   sec/op    │   sec/op     vs base              │
BytesEqual-12   144.9n ± 5%   147.8n ± 2%       ~ (p=0.485 n=6)
NotNil-12       4.416n ± 1%   4.399n ± 2%       ~ (p=1.000 n=6)
geomean         25.29n        25.49n       +0.80%

pkg: github.com/expr-lang/expr/optimizer
                                 │ before.txt  │             after.txt              │
                                 │   sec/op    │   sec/op     vs base               │
CountGtZero-12                     1.438µ ± 3%   1.416µ ± 4%        ~ (p=0.260 n=6)
CountGtZeroLargeEarlyMatch-12      9.107µ ± 7%   9.841µ ± 6%   +8.06% (p=0.004 n=6)
CountGtZeroNoMatch-12              29.32µ ± 4%   30.53µ ± 3%   +4.13% (p=0.026 n=6)
CountGteOneEarlyMatch-12           1.452µ ± 2%   1.541µ ± 4%   +6.13% (p=0.002 n=6)
CountGteOneNoMatch-12              29.22µ ± 5%   29.50µ ± 2%        ~ (p=0.132 n=6)
CountThresholdEarlyMatch-12        15.00µ ± 3%   13.64µ ± 3%   -9.09% (p=0.002 n=6)
CountThresholdGteEarlyMatch-12     12.20µ ± 2%   11.59µ ± 4%   -4.99% (p=0.002 n=6)
CountThresholdNoEarlyExit-12       342.4µ ± 2%   336.8µ ± 1%   -1.64% (p=0.041 n=6)
CountThresholdLargeEarlyMatch-12   89.66µ ± 2%   85.10µ ± 1%   -5.09% (p=0.002 n=6)
CountThresholdLtEarlyExit-12       15.56µ ± 9%   14.13µ ± 3%   -9.19% (p=0.002 n=6)
CountThresholdLteEarlyExit-12      12.45µ ± 3%   11.79µ ± 7%   -5.35% (p=0.002 n=6)
CountThresholdLtNoEarlyExit-12     340.8µ ± 3%   343.2µ ± 1%        ~ (p=0.699 n=6)
CountThresholdLteNoEarlyExit-12    353.2µ ± 3%   341.4µ ± 1%   -3.33% (p=0.004 n=6)
SumArray-12                        187.1n ± 4%   148.6n ± 2%  -20.58% (p=0.002 n=6)
SumRange_Optimized-12              35.64n ± 7%   34.83n ± 2%        ~ (p=0.058 n=6)
ReduceRangeSum_Optimized-12        35.45n ± 6%   34.85n ± 1%   -1.71% (p=0.035 n=6)
SumRange_Unoptimized-12            3.267µ ± 4%   3.336µ ± 1%        ~ (p=0.093 n=6)
geomean                            7.854µ        7.640µ        -2.73%

pkg: github.com/expr-lang/expr/parser
          │  before.txt  │          after.txt           │
          │    sec/op    │   sec/op     vs base         │
Parser-12   618.2n ± 13%   605.2n ± 3%  ~ (p=0.240 n=6)

          │ before.txt │           after.txt           │
          │    B/op    │    B/op     vs base           │
Parser-12   256.0 ± 0%   256.0 ± 0%  ~ (p=1.000 n=6) ¹
¹ all samples are equal

          │ before.txt │           after.txt           │
          │ allocs/op  │ allocs/op   vs base           │
Parser-12   8.000 ± 0%   8.000 ± 0%  ~ (p=1.000 n=6) ¹
¹ all samples are equal

pkg: github.com/expr-lang/expr/patcher/value
                    │ before.txt  │             after.txt             │
                    │   sec/op    │   sec/op     vs base              │
_valueAdd-12          86.08n ± 1%   83.43n ± 4%  -3.08% (p=0.026 n=6)
_valueUntypedAdd-12   102.7n ± 2%   102.1n ± 1%       ~ (p=0.223 n=6)
_valueTypedAdd-12     84.59n ± 1%   82.58n ± 1%  -2.37% (p=0.002 n=6)
geomean               90.75n        88.95n       -1.98%

pkg: github.com/expr-lang/expr/test/bench
                  │ before.txt  │             after.txt             │
                  │   sec/op    │   sec/op     vs base              │
Call_callTyped-12   8.686µ ± 1%   8.770µ ± 1%  +0.97% (p=0.041 n=6)
Call_eval-12        1.619µ ± 1%   1.603µ ± 1%       ~ (p=0.058 n=6)
geomean             3.750µ        3.749µ       -0.03%

pkg: github.com/expr-lang/expr/vm
                          │ before.txt  │             after.txt             │
                          │   sec/op    │   sec/op     vs base              │
VM/name=function_calls-12   1.670µ ± 1%   1.617µ ± 1%  -3.17% (p=0.002 n=6)

pkg: github.com/expr-lang/expr/vm/runtime
                                          │ before.txt  │             after.txt             │
                                          │   sec/op    │   sec/op     vs base              │
Equal/int_==_int-12                         2.163n ± 1%   2.235n ± 3%  +3.28% (p=0.004 n=6)
Equal/int_!=_int-12                         2.165n ± 0%   2.193n ± 2%  +1.27% (p=0.009 n=6)
Equal/int_==_int8-12                        2.379n ± 0%   2.383n ± 0%       ~ (p=0.831 n=6)
Equal/int_==_int16-12                       2.381n ± 0%   2.430n ± 2%       ~ (p=0.061 n=6)
Equal/int_==_int32-12                       2.188n ± 2%   2.188n ± 2%       ~ (p=0.792 n=6)
Equal/int_==_int64-12                       2.399n ± 1%   2.379n ± 1%       ~ (p=0.091 n=6)
Equal/float_==_float-12                     2.384n ± 1%   2.403n ± 1%  +0.78% (p=0.041 n=6)
Equal/float_!=_float-12                     2.384n ± 0%   2.408n ± 1%  +0.96% (p=0.026 n=6)
Equal/float_==_int-12                       2.606n ± 0%   2.600n ± 1%       ~ (p=0.329 n=6)
Equal/float_!=_int-12                       2.607n ± 2%   2.597n ± 0%       ~ (p=0.190 n=6)
Equal/string_==_string-12                   3.244n ± 2%   3.242n ± 1%       ~ (p=0.677 n=6)
Equal/string_!=_string-12                   4.547n ± 1%   4.544n ± 0%       ~ (p=0.608 n=6)
Equal/bool_==_bool-12                       2.165n ± 0%   2.163n ± 1%       ~ (p=0.790 n=6)
Equal/bool_!=_bool-12                       2.167n ± 0%   2.163n ± 0%       ~ (p=0.084 n=6)
Equal/[]any_==_[]int-12                     15.18n ± 6%   14.83n ± 7%       ~ (p=0.784 n=6)
Equal/[]any_!=_[]int-12                     15.50n ± 8%   15.30n ± 7%       ~ (p=0.413 n=6)
Equal/deep_[]any_==_[]any-12                84.08n ± 1%   85.07n ± 2%  +1.18% (p=0.015 n=6)
Equal/deep_[]any_!=_[]any-12                61.08n ± 2%   61.08n ± 1%       ~ (p=0.937 n=6)
Equal/map[string]any_==_map[string]any-12   253.5n ± 1%   254.0n ± 1%       ~ (p=0.615 n=6)
Equal/map[string]any_!=_map[string]any-12   59.46n ± 1%   59.36n ± 1%       ~ (p=0.589 n=6)
geomean                                     6.188n        6.201n       +0.20%

@vincentbernat
vincentbernat marked this pull request as ready for review April 26, 2026 11:55
@vincentbernat
vincentbernat force-pushed the feature/dce-elimination-tag branch from 216d251 to ec07cc1 Compare April 26, 2026 13:37
@antonmedv

Copy link
Copy Markdown
Member

Nice. Let me review this pull request and I will merge it and release new version shortly after.

@ofek-frame

Copy link
Copy Markdown

Any news, perhaps?

@antonmedv

Copy link
Copy Markdown
Member

Currently on my vacation, please @materight take a look

@vincentbernat
vincentbernat force-pushed the feature/dce-elimination-tag branch 3 times, most recently from 73c5477 to 4125b41 Compare August 11, 2026 11:46
@vincentbernat

Copy link
Copy Markdown
Contributor Author

I have updated this pull request to "fix" tests. In the future, this may be the main hindrance with such a patch: tests using Eval() will fail and need to be manually skipped. Maybe people will more likely use Compile()+Run() instead of Eval() when writing tests. I don't think in Go, we can automatically skip tests using Eval() (like an autofixture in pytest).

@vincentbernat

Copy link
Copy Markdown
Contributor Author

I think this would be doable by modifying Error() and NoError() helpers. Instead of panicking, when running inside tests, Eval() could return a specific error that would make the test skip. It does not cover all use cases (notably the ones using env methods), but it should reduce the need to skip tests. Let me know if this is something worth to do.

@antonmedv

Copy link
Copy Markdown
Member

Thanks! With the build tag set, Eval panics. Could you fix that and add a test that runs with the tag?

When `reflect.Value.MethodByName()` or `reflect.Value.Method()` are
reachable, the Go linker keeps all exported methods of all types. With
this tag, expr does not use them and the binary can be smaller.

By enabling this tag, two features are removed:
- functions in environment
- `Eval()` method

`Eval()` will return an error if compiled with `expr_noreflectmethod`.
This error is also used to auto-skip tests

Most tests are auto-skipped if they call `Eval()` to minimize the
changes to tests by modifying the `Error()` and `NoError()` helpers.
Remaining tests can call `assert.SkipNoReflectMethod()` or set a build
tag `!expr_noreflectmethod`.

Fix expr-lang#863.
@vincentbernat
vincentbernat force-pushed the feature/dce-elimination-tag branch from 4125b41 to 8171d27 Compare November 29, 2026 07:35
@vincentbernat

Copy link
Copy Markdown
Contributor Author

I have updated this PR:

  • Eval() does not panic anymore but returns an error (and a tests has been added for that)
  • Tests using Eval() are auto-skipped by modifying Error() and NoError(), so the change is now smaller

This branch has not been deployed

No deployments
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.

Avoid reflect.MethodByName

3 participants