bufbuild / protobuf-es

Protocol Buffers for ECMAScript. The only JavaScript Protobuf library that is fully-compliant with Protobuf conformance tests.
Apache License 2.0
1.15k stars 68 forks source link

Treeshaking for v2 #969

Closed lukasIO closed 2 months ago

lukasIO commented 2 months ago

Hi,

I was looking through the differences in generated code between v1 and v2 and noticed that each file gets a fileDesc that seemingly holds all (?) schema definitions of that file.

For a single message that means that it makes use of that fileDesc directly and needs a reference to the whole schema definition of that file.

I haven't done any size comparison testings yet so sorry if I'm jumping to conclusions here, but that sounds like tree shaking for single/few message imports won't work anymore or is at least very limited?

srikrsna-buf commented 2 months ago

Tree shaking is limited today, but the size of the generated code has been greatly reduced from v1. Instead of generating multiple classes and service definitions, we instead embed the entire descriptor, and the rest are types and selector functions. The overall generated code is low enough to offset most tree-shaking benefits v1 may have had. For comparison, you can look at the charts against google-protobuf. They also demonstrate how little v2 changes as new files are added.

With tree shaking, it really depends on the bundler, and as such there is no definitive way to measure how effective it really is. Reducing the overall generated code, on the other hand, will benefit bundle sizes regardless of bundler.

Closing this for now, feel free to reopen if you see a bundle size issue.

lukasIO commented 2 months ago

Thanks for the response!

the diff between v1 and v2 seems to suggest there's an inflection point at ~16 files (depending on whether you're looking at minified or gzipped). Which makes sense given the new approach.

When using less files v1 seems to be smaller plus it retains tree shaking ability.