Skip to content

[Experiment feature] [engine] [compile] use bytecode in execution #11

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

onheap
Copy link
Owner

@onheap onheap commented Jul 23, 2022

Summary

This PR updates the structure of the Expr, Adding a new field bytecode which is a slice of int16.

The idea is to flat the fields of node into four int16s, and store its in the int16 slice, please refer the code snippet below for details.
https://github.com/larry618/eval/blob/2d5ee1141e64bf9378c6730bf1eae99811ba1fa6/compile.go#L453-L472

Benchmark testing

Ideally, this change might improve the performance of the expression evaluation. However, based on the Benchmark testing, this change does not improve the performance of the evaluation, but introduces the additional complexity. Therefore, this PR should not be merged.

❯ go test -bench=BenchmarkEval -run=none -benchtime=3s -benchmem
goos: darwin
goarch: amd64
pkg: github.com/larry618/eval_bench
cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
BenchmarkEvalLocal-16     	42439639	        77.42 ns/op	      32 B/op	       1 allocs/op
BenchmarkEvalMain-16      	45236054	        78.23 ns/op	      32 B/op	       1 allocs/op
BenchmarkEvalLocal1-16    	44234589	        77.78 ns/op	      32 B/op	       1 allocs/op
BenchmarkEvalMain1-16     	44113219	        78.34 ns/op	      32 B/op	       1 allocs/op
PASS
ok  	github.com/larry618/eval_bench	14.583s

Test Plan

  • Unit test passed
    it's still working in progress, the debug node has not been supported yet.
❯ go test
--- FAIL:
exit status 2
FAIL	github.com/larry618/eval	0.421s
  • No degradation in performance
❯ go test -bench=BenchmarkEval -run=none -benchtime=3s -benchmem
goos: darwin
goarch: amd64
pkg: github.com/larry618/eval_bench
cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
BenchmarkEvalLocal-16     	41244609	        77.11 ns/op	      32 B/op	       1 allocs/op
BenchmarkEvalMain-16      	45792898	        77.74 ns/op	      32 B/op	       1 allocs/op
BenchmarkEvalLocal1-16    	45724270	        77.53 ns/op	      32 B/op	       1 allocs/op
BenchmarkEvalMain1-16     	45304260	        77.71 ns/op	      32 B/op	       1 allocs/op
PASS
ok  	github.com/larry618/eval_bench	14.626s
  • Tested on over a million random expressions
❯ go test -run='TestRandomExpressions'
❯ go test -run='TestRandomExpressions'
| gen progs|exec progs| gen count|exec count|  gen chan| exec chan|      time|
|----------|----------|----------|----------|----------|----------|----------|
|       1 %|       1 %|    110277|    100000|      7489|       388|    1.792s|
|       2 %|       2 %|    222400|    200000|     10000|     10000|    2.215s|
|       3 %|       3 %|    309449|    300000|      6728|       321|    1.764s|
|       4 %|       4 %|    411369|    400000|      8605|       364|    2.308s|
|       5 %|       5 %|    511607|    500000|      9189|        18|    2.532s|
|       6 %|       6 %|    603887|    600000|      1300|       187|    2.324s|
|       7 %|       7 %|    707500|    700000|      5098|         2|    2.279s|
|       8 %|       8 %|    812933|    800000|      7921|      2612|    2.329s|
|       9 %|       9 %|    906383|    900000|      3666|       317|    2.344s|
|      10 %|      10 %|   1009211|   1000000|      6753|        58|    2.393s|
|      11 %|      11 %|   1111942|   1100000|      9367|       175|    2.191s|
|      12 %|      12 %|   1212320|   1200000|      8637|      1283|    2.467s|
|      13 %|      13 %|   1311230|   1300000|      8504|       326|     2.33s|
|      14 %|      14 %|   1410075|   1400000|      6984|       691|    2.434s|
|      15 %|      15 %|   1510107|   1500000|      7654|        56|    2.666s|
|      16 %|      16 %|   1613503|   1600000|      9828|      1276|    2.455s|
|      17 %|      17 %|   1712323|   1700000|      9389|       534|    2.293s|
|      18 %|      18 %|   1804158|   1800000|      1118|       640|    2.517s|
|      19 %|      19 %|   1912668|   1900000|      9579|       689|    2.642s|
|      20 %|      20 %|   2011252|   2000000|      8616|       236|    2.492s|
|      21 %|      21 %|   2108483|   2100000|      5996|        87|    2.492s|
|      22 %|      22 %|   2211745|   2200000|      9322|        23|    2.585s|
|      23 %|      23 %|   2308236|   2300000|      5280|       556|    2.513s|
|      24 %|      24 %|   2405612|   2400000|      3029|       185|    2.685s|
|      25 %|      25 %|   2511001|   2500000|      8584|        17|    2.403s|
|      26 %|      26 %|   2612730|   2600000|      9747|       583|    2.494s|
|      27 %|      27 %|   2711629|   2700000|      7317|      1912|    2.403s|
|      28 %|      28 %|   2808242|   2800000|      5740|       102|    2.434s|
|      29 %|      29 %|   2906882|   2900000|      4422|        60|    2.567s|
|      30 %|      30 %|   3009431|   3000000|      4883|      2148|    2.627s|
|      31 %|      31 %|   3106366|   3100000|      3182|       784|    2.328s|
|      32 %|      32 %|   3206442|   3200000|      2950|      1092|    2.329s|
|      33 %|      33 %|   3302950|   3300000|       412|       138|    2.385s|
|      34 %|      34 %|   3408363|   3400000|      5362|       602|    2.469s|
|      35 %|      35 %|   3505567|   3500000|      2956|       211|    2.517s|
|      36 %|      36 %|   3606940|   3600000|      4528|        12|    2.473s|
|      37 %|      37 %|   3708077|   3700000|      5663|        14|    2.544s|
|      38 %|      38 %|   3803596|   3800000|       402|       794|    2.427s|
|      39 %|      39 %|   3911215|   3900000|      8811|         4|    2.655s|
|      40 %|      40 %|   4003888|   4000000|       525|       964|    2.417s|
|      41 %|      41 %|   4116895|   4100000|      9899|      4596|    2.412s|
|      42 %|      42 %|   4211883|   4200000|      9479|         4|    2.197s|
|      43 %|      43 %|   4306862|   4300000|      4395|        67|    2.289s|
|      44 %|      44 %|   4411983|   4400000|      9340|       243|    2.331s|
|      45 %|      45 %|   4503711|   4500000|      1091|       220|    2.365s|
|      46 %|      46 %|   4622400|   4600000|     10000|     10000|    2.836s|
|      47 %|      47 %|   4710679|   4700000|      8110|       169|    2.133s|
|      48 %|      48 %|   4809282|   4800000|      6877|         5|    2.195s|
|      49 %|      49 %|   4904257|   4900000|      1786|        71|    2.382s|
|      50 %|      50 %|   5003874|   5000000|       836|       638|     2.42s|
|      51 %|      51 %|   5110220|   5100000|      7378|       442|    2.718s|
|      52 %|      52 %|   5205790|   5200000|      3030|       360|    2.523s|
|      53 %|      53 %|   5310353|   5300000|      7068|       885|    2.614s|
|      54 %|      54 %|   5407307|   5400000|      4478|       429|     2.69s|
|      55 %|      55 %|   5513038|   5500000|      9680|       958|     2.67s|
|      56 %|      56 %|   5604377|   5600000|       778|      1199|    2.575s|
|      57 %|      57 %|   5705064|   5700000|      2504|       160|    2.825s|
|      58 %|      58 %|   5811646|   5800000|      8889|       357|    2.633s|
|      59 %|      59 %|   5912275|   5900000|      9691|       184|    2.703s|
|      60 %|      60 %|   6009871|   6000000|      7330|       141|    2.607s|
|      61 %|      61 %|   6110627|   6100000|      8221|         6|    2.873s|
|      62 %|      62 %|   6211151|   6200000|      8683|        68|    2.698s|
|      63 %|      63 %|   6312444|   6300000|      9416|       628|    2.687s|
|      64 %|      64 %|   6411668|   6400000|      8186|      1082|    2.666s|
|      65 %|      65 %|   6522400|   6500000|     10000|     10000|    2.897s|
|      66 %|      66 %|   6612398|   6600000|     10000|         3|    2.312s|
|      67 %|      67 %|   6711082|   6700000|      8634|        48|     2.51s|
|      68 %|      68 %|   6806523|   6800000|      3835|       288|    2.496s|
|      69 %|      69 %|   6906419|   6900000|      3892|       127|     2.53s|
|      70 %|      70 %|   7008570|   7000000|      5662|       508|    2.606s|
|      71 %|      71 %|   7110340|   7100000|      7820|       120|     2.55s|
|      72 %|      72 %|   7205394|   7200000|      2938|        57|    2.688s|
|      73 %|      73 %|   7304317|   7300000|       897|      1020|    2.572s|
|      74 %|      74 %|   7407732|   7400000|      4527|       805|    2.439s|
|      75 %|      75 %|   7510617|   7500000|      7240|       977|    2.571s|
|      76 %|      76 %|   7609934|   7600000|      7380|       154|    2.484s|
|      77 %|      77 %|   7710652|   7700000|      7381|       871|    2.582s|
|      78 %|      78 %|   7806363|   7800000|      3416|       547|    2.575s|
|      79 %|      79 %|   7914072|   7900000|      9929|      1743|      2.6s|
|      80 %|      80 %|   8009360|   8000000|      6577|       383|    2.608s|
|      81 %|      81 %|   8109338|   8100000|      6793|       145|    2.667s|
|      82 %|      82 %|   8212283|   8200000|      9788|        95|    2.599s|
|      83 %|      83 %|   8311108|   8300000|      8596|       112|    2.464s|
|      84 %|      84 %|   8403230|   8400000|         4|       826|    2.579s|
|      85 %|      85 %|   8512070|   8500000|      9667|         3|    2.617s|
|      86 %|      86 %|   8604469|   8600000|      1668|       402|    2.607s|
|      87 %|      87 %|   8711523|   8700000|      9065|        58|    2.607s|
|      88 %|      88 %|   8811562|   8800000|      9101|        61|    2.542s|
|      89 %|      89 %|   8912236|   8900000|      9743|        93|    2.431s|
|      90 %|      90 %|   9012544|   9000000|      9913|       231|    2.804s|
|      91 %|      91 %|   9109547|   9100000|      6148|       999|    2.473s|
|      92 %|      92 %|   9210632|   9200000|      8151|        81|    2.701s|
|      93 %|      93 %|   9312120|   9300000|      8950|       772|    2.688s|
|      94 %|      94 %|   9412286|   9400000|      9018|       868|     2.77s|
|      95 %|      95 %|   9509283|   9500000|      6352|       531|    2.887s|
|      96 %|      96 %|   9612171|   9600000|      9417|       354|    2.666s|
|      97 %|      97 %|   9710587|   9700000|      8140|        47|    2.625s|
|      98 %|      98 %|   9813889|   9800000|      8958|      2531|    2.773s|
|      99 %|      99 %|   9913549|   9900000|      8742|      2407|    2.799s|
|     100 %|     100 %|  10000000|  10000000|         0|         0|     2.72s|
PASS
ok  	github.com/larry618/eval	252.496s

Reviewers

@larry618

@onheap onheap changed the title [compile] [engine] use bytecode in execution [Experiment feature] [engine] [compile] use bytecode in execution Jul 23, 2022
size = 3000000
level = 53
size = 10000000
level = 17
Copy link
Owner Author

Choose a reason for hiding this comment

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

Because we use int16 as the type of the slice index, and the length of the bytecode is 8 times the length of the nodes. so the length (or the complexity) of the expression is reduced.

https://github.com/larry618/eval/blob/2d5ee1141e64bf9378c6730bf1eae99811ba1fa6/compile.go#L144-L153

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.

1 participant