Skip to content

Commit d455638

Browse files
committed
chore: add more docs
1 parent 62f139d commit d455638

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

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

+16
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ void heapsort(int64_t * a, uint64_t n) {
4343
}
4444
}
4545

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

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

+9-1
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

+3
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

+1-1
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)