celestiaorg / go-square

A library for encoding blobs into a 2D square of evenly sized chunks designed for sampling and reconstruction
Apache License 2.0
13 stars 16 forks source link

perf(namespace): speed up Namespace comparisons #63

Closed cristaloleg closed 3 months ago

cristaloleg commented 3 months ago

Overview

While reading and checking structures from https://celestiaorg.github.io/celestia-app/specs/data_structures.html I noticed that we compare namespaces not optimal enough. Every equal or compare-like operation allocates new slice with data that can be already compared directly.

% go-perftuner bstat a.txt b.txt
args: [a.txt b.txt]name        old time/op    new time/op    delta
Equal-10      71.9ns ± 0%     3.9ns ± 0%   -94.53%  (p=0.008 n=5+5)
Compare-10    72.0ns ± 1%     4.4ns ± 5%   -93.83%  (p=0.002 n=6+6)

name        old alloc/op   new alloc/op   delta
Equal-10       64.0B ± 0%      0.0B       -100.00%  (p=0.026 n=5+6)
Compare-10     64.0B ± 0%      0.0B       -100.00%  (p=0.002 n=6+6)

name        old allocs/op  new allocs/op  delta
Equal-10        2.00 ± 0%      0.00       -100.00%  (p=0.026 n=5+6)
Compare-10      2.00 ± 0%      0.00       -100.00%  (p=0.002 n=6+6)
Wondertan commented 3 months ago

It would be nice to have the same change in the node share pkg. Currently, we duplicate the Namespace type there and implement it as a byte slice rather than a struct.

cristaloleg commented 3 months ago

Thanks everyone, will address comments tomorrow 🫡