pyth-network / pyth-client

Client API for on-chain pyth programs
https://pyth.network
Apache License 2.0
169 stars 102 forks source link

refactor: replace merge-sort with heapsort #405

Closed ali-bahjati closed 4 months ago

ali-bahjati commented 5 months ago

This change replaces the merge-sort with a heapsort that uses much less CU than merge-sort.

The previous algorithm is very fast running on a normal CPU but doesn't work very well in BPF because all instructions (like load, move, ..) have the same cost and there is no cache and optimizations for memory-alignment have no real benefit.

The major benefits of heapsort are being non-recursive that reduces the high stackframe overhead in BPF and being inplace which minimizes number of copies.

Unfortunately there is no way to systematically get the the compute usage out of program test. The test_benchmark file has a simple code that helps running benchmarks on various number of publishers.

In 32-publisher setup, heapsort reduces the CU from 16.5k to 12k and in the 64-publisher setup 37k to 20.5k. The numbers are the worst cases running on randomized input. The result of running in a highly similar input (two distinct prices, and two distinct confidences) is 14.7k and 25k respectively, which is still better and is very unlikely in practice.