Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: change underlying value from string to interface{} #28

Merged
merged 6 commits into from
Dec 30, 2023

Conversation

muktihari
Copy link
Owner

  • Change underlying value from string to interface value, reducing unnecessary string conversion overhead.
  • Use sync.Pool to handle Visitor creation to reduce allocs, instead of creating new Visitor each Visit, we can use existing Visitors. Look at this code example, after Visitor X finish visiting, Visitor Y might reuse Visitors created during previous Visitor X's Visit. This will happen recursively. And also, next time user trigger any expr function, chances are they can ruse existing Visitor objects from previous invocation, only if it is still being kept alive by sync.Pool.
func (v *Visitor) visitBinary(binaryExpr *ast.BinaryExpr) ast.Visitor {
    vx := visitorPool.Get().(*Visitor)
    defer visitorPool.Put(vx)
    vx.reset()
    vx.options = v.options

    ast.Walk(vx, binaryExpr.X)
    if vx.err != nil {
         v.err = vx.err
        return nil
    }

    /* Visitor Y might reuse existing Visitor objects created during Visitor X's Visit 
       (or from previous invocation) */
    vy := visitorPool.Get().(*Visitor)
    defer visitorPool.Put(vy)
    vy.reset()
    vy.options = v.options

    ast.Walk(vy, binaryExpr.Y)
    if vy.err != nil {
        v.err = vy.err
        return nil
    }
...

Here is the benchstat from old Version that use string with this new Version using interface plus sync.Pool.

goos: darwin
goarch: amd64
pkg: github.com/muktihari/expr
cpu: Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
                      old.txt                   xxx.txt                
                      sec/op         sec/op     vs base                
Any/KindBoolean-4    13.51µ ±  3%   13.28µ ± 31%        ~ (p=0.631 n=10)
Any/KindInt-4        7.053µ ± 60%   5.364µ ± 14%  -23.94% (p=0.000 n=10)
Any/KindFloat-4     11.209µ ±  4%   9.288µ ± 31%  -17.14% (p=0.029 n=10)
Any/KindImag-4       9.600µ ± 72%   5.651µ ± 17%  -41.14% (p=0.000 n=10)
geomean              10.06µ         7.819µ        -22.29%

                     old.txt                   xxx.txt                
                       B/op          B/op      vs base                
Any/KindBoolean-4   5.297Ki ± 0%   2.734Ki ± 0%  -48.38% (p=0.000 n=10)
Any/KindInt-4       2.664Ki ± 0%   1.562Ki ± 0%  -41.35% (p=0.000 n=10)
Any/KindFloat-4     3.844Ki ± 0%   2.047Ki ± 0%  -46.75% (p=0.000 n=10)
Any/KindImag-4      2.719Ki ± 0%   1.641Ki ± 0%  -39.66% (p=0.000 n=10)
geomean             3.485Ki        1.946Ki       -44.15%

                     old.txt                 xxx.txt               
                    allocs/op   allocs/op   vs base                
Any/KindBoolean-4   111.00 ± 0%   72.00 ± 0%  -35.14% (p=0.000 n=10)
Any/KindInt-4        62.00 ± 0%   44.00 ± 0%  -29.03% (p=0.000 n=10)
Any/KindFloat-4      91.00 ± 0%   61.00 ± 0%  -32.97% (p=0.000 n=10)
Any/KindImag-4       77.00 ± 0%   49.00 ± 0%  -36.36% (p=0.000 n=10)
geomean              83.33        55.47       -33.43%

@muktihari muktihari added the enhancement New feature or request label Dec 30, 2023
@muktihari muktihari self-assigned this Dec 30, 2023
@muktihari muktihari changed the title perf: change underlying value from string to interface value perf: change underlying value from string to interface Dec 30, 2023
@muktihari muktihari changed the title perf: change underlying value from string to interface perf: change underlying value from string to interface{} Dec 30, 2023
Copy link

codecov bot commented Dec 30, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (287578d) 100.00% compared to head (9e16f36) 100.00%.

Additional details and impacted files
@@            Coverage Diff            @@
##            master       #28   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           12        12           
  Lines          837       901   +64     
=========================================
+ Hits           837       901   +64     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@muktihari muktihari merged commit 340f13e into master Dec 30, 2023
3 checks passed
@muktihari muktihari deleted the try-interface-value branch December 30, 2023 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant