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

Expanded BigInt support #2

Merged
merged 6 commits into from
Feb 23, 2024
Merged

Conversation

robdmoore
Copy link

Took msgpack#211, rebased against latest main.

BigInt support had been added (in a very basic way) in msgpack library, these changes expand the int handling to have more configurability and sophistication, while maintaining backwards compatibility with latest msgpack.

I also refactored the tests to pull in a whole heap of external dependencies that need to be here so they are more easily understood and modified (I had to modify them to handle BigInt to avoid tests breaking).

robdmoore and others added 2 commits January 27, 2024 15:00
Modified them to properly support bigint and convert them to TypeScript
Adding VS Code support for mocha
README.md Show resolved Hide resolved
README.md Show resolved Hide resolved
package.json Show resolved Hide resolved
src/Encoder.ts Outdated
@@ -200,7 +213,7 @@ export class Encoder<ContextType = undefined> {
// uint 32
this.writeU8(0xce);
this.writeU32(object);
} else if (!this.useBigInt64) {
} else if (this.useInt64) {
Copy link
Author

Choose a reason for hiding this comment

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

The useBigInt64 option was being conflated for 2 things - one was encoding bigint's as int64, the other was encoding big number's as int64 rather than float64 so I've added configuration to support both (useInt64 and forceBigIntToInt64). The default config (both false) will have the same behaviour as before.

Copy link
Author

Choose a reason for hiding this comment

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

Actually, on reflection, maybe the default behaviour here is different and I need to make useInt64 true by default?

Choose a reason for hiding this comment

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

Here's my opinion. The upstream library's decision to encode numbers above the uint32 max or below the int32 min if useBigInt64 is false is nonsensical. Javascript numbers can safely represent integers outside of this range without using bigints, and the decision to encode these as floats when using the default encoding params (thereby confusing every other non-JS msgp decoder) is mind boggling.

I'd get rid of this condition entirely.

At this point our top priority is delivering a library with enhanced features that are easy to use and make sense. We're only in this position because the upstream library has failed us. Let's focus on correcting the behavior over trying to be compatible with it.

Copy link
Author

Choose a reason for hiding this comment

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

I'm happy to drop compatibility with the core library if you are. My main concern is that it means we do have to maintain this library long term since it makes it less likely that the core library would accept our changes if they take on maintenance of that library again.

Side-note: should we raise all of these PRs to the core library as well as this one do you think or not worth it?

Copy link
Author

Choose a reason for hiding this comment

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

Also, the fact there is a forceIntegerToFloat means that it's still possible to force a BigInt to a float.

Choose a reason for hiding this comment

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

Also, the fact there is a forceIntegerToFloat means that it's still possible to force a BigInt to a float.

We should fix this inconsistency, since the docstring for forceBigIntToInt64 says forceIntegerToFloat doesn't affect bigints. I'm fine with qualifying that dosctring to say that it doesn't affect bigints if that option is enabled.

should we raise all of these PRs to the core library as well as this one do you think or not worth it?

For the most part each of these PRs already exists upstream, right? I guess the main difference is that they're not based on the most recent changes to the main branch.

My current intention is to release this fork under a different name, then post a link to it on each of the upstream PRs. Hopefully then we'll at least help others in similar situations.

Ideally yes, we could merge all of these features back upstream, but I'm not counting on that as a viable option in the short term.

@jasonpaulos
Copy link

Closing and reopening to trigger github actions

@jasonpaulos jasonpaulos closed this Feb 2, 2024
@jasonpaulos jasonpaulos reopened this Feb 2, 2024
src/Decoder.ts Show resolved Hide resolved
src/Encoder.ts Outdated Show resolved Hide resolved
src/Encoder.ts Outdated
@@ -200,7 +213,7 @@ export class Encoder<ContextType = undefined> {
// uint 32
this.writeU8(0xce);
this.writeU32(object);
} else if (!this.useBigInt64) {
} else if (this.useInt64) {

Choose a reason for hiding this comment

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

Here's my opinion. The upstream library's decision to encode numbers above the uint32 max or below the int32 min if useBigInt64 is false is nonsensical. Javascript numbers can safely represent integers outside of this range without using bigints, and the decision to encode these as floats when using the default encoding params (thereby confusing every other non-JS msgp decoder) is mind boggling.

I'd get rid of this condition entirely.

At this point our top priority is delivering a library with enhanced features that are easy to use and make sense. We're only in this position because the upstream library has failed us. Let's focus on correcting the behavior over trying to be compatible with it.

src/Encoder.ts Outdated Show resolved Hide resolved
test/bigint64.test.ts Outdated Show resolved Hide resolved
BREAKING CHANGE: `useBigInt64` deprecated from Encoder and > 32 bit ints now get encoded as a uint64 rather than float64
@robdmoore
Copy link
Author

FYI I pushed another commit with the tweaks based on feedback

Copy link

@jasonpaulos jasonpaulos left a comment

Choose a reason for hiding this comment

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

Implementation is looking good (let's just address the forceIntegerToFloat issue).

I left a few test comments as well.

src/Encoder.ts Outdated Show resolved Hide resolved
src/Encoder.ts Outdated
@@ -200,7 +213,7 @@ export class Encoder<ContextType = undefined> {
// uint 32
this.writeU8(0xce);
this.writeU32(object);
} else if (!this.useBigInt64) {
} else if (this.useInt64) {

Choose a reason for hiding this comment

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

Also, the fact there is a forceIntegerToFloat means that it's still possible to force a BigInt to a float.

We should fix this inconsistency, since the docstring for forceBigIntToInt64 says forceIntegerToFloat doesn't affect bigints. I'm fine with qualifying that dosctring to say that it doesn't affect bigints if that option is enabled.

should we raise all of these PRs to the core library as well as this one do you think or not worth it?

For the most part each of these PRs already exists upstream, right? I guess the main difference is that they're not based on the most recent changes to the main branch.

My current intention is to release this fork under a different name, then post a link to it on each of the upstream PRs. Hopefully then we'll at least help others in similar situations.

Ideally yes, we could merge all of these features back upstream, but I'm not counting on that as a viable option in the short term.

test/bigint64.test.ts Outdated Show resolved Hide resolved
test/codec-bigint.test.ts Outdated Show resolved Hide resolved
test/encode.test.ts Outdated Show resolved Hide resolved
src/Encoder.ts Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@robdmoore
Copy link
Author

@jasonpaulos Ready for final review

Copy link

@jasonpaulos jasonpaulos left a comment

Choose a reason for hiding this comment

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

Looks good!

@jasonpaulos jasonpaulos merged commit 1007830 into algorand:main Feb 23, 2024
7 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.

2 participants