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

make zero-sized value counting more fine grained #516

Closed
wants to merge 5 commits into from

Conversation

chenyan-dfinity
Copy link
Contributor

@chenyan-dfinity chenyan-dfinity commented Jan 26, 2024

Update spec to limit the number of zero-sized values in a message, instead of the sum length of vec {zero-sized value}.

Copy link

Benchmark for 92f94e6

Click to view benchmark
Test Base PR %
Blob/&str 610.0±91.17µs 615.4±97.25µs +0.89%
Blob/ByteBuf 345.4±89.29µs 347.4±87.07µs +0.58%
Blob/Bytes 392.0±75.69µs 402.6±78.91µs +2.70%
Blob/String 626.1±243.34µs 639.5±255.18µs +2.14%
Collections/vec (text, nat) 47.6±0.53ms 49.4±1.20ms +3.78%
Collections/vec int 27.9±1.81ms 23.7±0.13ms -15.05%
Collections/vec int64 15.3±0.04ms 15.2±0.49ms -0.65%
Collections/vec nat8 12.0±0.06ms 11.6±0.05ms -3.33%
option list/1024 965.6±6.56µs 960.4±4.69µs -0.54%
profiles/1024 1774.7±33.95µs 1831.4±14.36µs +3.19%
variant list/1024 796.2±60.08µs 798.4±6.50µs +0.28%

fn dec_zero_sized_value(&mut self) -> Result<()> {
//eprintln!("{}", self.zero_sized_values);
if self.zero_sized_values == 0 {
return Err(Error::msg("Too many zero sized values"));
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return Err(Error::msg("Too many zero sized values"));
return Err(Error::msg("Too many zero sized values"));
Suggested change
return Err(Error::msg("Too many zero sized values"));
return Err(Error::msg("Too many zero-sized values"));

Copy link
Contributor

@crusso crusso left a comment

Choose a reason for hiding this comment

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

Looks reasonable to me. Nice to have some examples too.

}),
_ => false,
fn dec_zero_sized_value(&mut self) -> Result<()> {
//eprintln!("{}", self.zero_sized_values);
Copy link
Contributor

Choose a reason for hiding this comment

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

should this comment be dropped?

spec/Candid.md Outdated
@@ -1255,7 +1255,7 @@ M(id(v*) : principal) = i8(1) M(v* : vec nat8)

Note:

* Since `null`, `reserved`, `record {}`, and records of such values, take no space, to prevent unbounded sized message, we limit the total vector length of such zero-sized values in a messagev (on the wire) to be 2,000,000 elements. For example, if a message contains two vectors, one at type `vec null` and one at type `vec record {}`, then the length of both vectors combined cannot exceed 2,000,000 elements.
* Since `null`, `reserved` and `record {}` take no space, to prevent unbounded sized message, we limit the total number of such zero-sized values in a message (on the wire) to be 2,000,000 elements. For example, a value of type `record { null; reserved; record { record {} } }` contains 3 zero-sized values; a value of type `vec opt record { null; null }` with length 100 contains at most 200 zero-sized values (it is 200 when the vector contains no `null` values); if a message contains two vectors, one at type `vec null` and one at type `vec record {}`, then the length of both vectors combined cannot exceed 2,000,000 elements.
Copy link
Contributor

Choose a reason for hiding this comment

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

a value of type record { null; reserved; record { record {} } } contains 3 zero-sized values
Could you please list the zero-sized values explicitly? I believe to have counter 4:

  • null
  • reserved
  • record {}
  • record { record {} }

Copy link
Contributor

Choose a reason for hiding this comment

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

And in fact also the entire record { null; reserved; record { record {} } } is a 5th zero-sized value there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will try to rephrase the spec. We only count the occurrences of null, reserved and record {} to simulate the effect that if these values were not zero sized. record { record {} } would not be zero sized, if record {} weren't zero sized.

@@ -337,16 +334,13 @@ impl<'de> Deserializer<'de> {
}
Ok(())
}
fn is_zero_sized_type(&self, t: &Type) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

Conceptually, we should check this condition for every (nested) value and limit all such (nested) zero-sized values at 2M in total.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See above. That would be overcounting, e.g., record { null; null } would be counted as 3 instead of 2.

Copy link

Benchmark for 029bbc4

Click to view benchmark
Test Base PR %
Blob/&str 627.8±97.62µs 595.0±87.47µs -5.22%
Blob/ByteBuf 373.3±93.92µs 361.9±89.19µs -3.05%
Blob/Bytes 392.0±65.27µs 357.4±60.08µs -8.83%
Blob/String 353.2±134.79µs 385.0±127.24µs +9.00%
Collections/vec (text, nat) 50.7±1.75ms 52.2±2.11ms +2.96%
Collections/vec int 23.4±0.24ms 23.2±0.16ms -0.85%
Collections/vec int64 15.5±0.12ms 14.9±0.08ms -3.87%
Collections/vec nat8 11.1±0.04ms 11.0±0.02ms -0.90%
option list/1024 969.4±10.60µs 962.5±9.95µs -0.71%
profiles/1024 1759.3±16.86µs 1853.3±39.18µs +5.34%
variant list/1024 810.6±10.25µs 802.7±12.38µs -0.97%

@chenyan-dfinity chenyan-dfinity deleted the fix-zsv branch February 4, 2024 02:56
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