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: full support to struct attributes #22741

Merged
merged 27 commits into from
Dec 6, 2024

Conversation

enghitalo
Copy link
Contributor

@enghitalo enghitalo commented Nov 2, 2024

Look at this simple solution for dealing with attributes and not having to worry about new attributes that are in fashion.

import x.json2.decoder2 as json

struct Stru {
	a string @[json: '-']
	b string @[json: 'name']
	c string @[required]
	d string @[raw]
	e string @[omitempty]
}

Huly®: V_0.6-21188

@enghitalo enghitalo changed the title x.json2.decoder2: attributes x.json2.decoder2: $struct fields attributes Nov 2, 2024
@spytheman
Copy link
Member

Look at this simple solution for dealing with attributes and not having to worry about new attributes that are in fashion.

What is simple about it?
Can you expand a bit on that?

@enghitalo
Copy link
Contributor Author

Look at this simple solution for dealing with attributes and not having to worry about new attributes that are in fashion.

What is simple about it?
Can you expand a bit on that?

Previous solutions had rules, related to attributes, scattered throughout encode_value/decode_value.Now, the rules are distributed across other functions, each with its own responsibility. Which reduces the maintenance "cost". And it leaves more freedom for the user, in case they want to implement a new feature that they think is cool and that has not been developed by the json library. It also allows other libraries to create support for other attributes, such as email and phone numbers validators... ,without much effort and without affecting performance too much.

@enghitalo enghitalo marked this pull request as draft November 3, 2024 10:56
@spytheman
Copy link
Member

spytheman commented Nov 3, 2024

The maintenance cost, of code that uses indirection and reflection is higher (when it breaks). That can be offset by simpler usages. I do not see good ones in this PR yet.

Please provide concrete examples, of code that will be simplified.

@enghitalo enghitalo marked this pull request as ready for review December 3, 2024 15:42
@enghitalo
Copy link
Contributor Author

enghitalo commented Dec 3, 2024

Note: struct is 2x slower to decode when using @[skip]
Now / Before
image

@enghitalo
Copy link
Contributor Author

enghitalo commented Dec 3, 2024

TCC

Now / Before

image

@enghitalo enghitalo marked this pull request as draft December 3, 2024 16:55
@enghitalo
Copy link
Contributor Author

enghitalo commented Dec 5, 2024

Before [latest V] (faster)

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   340.470 ms in decoder2.decode[Stru](json_data)!
 SPENT   482.235 ms in old_json.decode(Stru, json_data)!

 SPENT   387.807 ms in decoder2.decode[SumTypes](json_data)!
 SPENT   509.426 ms in old_json.decode(SumTypes, json_data)!

 SPENT    90.053 ms in decoder2.decode[StructType[string]](json_data1)!
 SPENT    94.422 ms in old_json.decode(StructType[string], json_data1)!

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

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

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


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


***simple types***
 SPENT    20.355 ms in decoder2.decode[int]('2')!
 SPENT    17.608 ms in decoder2.decode[bool]('true')!
 SPENT    53.068 ms in decoder2.decode[time.Time]('2022-03-11T13:54:25')!
 SPENT    87.921 ms in decoder2.decode[string]('"abcdefghijklimnopqrstuv"')!

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

***Sumtypes***
 SPENT    73.181 ms in decoder2.decode[SumTypes](2)!
 SPENT   134.789 ms in decoder2.decode[SumTypes]('"abcdefghijklimnopqrstuv"')!

Now [This PR] Slower

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   435.348 ms in decoder2.decode[Stru](json_data)!
 SPENT   481.752 ms in old_json.decode(Stru, json_data)!

 SPENT   502.539 ms in decoder2.decode[SumTypes](json_data)!
 SPENT   502.240 ms in old_json.decode(SumTypes, json_data)!

 SPENT   116.154 ms in decoder2.decode[StructType[string]](json_data1)!
 SPENT    91.710 ms in old_json.decode(StructType[string], json_data1)!

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

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

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


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


***simple types***
 SPENT    19.737 ms in decoder2.decode[int]('2')!
 SPENT    17.488 ms in decoder2.decode[bool]('true')!
 SPENT    53.500 ms in decoder2.decode[time.Time]('2022-03-11T13:54:25')!
 SPENT    82.150 ms in decoder2.decode[string]('"abcdefghijklimnopqrstuv"')!

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

***Sumtypes***
 SPENT    63.274 ms in decoder2.decode[SumTypes](2)!
 SPENT   130.163 ms in decoder2.decode[SumTypes]('"abcdefghijklimnopqrstuv"')!

@enghitalo enghitalo marked this pull request as ready for review December 5, 2024 14:20
@enghitalo enghitalo changed the title x.json2.decoder2: $struct fields attributes x.json2.decoder2: full support to struct attributes Dec 5, 2024
@enghitalo
Copy link
Contributor Author

After merge on latest master

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   404.889 ms in decoder2.decode[Stru](json_data)!
 SPENT   460.689 ms in old_json.decode(Stru, json_data)!

 SPENT   479.434 ms in decoder2.decode[SumTypes](json_data)!
 SPENT   494.619 ms in old_json.decode(SumTypes, json_data)!

 SPENT   110.415 ms in decoder2.decode[StructType[string]](json_data1)!
 SPENT    89.593 ms in old_json.decode(StructType[string], json_data1)!

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

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

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


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


***simple types***
 SPENT    19.128 ms in decoder2.decode[int]('2')!
 SPENT    17.133 ms in decoder2.decode[bool]('true')!
 SPENT    52.787 ms in decoder2.decode[time.Time]('2022-03-11T13:54:25')!
 SPENT    84.898 ms in decoder2.decode[string]('"abcdefghijklimnopqrstuv"')!

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

***Sumtypes***
 SPENT    59.625 ms in decoder2.decode[SumTypes](2)!
 SPENT   125.958 ms in decoder2.decode[SumTypes]('"abcdefghijklimnopqrstuv"')!

@spytheman spytheman merged commit 210239f into vlang:master Dec 6, 2024
61 of 62 checks passed
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