stellar / js-xdr

Read/write XDR encoded data structures (RFC 4506)
Apache License 2.0
25 stars 31 forks source link

Refactor XDR serialization/deserialization logic #91

Closed orbitlens closed 1 year ago

orbitlens commented 1 year ago

Minified browser bundle size decreased from 335 KB (prev version) to 43 KB (this version). Memory consumption during serialization decreased due to the smaller writer buffer allocation.

We already actively use it as a drop-in replacement for original js-xdr package for ingestion. So far, no compatibility issues have been found.

Also I created a primitive performance benchmark for Node.js to test serialization/deserialization speed for refactored version vs original. It shows significant performance boost, up to 5X depending on the serialized type.

Benchmark link. Benchmark results on my computer:

Performance benchmark
10 samples 10000 iterations each
------------------------------------------------
LedgerHeader.fromXDR - new: 0.0435s (min 0.038s - max 0.068s)
LedgerHeader.fromXDR - old: 0.272s (min 0.264s - max 0.302s)
difference: 6.3X

LedgerHeader.toXDR - new: 0.0698s (min 0.06s - max 0.101s)
LedgerHeader.toXDR - old: 0.348s (min 0.263s - max 0.558s)
difference: 5.0X

TransactionEnvelope.fromXDR - new: 0.423s (min 0.412s - max 0.477s)
TransactionEnvelope.fromXDR - old: 1.77s (min 1.701s - max 1.918s)
difference: 4.2X

TransactionEnvelope.toXDR - new: 0.436s (min 0.342s - max 0.715s)
TransactionEnvelope.toXDR - old: 0.646s (min 0.613s - max 0.695s)
difference: 1.5X

TransactionMeta.fromXDR - new: 3.20s (min 2.694s - max 4.016s)
TransactionMeta.fromXDR - old: 9.76s (min 9.543s - max 10.194s)
difference: 3.1X

TransactionMeta.toXDR - new: 2.35s (min 2.149s - max 2.549s)
TransactionMeta.toXDR - old: 3.19s (min 2.934s - max 3.46s)
difference: 1.4X

TransactionResult.fromXDR - new: 0.0370s (min 0.034s - max 0.056s)
TransactionResult.fromXDR - old: 0.0627s (min 0.059s - max 0.074s)
difference: 1.7X

TransactionResult.toXDR - new: 0.0626s (min 0.057s - max 0.07s)
TransactionResult.toXDR - old: 0.218s (min 0.184s - max 0.272s)
difference: 3.5X
orbitlens commented 1 year ago

Wow, Herculean stuff 👏 It'd be nice if we had .x -> .js tests to see if there were any output changes.

I tried generating .x -> .js with examples/test.x (via yarn run test-generate), but of course it makes sense to test with .x files from stellar-core. Could you please check the generated output for the difference? Because I'm a bit lost between various versions of stellar-core and curr/next XDR.

tomerweller commented 1 year ago

Great work. thanks @orbitlens!