protobuf.js icon indicating copy to clipboard operation
protobuf.js copied to clipboard

Use BigInt over Number for int64

Open mjgp2 opened this issue 5 years ago • 24 comments

This PR removes the need for the Long package, and instead of Number uses BigInt as standard for all int64 types.

I realise that this would be a breaking change, but it seems like it would be the right design decision for the next major version of this library?

All unit tests pass, and needed to switch to terser from uglify.

Node versions before 10 would not be supported. However, Node 10 LTS is EOL on 2021-04-30.

mjgp2 avatar Mar 09 '21 13:03 mjgp2

Hi @mjgp2,

Thank you for this PR. We probably cannot include it in v7.0.0 (that is coming soon) but we'll think if we can drop long in the next semver major. At least this would be a major pain for us at Google to convert all the client libraries (which reexport types from protobuf.js), and I can only imagine how disruptive it can be for others. Also, if you see how the long support is implemented, there's a configuration option that enables or disables long. It maybe makes sense to think if we can make this less breaking by doing it in steps:

(1) start accepting BigInt where we currently accept long when serializing; (2) make it configurable to be able to choose if we dump long or BigInt.

Also, dropping support for Node.js v4-6-8 is a big decision.

Of course, Cc: @dcodeIO on this.

I promise we'll get back to reviewing this PR when we're done with releasing v7.0.0 :)

alexander-fenster avatar Mar 12 '21 23:03 alexander-fenster

Thanks for your reply.

Yes, I think that definitely you could support Number, BigInt, String and Long for int64 types. My thinking here was firstly to understand the codebase and to swap Long for BigInt being simpler than adding the extra branching logic, and then hopefully get some feedback.

I would suggest that it is considered whether support for Number for int64 should be dropped - there are silent overflows today I think within the codebase that are really data corruption.

mjgp2 avatar Mar 15 '21 10:03 mjgp2

Any progress on this PR?

ardatan avatar Apr 20 '21 08:04 ardatan

@ardatan - you can use the branch on my repo, it works, i.e. in your package.json

  "resolutions": {
    "**/protobufjs": "mjgp2/protobuf.js#bigint"
  },

I understand it's going to be targeted for version 8. I'm happy to help when the time comes - it's still my thinking that Number support for int64 should be dropped in version 8 because it will corrupt data.

mjgp2 avatar Jun 25 '21 16:06 mjgp2

Sorry for pinging a bunch of people but me and my colleagues are really looking forward to this PR being merged and published so we are looking for a status on this. Is it stuck on something other than having conflicts? Can we help out in any way?

hornta avatar Sep 17 '21 16:09 hornta

I'm happy to take it forward, but I think I was waiting for v7 to be released first. Not sure where that is right now?

mjgp2 avatar Sep 17 '21 17:09 mjgp2

Well done @mjgp2

Is there some sort of a timeline with a release date for v7 and v8?, I'm really looking forward to dropping the ~40KB long.js from our codebase.

image

mouafa avatar Dec 01 '21 12:12 mouafa

just use this https://gist.github.com/goya/8185996e60725371bb30971bd641aad4

goya avatar Dec 01 '21 15:12 goya

@alexander-fenster - wondering if there is an ETA for v7 so I can start to work out how to take this forward?

mjgp2 avatar Jan 25 '22 15:01 mjgp2

@mjgp2 Sorry, I don't have access to releases in this repository so I cannot help here. Your best bet would be to ask the owner, @dcodeIO.

alexander-fenster avatar Feb 02 '22 01:02 alexander-fenster

Any news?

mahnunchik avatar Jul 19 '22 14:07 mahnunchik

Well, v7 has been released (woo!), and so hopefully we can start to pick this up @dcodeIO ?

mjgp2 avatar Jul 19 '22 15:07 mjgp2

Yes, we finally got v7 out - and also dropped support for older versions of Node.js (we start from 12 now). I should say I'm not sure how we properly release this if we make this change, this will be a huge breaking change for many users. I wonder if we could somehow use the configuration option to choose which type to use (and have this option for pbts as well, since the generated types are also affected).

alexander-fenster avatar Jul 20 '22 08:07 alexander-fenster

I'm assuming bigint will perform significantly better since it's a native type. If we aren't going to merge this, would it make sense to fork this repo and have an npm package under a different name?

Having an object wrapping two Numbers uses significantly more memory as well, so wouldn't it be advantageous to remove Long? We could add a Long shim around bigint for backwards compatibility.

hallahan avatar Sep 02 '22 03:09 hallahan

Any news?

mahnunchik avatar Oct 26 '22 12:10 mahnunchik

Any news?

Maligosus avatar Nov 08 '22 04:11 Maligosus

I'm not a maintainer so speaking for myself here.

Honestly I don't think this can be merged due to breaking compatibility.

What needs to be done is:

First: Add support for bigints as an option (so people can migrate to it).

Then: Maybe in 5 years, drop support of Long (since no one would be using it by then, hopefully).

It's a great patch and I wish it were merged already. I need bigints myself and don't want to start a new project with legacy junk from the start. However, I can imagine that there are millions of lines of code affected by this change and that's why it's been 2 years since the patch was written and it's still not merged. It breaks people's code.

Yes, the maintainers can merge it and force people to migrate or stay behind. However, people will generally not like that.

@dcodeIO @alexander-fenster @mjgp2 Is this kinda what the situation is? Would this be a reasonable approach to make progress?

azizghuloum avatar Dec 28 '22 04:12 azizghuloum

Why not just merge this and release a full version bump? That's what versions are for. Software that wants to keep using Long can stick with an older version.

Putting two Numbers (which are float64) in an Object to handle integers over 52 bits is a very inefficient solution.

hallahan avatar Dec 28 '22 05:12 hallahan

i agree merge this. i vote we merge this 1 year after bigint has been released in stable JS versions.

“millions of lines of code” = code generated by this library.

goya avatar Dec 28 '22 07:12 goya

i agree merge this. i vote we merge this 1 year after bigint has been released in stable JS versions.

Bigint is in all major browsers for over 2 years and since Node 10.

I think it's waiting for the owner(s) to decide how to proceed - there is a decision to be made about if it is necessary to support both Long and BigInt. I do not generate code with this library, just use it dynamically, so do not have much context on how it affects that.

mjgp2 avatar Dec 31 '22 07:12 mjgp2

@alexander-fenster ping from 2023

mahnunchik avatar May 24 '23 11:05 mahnunchik

I wait for native bigint too

phoenix741 avatar May 24 '23 12:05 phoenix741

I created a PR that's smaller in scope: #1908 which adds support for toObject({ longs: BigInt }).

jtbandes avatar Jul 06 '23 00:07 jtbandes

the concerns about breaking changes seem like they are complicating this PR, but could we get some 👀 on @jtbandes's PR https://github.com/protobufjs/protobuf.js/pull/1908 ? it seems like a simple first step that would at least unblock users like myself who want to opt in to using BigInt 😄

jmondo avatar Dec 20 '23 17:12 jmondo

2024, any progress?

shanghaikid avatar Feb 21 '24 08:02 shanghaikid

I don't have bandwidth to look at this any further - hopefully someone will be able to get some form of support for BigInt in.

mjgp2 avatar Feb 21 '24 08:02 mjgp2

😢

hallahan avatar Feb 21 '24 14:02 hallahan

😢 😢 😢

mahnunchik avatar Feb 21 '24 17:02 mahnunchik

Lol. It seems that int64/uint64 is a big problem to javascript.

cupen avatar Mar 21 '24 10:03 cupen