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

Remove improperly supported "unbounded" size #125

Merged

Conversation

greymistcube
Copy link
Contributor

@greymistcube greymistcube commented Dec 1, 2024

This should follow #124.

Squeezes out another 5% or so from the result of #124. Although technically, unbounded encoding length could be considered as part of the spec, this would be hard to do properly without taking a significant performance hit. If one really wants to support it, the entire stack must be overhauled.

There are two rationales:

  • I am skeptical of the original code that it actually supports the feature and works properly it is trying to accommodate.
    • C# itself has a hard limit on an array's length, which is int.MaxValue.
    • That is, one cannot expect byte[] to be of length greater than int.MaxValue. Especially buffer[offset] where offset is greater than int.MaxValue is likely to fail.
  • With int.MaxValue, the maximum size of byte[] comes out to be about 2GB. For all intents and purposes, this should be enough for now for encoding a single IValue.
    • Bencode, which forms the basis of the bencodex, originates from encoding torrent metadata files, not the file itself. It was never meant to be big. 🙄

Note that serialization of BigInteger, i.e. an integer without a limit (as long as it doesn't exceed int.MaxValue number of digits in length), is still supported.

Encode() without bloat

Method SetSize WordSize Mean Error StdDev Gen0 Allocated
EncodeList 8 8 3.200 ms 0.3390 ms 0.2243 ms 265.6250 1.6 MB
EncodeDict 8 8 12.088 ms 1.2449 ms 0.8234 ms 453.1250 2.73 MB
EncodeNestedList 8 8 27.358 ms 2.9628 ms 1.9597 ms 1937.5000 11.65 MB
EncodeNestedDict 8 8 121.634 ms 10.6371 ms 5.5634 ms 3500.0000 21.6 MB
EncodeList 8 16 3.773 ms 0.5371 ms 0.3553 ms 382.8125 2.29 MB
EncodeDict 8 16 13.179 ms 0.2856 ms 0.1889 ms 671.8750 4.1 MB
EncodeNestedList 8 16 30.123 ms 4.3146 ms 2.8538 ms 2812.5000 17.14 MB
EncodeNestedDict 8 16 134.295 ms 15.8120 ms 10.4586 ms 5500.0000 33.28 MB
EncodeList 16 8 6.171 ms 0.8664 ms 0.5730 ms 398.4375 2.42 MB
EncodeDict 16 8 25.133 ms 2.0928 ms 1.3842 ms 718.7500 4.33 MB
EncodeNestedList 16 8 95.434 ms 15.1526 ms 10.0225 ms 5833.3333 35.3 MB
EncodeNestedDict 16 8 416.789 ms 55.9215 ms 36.9886 ms 11000.0000 67.73 MB
EncodeList 16 16 6.843 ms 1.0291 ms 0.6807 ms 632.8125 3.8 MB
EncodeDict 16 16 27.280 ms 1.8400 ms 1.0949 ms 1156.2500 7.07 MB
EncodeNestedList 16 16 109.229 ms 17.3151 ms 11.4529 ms 9500.0000 57.27 MB
EncodeNestedDict 16 16 447.549 ms 44.9496 ms 29.7314 ms 18000.0000 113.04 MB

@greymistcube greymistcube force-pushed the refactor/remove-incomplete-feature branch from 3a62ead to 531be58 Compare December 3, 2024 06:52
@riemannulus riemannulus self-assigned this Dec 3, 2024
@riemannulus
Copy link
Member

riemannulus commented Dec 3, 2024

Are there any cases where using bencodex needs a long offset in actual data?

@greymistcube
Copy link
Contributor Author

@riemannulus

Are there any cases where you need a long offset in actual data?

Probably not. As I have mentioned, default byte[] cannot handle long as its indexer, nor can it be longer than int.MaxValue. That is, the original implementation would fail anyway. It would be possible to support long sized byte "array" data, but that would require a complete overhaul. If we decide to do so, as with any kind of support for the data structure that is not natively supported by the language, there will be a huge performance hit.

Also, neither IStore nor IStateStore in libplanet support data types requiring long indexing. So we can neither write or read anyway at this point. 🙄

@riemannulus riemannulus merged commit 884c09c into planetarium:main Dec 3, 2024
4 checks passed
@riemannulus
Copy link
Member

Thanks for your contribution!

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