Skip to content

Commit 0302196

Browse files
committed
chore: add more docs
1 parent 312d079 commit 0302196

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

program/c/src/oracle/model/price_model.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@ void heapsort(int64_t * a, uint64_t n) {
4545
}
4646
}
4747

48+
/*
49+
* Find the 25, 50, and 75 percentiles of the given quotes using heapsort.
50+
*
51+
* This implementation optimizes the price_model_core function for minimal compute unit usage in BPF.
52+
*
53+
* In Solana, each BPF instruction costs 1 unit of compute and is much different than a native code
54+
* execution time. Here are some of the differences:
55+
* 1. There is no cache, so memory access is much more expensive.
56+
* 2. The instruction set is very minimal, and there are only 10 registers available.
57+
* 3. The BPF compiler is not very good at optimizing the code.
58+
* 4. The stack size is limited and having extra stack frame has high overhead.
59+
*
60+
* This implementation is chosen among other implementations such as merge-sort, quick-sort, and quick-select
61+
* because it is very fast, has small number of instructions, and has a very small memory footprint by being
62+
* in-place and is non-recursive.
63+
*/
4864
int64_t *
4965
price_model_core( uint64_t cnt,
5066
int64_t * quote,

program/c/src/oracle/model/price_model.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,16 @@
88
extern "C" {
99
#endif
1010

11+
/*
12+
* This function computes the p25, p50 and p75 percentiles of the given quotes and
13+
* writes them to the given pointers. It also returns the sorted quotes array. Being
14+
* sorted is not necessary for this model to work, and is only relied upon by the
15+
* tests to verify the correctness of the model with more confidence.
16+
*
17+
* The quote array might get modified by this function.
18+
*/
1119
int64_t *
12-
price_model_core( uint64_t cnt, /* Assumes price_model_cnt_valid( cnt ) is true */
20+
price_model_core( uint64_t cnt, /* Number of elements in quote */
1321
int64_t * quote, /* Assumes quote[i] for i in [0,cnt) is the i-th quote on input */
1422
int64_t * _p25, /* Assumes *_p25 is safe to write to the p25 model output */
1523
int64_t * _p50, /* Assumes *_p50 " */

program/c/src/oracle/model/test_price_model.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ int test_price_model() {
3939

4040
/* Validate the results */
4141

42+
/*
43+
* Although being sorted is not necessary it gives us more confidence about the correctness of the model.
44+
*/
4245
qsort( quote0, (size_t)cnt, sizeof(int64_t), qcmp );
4346
if( memcmp( quote, quote0, sizeof(int64_t)*(size_t)cnt ) ) { printf( "FAIL (sort)\n" ); return 1; }
4447

program/rust/src/tests/test_benchmark.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ async fn run_benchmark(num_publishers: usize) -> Result<(), Box<dyn std::error::
3636
sim.add_publisher(&price_keypair, *publisher).await?;
3737
}
3838

39-
// Set the seed to make the test deterministic
39+
// Set the seed to make the test is deterministic
4040
let mut rnd = rand::rngs::SmallRng::seed_from_u64(14);
4141

4242
for kp in publishers_keypairs.iter() {

0 commit comments

Comments
 (0)