Wax Just Lost Weight - And It Feels Great!

in HiveDevs โ€ข 3 months ago (edited)
Authored by @mtyszczak

Wax Just Lost Weight - And It Feels Great! ๐Ÿ‹๏ธ

Hey Hive community! ๐Ÿ‘‹

We hope you've had a chance to read about the major improvements we've made recently in the Denser project, particularly regarding signer support โ€” if not, here is the source...

Honestly, the above changes would have been impossible without a huge amount of work done in a place far less exposed and glamorous than a frontend application โ€” deep inside a low-level, quietly living library. The Wax library, which we hope will become the standard way to interact with the Hive blockchain, thanks to its very tight integration with the Hive protocol's C++ code and its clean, modern TypeScript interface.

This short story, which I hope we'll expand in a future post with a detailed, "nerd-like" description of how we did it, actually covers roughly the last year (or perhaps even longer) of work to make things better, simpler, and cheaper in everyday maintenance.

That's why we decided to release a new version of Wax with a HUGE versioning leap (even though, honestly, the last few releases contained no major breaking changes to the public interface): TWO at the beginning just looks much more mature โ€” or so we hope... But enough about that โ€” let's see what we actually changed.

It is already published on npmjs. This version also comes with documentation, which you can find here.

I hope the above (perhaps a bit boring, written by yours truly โ€” @small.minion) along with the enthusiastic words below from Wax's "daddy," @mtyszczak, will bring you a few moments of joy.

You know how we've been pushing performance optimizations for our libraries? Well, this time we went the other direction - size optimization - and the results are absolutely wild! ๐Ÿคฏ

Weight Loss

๐Ÿ“‰ The Numbers Don't Lie

MetricBeforeAfterReduction
WASM binary4.6 MB2.5 MB46%
Package size (compressed)2.5 MB1.1 MB56%
Unpacked size5.5 MB3.4 MB38%

That's right - more than half of the package size just... gone! ๐Ÿ’จ

And before you ask - no, we didn't remove any features. Everything still works exactly as before. We just taught the build system some new tricks ๐Ÿ˜Ž

Mind Blown

๐Ÿ”ง How We Did It

1. Better Crypto Library Compilation ๐Ÿ”

We upgraded our emsdk toolchain to version 4.0.22 with -Oz optimized libraries. The secp256k1 cryptographic library (responsible for all the signature magic โœ๏ธ) is now pre-built with aggressive size optimizations. This alone made a HUGE difference!

2. Additional wasm-opt Pass ๐Ÿช“

We added an extra optimization phase using wasm-opt. Think of it as a second round of compression, specifically designed for WebAssembly binaries. This shaved off another ~500kB

3. Smarter Exception Handling in fc ๐Ÿง 

Here's a nerdy one: The fc library had exception handling code (FC_RETHROW_EXCEPTIONS) that was being duplicated across every template instantiation. We introduced an unpack_error_handler class that centralizes this logic into a single non-template function.

Translation: Instead of having ~3 catch handlers per template (and there are A LOT of templates), we now have one shared handler. Less code = smaller binary!

4. Excluding Virtual Operations from WASM Build ๐Ÿšซ

Virtual operations are blockchain-generated events (like author_reward_operation, curation_reward_operation, etc.) - users never create or sign them. So why include 43 extra operation types in the WASM build?

We added a compiler flag HIVE_PROTOCOL_SKIP_VOPS that excludes all virtual operations from the hive::protocol::operation type definition. Less types in the variant = less template instantiations = smaller binary!

5. Stripped-Down OpenSSL Build ๐Ÿ”“

OpenSSL is a beast of a library - but we don't need 90% of it for WASM! We built a minimal OpenSSL with only the crypto primitives we actually use:

  • Disabled: SSL/TLS/DTLS protocols, DSA, DH, RC2/4/5, Blowfish, CMS, OCSP, compression, sockets, error strings
  • Enabled: Only the bare minimum for our cryptographic needs

Combined with -fvisibility=hidden and --gc-sections linker flags, this stripped away tons of unused code!

Big Brain

โšก Same Speed, Less Weight

The best part? Zero performance impact. All those size optimizations are purely at the compilation level - the code that actually runs is still as fast as ever.

This means:

  • โฌ‡๏ธ Faster downloads for your users
  • โฌ‡๏ธ Less bandwidth for your servers
  • โฌ‡๏ธ Quicker cold starts in serverless environments
  • โœ… Same blazing fast execution

Fast

๐Ÿ“Š How Does Wax Compare to Other Hive Libraries?

You might be thinking: "But wait, isn't WASM still bigger than pure JavaScript libraries?"

Let's talk about that. We bundled simple scripts using popular Hive libraries to see the real JavaScript bundle sizes:

LibraryWhat It DoesBundle SizeGzipped
@hiveio/waxCreate foundation, format coins169 KB43 KB
@hiveio/dhiveCreate client, iterate blocks927 KB211 KB
hive-jsBroadcast Vote1.2 MB257 KB
keychain-sdkEncode a memo message2.3 MB483 KB

Yes, you read that right. The Wax JavaScript bundle alone is 5.5x smaller than dhive and 13.5x smaller than keychain-sdk! ๐Ÿคฏ

But What About the WASM File?

Fair point! Wax also needs to download the WASM binary (~2.5 MB, ~1 MB gzipped). But here's the thing:

  1. Browser Caching ๐Ÿ—„๏ธ - The WASM file is downloaded once and cached. Subsequent page loads only need the tiny 43 KB JS bundle.

  2. CDN-Friendly ๐ŸŒ - WASM files are perfectly suited for CDN distribution with long cache headers.

  3. Parallel Loading โšก - Modern browsers fetch and compile WASM in parallel with JavaScript execution.

The Real Difference: What's Inside

Here's what really matters - what are you actually downloading?

Aspect@hiveio/waxdhive / hive-js
Protocol ImplementationActual C++ hive codeJavaScript reimplementation
SerializationHF26 format (modern, preferred)Legacy format (obsolete)
CryptoNative secp256k1 via WASMJS crypto libraries
CorrectnessVerified against hived"Should work" ๐Ÿคž
API TypesAuto-generated from protocolManually maintained

The other libraries essentially reinvent the wheel in JavaScript - reimplementing serialization, crypto, and protocol logic that already exists in the official hive C++ codebase. And they're stuck with the legacy serialization format from the pre-HF26 era.

Wax takes the actual hive protocol implementation, compiles it to WASM, and wraps it with a modern TypeScript interface. You get:

  • โœ… Guaranteed correctness - same code that runs on hived
  • โœ… HF26 serialization - the modern, preferred format
  • โœ… Native performance - WASM crypto is faster than JS
  • โœ… Future-proof - protocol updates come from the source

Mic Drop

๐Ÿš€ What's Next?

Ready to try the slimmer Wax? Getting started is simple:

npm install @hiveio/wax

๐Ÿ“ฆ NPM Package: https://www.npmjs.com/package/@hiveio/wax

๐Ÿ“š Documentation: https://hive.pages.syncad.com/wax-doc

๐Ÿ”ฎ But Wait, There's More!

We're not done yet! ๐Ÿ˜

There are still a few optimization opportunities we're exploring. The goal? Making Wax as lean as possible without compromising on functionality or speed.

Stay tuned for more updates - the optimization journey continues! ๐Ÿš€

To be continued


Happy building! ๐Ÿ

@mtyszczak from @thebeedevs

Sort: ย 
ย 3 months agoย ย 

This short story, which I hope we'll expand in a future post with a detailed, "nerd-like" description of how we did it

can't wait

Playing with wax is on my to-do list...

Great job guys

That's what we like to see.
Not sure what any of it means but it all sounds super impressive and important so well done.
untitled.gif

untitled.gif

Amazing work @thebeedevs team.
Congratulations ๐ŸŽ‰

untitled.gif

This is a huge milestone.
Keep it up.
Wish you all the best for next set of optimization ๐Ÿคž

untitled.gif

I'll now have to think about this seriously.
Stop using hiveio/dhive & hiveio/hivejs & switch to this new one.
Wish me luck

untitled.gif