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

string-matcher: reduce per-matcher memory #37782

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

Conversation

adisuissa
Copy link
Contributor

@adisuissa adisuissa commented Dec 20, 2024

Commit Message: string-matcher: reduce per-matcher memory
Additional Description:
This is similar to #37324 but doesn't use virtual functions (uses std::variant instead).
These are the goals for this new StringMatcherImpl class:

  1. improve encapsulation and use polymorphism that will make the code a bit more readable.
  2. reduce the amount of memory used by the StringMatcher (e.g., an object of type ExactStringMatcher doesn't need to also hold a pointer to a regex object).
  3. reduce the amount of memory that is essentially repeated (removing the kept protobuf matcher object that holds redundant information).
  4. replace the uses of the matcher() that are needed to find the contents of the string-matcher by introducing the stringRepresentation() method.

Compared to #37324, this version enjoys the inlining compiler optimizations that can be made when virtual tables aren't used. The downside is the use of std::variant, which may require a bit more memory, and results in a more coupled code for the string matchers, but IMHO it improves upon the current implementation.

Risk Level: low - not being used at the moment (but future PRs will).
Testing: duplicated the tests needed
Docs Changes: N/A
Release Notes: N/A
Platform Specific Features: N/A

Copy link

As a reminder, PRs marked as draft will not be automatically assigned reviewers,
or be handled by maintainer-oncall triage.

Please mark your PR as ready when you want it to be reviewed!

🐱

Caused by: #37782 was opened by adisuissa.

see: more, trace.

Signed-off-by: Adi Suissa-Peleg <[email protected]>
Signed-off-by: Adi Suissa-Peleg <[email protected]>
Signed-off-by: Adi Suissa-Peleg <[email protected]>
Signed-off-by: Adi Suissa-Peleg <[email protected]>
@adisuissa
Copy link
Contributor Author

I've executed the router_config_impl_speed_test, using the following:
bazel build -c opt //test/common/router:config_impl_speed_test --config=libc++
and then executing:
taskset 0x1000 bazel-bin/test/common/router/config_impl_speed_test --benchmark_repetitions=10 --benchmark_out=output.csv --benchmark_out_format=csv

and got the following results - showing only the mean of each 10 runs, and the second column is "diff%" (negative number implies that the new version is better - less time, positive number implies that the original version is better):

name diff% ( (NEW-ORIGINAL)/ORIGINAL)*100 ) Orig Real time Orig CPU time New Real time New CPU time
bmRouteTableSizeWithPathPrefixMatch/1_mean -0.665188 903 902 897 896
bmRouteTableSizeWithPathPrefixMatch/2_mean -0.306748 979 978 976 975
bmRouteTableSizeWithPathPrefixMatch/4_mean -1.40845 1137 1136 1121 1120
bmRouteTableSizeWithPathPrefixMatch/8_mean -2.43031 1400 1399 1366 1365
bmRouteTableSizeWithPathPrefixMatch/16_mean -3.62615 1959 1958 1889 1887
bmRouteTableSizeWithPathPrefixMatch/32_mean -5.44506 3070 3067 2902 2900
bmRouteTableSizeWithPathPrefixMatch/64_mean -7.57405 5339 5334 4935 4930
bmRouteTableSizeWithPathPrefixMatch/128_mean -8.4785 9940 9931 9098 9089
bmRouteTableSizeWithPathPrefixMatch/256_mean -9.862 19296 19276 17391 17375
bmRouteTableSizeWithPathPrefixMatch/512_mean -10.0741 37534 37492 33745 33715
bmRouteTableSizeWithPathPrefixMatch/1024_mean -13.3069 82592 82506 71590 71527
bmRouteTableSizeWithPathPrefixMatch/2048_mean -10.5528 174332 174086 155859 155715
bmRouteTableSizeWithPathPrefixMatch/4096_mean -6.591 333067 332787 311163 310853
bmRouteTableSizeWithPathPrefixMatch/8192_mean -5.59905 635485 634911 599967 599362
bmRouteTableSizeWithPathPrefixMatch/16384_mean -8.94865 1301248 1299905 1184862 1183581
bmRouteTableSizeWithExactPathMatch/1_mean -0.775194 904 903 897 896
bmRouteTableSizeWithExactPathMatch/2_mean -0.817996 979 978 971 970
bmRouteTableSizeWithExactPathMatch/4_mean -1.67401 1136 1135 1117 1116
bmRouteTableSizeWithExactPathMatch/8_mean -2.64097 1403 1401 1366 1364
bmRouteTableSizeWithExactPathMatch/16_mean -3.71501 1968 1965 1893 1892
bmRouteTableSizeWithExactPathMatch/32_mean -5.85192 3096 3093 2914 2912
bmRouteTableSizeWithExactPathMatch/64_mean -8.63469 5425 5420 4957 4952
bmRouteTableSizeWithExactPathMatch/128_mean -9.28869 10076 10066 9140 9131
bmRouteTableSizeWithExactPathMatch/256_mean -9.07519 19105 19085 17368 17353
bmRouteTableSizeWithExactPathMatch/512_mean -7.7453 36615 36577 33777 33744
bmRouteTableSizeWithExactPathMatch/1024_mean -9.8651 79410 79320 71564 71495
bmRouteTableSizeWithExactPathMatch/2048_mean -7.65469 175864 175644 162343 162199
bmRouteTableSizeWithExactPathMatch/4096_mean -3.29229 322320 321964 311665 311364
bmRouteTableSizeWithExactPathMatch/8192_mean -4.08471 628595 627609 602605 601973
bmRouteTableSizeWithExactPathMatch/16384_mean -6.73461 1270351 1268997 1184974 1183535
bmRouteTableSizeWithRegexMatch/1_mean 0.0958773 1044 1043 1045 1044
bmRouteTableSizeWithRegexMatch/2_mean 0.475059 1264 1263 1270 1269
bmRouteTableSizeWithRegexMatch/4_mean 1.04615 1626 1625 1644 1642
bmRouteTableSizeWithRegexMatch/8_mean 0.121902 2463 2461 2466 2464
bmRouteTableSizeWithRegexMatch/16_mean -0.735622 4491 4486 4460 4453
bmRouteTableSizeWithRegexMatch/32_mean -0.597356 7876 7868 7829 7821
bmRouteTableSizeWithRegexMatch/64_mean -0.135291 14806 14783 14777 14763
bmRouteTableSizeWithRegexMatch/128_mean -0.0775874 29672 29644 29652 29621
bmRouteTableSizeWithRegexMatch/256_mean 2.34694 58304 58246 59673 59613
bmRouteTableSizeWithRegexMatch/512_mean -2.26124 182966 182776 178829 178643
bmRouteTableSizeWithRegexMatch/1024_mean 2.46927 391451 391006 401057 400661
bmRouteTableSizeWithRegexMatch/2048_mean -1.02613 565276 564648 559373 558854
bmRouteTableSizeWithRegexMatch/4096_mean -0.588469 1104409 1103201 1098546 1096709
bmRouteTableSizeWithRegexMatch/8192_mean -6.25331 2668977 2666013 2501743 2499299
bmRouteTableSizeWithRegexMatch/16384_mean -1.14446 8314663 8306593 8220362 8211527
bmRouteTableSizeWithExactMatcherTree/1_mean 1.95412 1178 1177 1201 1200
bmRouteTableSizeWithExactMatcherTree/2_mean 1.95412 1178 1177 1201 1200
bmRouteTableSizeWithExactMatcherTree/4_mean 2.04255 1176 1175 1200 1199
bmRouteTableSizeWithExactMatcherTree/8_mean 2.04255 1176 1175 1200 1199
bmRouteTableSizeWithExactMatcherTree/16_mean 1.34794 1188 1187 1205 1203
bmRouteTableSizeWithExactMatcherTree/32_mean 1.51643 1188 1187 1206 1205
bmRouteTableSizeWithExactMatcherTree/64_mean 1.34794 1188 1187 1204 1203
bmRouteTableSizeWithExactMatcherTree/128_mean 1.43218 1189 1187 1205 1204
bmRouteTableSizeWithExactMatcherTree/256_mean 1.17845 1189 1188 1203 1202
bmRouteTableSizeWithExactMatcherTree/512_mean 1.26263 1189 1188 1204 1203
bmRouteTableSizeWithExactMatcherTree/1024_mean 1.34907 1187 1186 1204 1202
bmRouteTableSizeWithExactMatcherTree/2048_mean 2.02703 1186 1184 1209 1208
bmRouteTableSizeWithExactMatcherTree/4096_mean 1.17944 1188 1187 1202 1201
bmRouteTableSizeWithExactMatcherTree/8192_mean 1.34907 1187 1186 1203 1202
bmRouteTableSizeWithExactMatcherTree/16384_mean 0.674536 1187 1186 1195 1194
bmRouteTableSizeWithPrefixMatcherTree/1_mean 1.26796 1184 1183 1199 1198
bmRouteTableSizeWithPrefixMatcherTree/2_mean 1.35135 1185 1184 1202 1200
bmRouteTableSizeWithPrefixMatcherTree/4_mean 1.86757 1179 1178 1201 1200
bmRouteTableSizeWithPrefixMatcherTree/8_mean 2.03735 1180 1178 1203 1202
bmRouteTableSizeWithPrefixMatcherTree/16_mean 1.86916 1178 1177 1200 1199
bmRouteTableSizeWithPrefixMatcherTree/32_mean 2.03562 1181 1179 1205 1203
bmRouteTableSizeWithPrefixMatcherTree/64_mean 1.7842 1178 1177 1200 1198
bmRouteTableSizeWithPrefixMatcherTree/128_mean 1.01095 1189 1187 1200 1199
bmRouteTableSizeWithPrefixMatcherTree/256_mean 0.670578 1194 1193 1203 1201
bmRouteTableSizeWithPrefixMatcherTree/512_mean 0.671141 1194 1192 1201 1200
bmRouteTableSizeWithPrefixMatcherTree/1024_mean 0.671704 1192 1191 1201 1199
bmRouteTableSizeWithPrefixMatcherTree/2048_mean 0.672269 1191 1190 1199 1198
bmRouteTableSizeWithPrefixMatcherTree/4096_mean 0.925926 1189 1188 1200 1199
bmRouteTableSizeWithPrefixMatcherTree/8192_mean 0.92437 1191 1190 1203 1201
bmRouteTableSizeWithPrefixMatcherTree/16384_mean 0.756303 1191 1190 1200 1199

combined_route_config_impl_speed_test_10_mean.csv

IMHO, and from running a few more tests, I've observed that these tests are somewhat noisy, and there's no clear winner.
All results seems to be within the margin of error, and not much execution time difference is observed.

Signed-off-by: Adi Suissa-Peleg <[email protected]>
Signed-off-by: Adi Suissa-Peleg <[email protected]>
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