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

x.json2.decoder2: use json_str and json_len, intead of just json #22704

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

enghitalo
Copy link
Contributor

@enghitalo enghitalo commented Oct 30, 2024

Description

Allow to decode part of a string without need to create a substring

Benchmark

V cache folder /home/hitalo/.vmodules/.cache was wiped.
V tmp.c and tests folder folder /tmp/v_1000 was wiped.
Starting benchmark...
max_iterations: 1000000

***Structure and maps***
 SPENT   645.359 ms in decoder2.decode[Stru](json_data)!
 SPENT  1204.135 ms in old_json.decode(Stru, json_data)!

 SPENT   730.546 ms in decoder2.decode[SumTypes](json_data)!
 SPENT  1161.810 ms in old_json.decode(SumTypes, json_data)!

 SPENT   127.497 ms in decoder2.decode[StructType[time.Time]](json_data_timestamp)!
 SPENT    83.384 ms in decoder2.decode[StructType[string]](json_data1)!
 SPENT    95.434 ms in old_json.decode(StructType[string], json_data1)!

 SPENT    82.203 ms in decoder2.decode[StructTypeOption[string]](json_data1)!
 SPENT    95.225 ms in old_json.decode(StructTypeOption[string], json_data1)!

 SPENT    50.005 ms in decoder2.decode[StructType[int]](json_data2)!
 SPENT    98.830 ms in old_json.decode(StructType[int], json_data2)!

 SPENT   194.058 ms in decoder2.decode[map[string]string](json_data1)!
 SPENT   192.357 ms in old_json.decode(map[string]string, json_data1)!


***arrays***
 SPENT   297.313 ms in decoder2.decode[[]int]('[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]')!
 SPENT   533.552 ms in old_json.decode([]int, '[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]')!


***simple types***
 SPENT    25.032 ms in decoder2.decode[int]('2')!
 SPENT    15.057 ms in decoder2.decode[bool]('true')!
 SPENT    94.516 ms in decoder2.decode[time.Time]('2022-03-11T13:54:25Z')!
 SPENT    74.930 ms in decoder2.decode[string]('"abcdefghijklimnopqrstuv"')!

***alias***
 SPENT    17.453 ms in decoder2.decode[IntAlias](2)!
 SPENT    73.759 ms in decoder2.decode[StringAlias]('"abcdefghijklimnopqrstuv"')!

***Sumtypes***
 SPENT    64.705 ms in decoder2.decode[SumTypes](2)!
 SPENT   131.476 ms in decoder2.decode[SumTypes]('"abcdefghijklimnopqrstuv"')!
 SPENT   645.622 ms in raw decode from HTTP request
 SPENT   736.365 ms in decode from HTTP request

Valgrind

./../../../../../v wipe-cache && ./../../../../../v -prod bench.v -gc none -o b_out && valgrind -s ./b_out

Before

==4056878== 
==4056878== HEAP SUMMARY:
==4056878==     in use at exit: 42,134 bytes in 1,451 blocks
==4056878==   total heap usage: 2,279 allocs, 828 frees, 97,070 bytes allocated
==4056878== 
==4056878== LEAK SUMMARY:
==4056878==    definitely lost: 28,087 bytes in 798 blocks
==4056878==    indirectly lost: 14,028 bytes in 651 blocks
==4056878==      possibly lost: 0 bytes in 0 blocks
==4056878==    still reachable: 19 bytes in 2 blocks
==4056878==         suppressed: 0 bytes in 0 blocks
==4056878== Rerun with --leak-check=full to see details of leaked memory
==4056878== 
==4056878== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

After

==4056018== 
==4056018== HEAP SUMMARY:
==4056018==     in use at exit: 24,334 bytes in 691 blocks
==4056018==   total heap usage: 2,249 allocs, 1,558 frees, 96,814 bytes allocated
==4056018== 
==4056018== LEAK SUMMARY:
==4056018==    definitely lost: 23,727 bytes in 598 blocks
==4056018==    indirectly lost: 588 bytes in 91 blocks
==4056018==      possibly lost: 0 bytes in 0 blocks
==4056018==    still reachable: 19 bytes in 2 blocks
==4056018==         suppressed: 0 bytes in 0 blocks
==4056018== Rerun with --leak-check=full to see details of leaked memory
==4056018== 
==4056018== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

vlib/time/parse.c.v Outdated Show resolved Hide resolved
@enghitalo
Copy link
Contributor Author

@spytheman I'm in an appointment. I think I'll be able to make the changes tomorrow.

@enghitalo enghitalo changed the title perf: use json_str and json_len, intead of just json x.json2.decoder2: use json_str and json_len, intead of just json Nov 1, 2024
vlib/time/parse.c.v Outdated Show resolved Hide resolved
…ite and is a bit less expressive (checking the `len` is not exclusive to strings; while it is fairly clear in this context, in others it can create more mental overhead to grasp what is happening when reading the code) using `== ` has become something that v vet recommends to check for an empty string.
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.

3 participants