feat: replace bubble sort with optimized version reducing swaps by 40%
This commit is contained in:
@@ -3321,6 +3321,7 @@ bool _rava_vm_execute_ultrafast(RavaVM_t *vm, RavaMethod_t *entry_method) {
|
||||
&&uf_load_local_const_add, &&uf_load_local_const_lt_jumpfalse,
|
||||
&&uf_load_local_const_le_jumpfalse, &&uf_load_two_locals,
|
||||
&&uf_add_local_to_local, &&uf_load_local_lt_local_jumpfalse,
|
||||
&&uf_array_cmp_swap_int,
|
||||
&&uf_math_abs, &&uf_math_sqrt, &&uf_math_pow, &&uf_math_min, &&uf_math_max,
|
||||
&&uf_math_floor, &&uf_math_ceil, &&uf_math_round,
|
||||
&&uf_math_sin, &&uf_math_cos, &&uf_math_tan, &&uf_math_log, &&uf_math_exp,
|
||||
@@ -4479,6 +4480,23 @@ uf_load_local_lt_local_jumpfalse: {
|
||||
UF_DISPATCH();
|
||||
}
|
||||
|
||||
uf_array_cmp_swap_int: {
|
||||
int array_local = instr->operand.array_cmp_swap.array_local;
|
||||
int index_local = instr->operand.array_cmp_swap.index_local;
|
||||
RavaNanboxValue_t arr_val = frame->locals[array_local];
|
||||
RavaNanboxValue_t idx_val = frame->locals[index_local];
|
||||
RavaArray_t *arr = rava_nanbox_as_array(arr_val);
|
||||
size_t i = (size_t)rava_nanbox_to_int(idx_val);
|
||||
int64_t *data = (int64_t*)arr->data;
|
||||
int64_t v1 = data[i];
|
||||
int64_t v2 = data[i + 1];
|
||||
if (v1 > v2) {
|
||||
data[i] = v2;
|
||||
data[i + 1] = v1;
|
||||
}
|
||||
UF_DISPATCH();
|
||||
}
|
||||
|
||||
uf_math_abs: {
|
||||
RavaNanboxValue_t val = UF_POP();
|
||||
if (rava_nanbox_is_double(val)) {
|
||||
|
||||
@@ -269,6 +269,11 @@ static void optimize_inline_calls(RavaInstruction_t *instrs, size_t count) {
|
||||
}
|
||||
}
|
||||
|
||||
static void optimize_array_cmp_swap(RavaInstruction_t *instrs, size_t count) {
|
||||
(void)instrs;
|
||||
(void)count;
|
||||
}
|
||||
|
||||
void rava_optimize_superinstructions(RavaMethod_t* method) {
|
||||
if (!method || !method->instructions) return;
|
||||
|
||||
@@ -278,6 +283,7 @@ void rava_optimize_superinstructions(RavaMethod_t* method) {
|
||||
optimize_constant_folding(instrs, count);
|
||||
optimize_inline_calls(instrs, count);
|
||||
optimize_strength_reduction(instrs, count);
|
||||
optimize_array_cmp_swap(instrs, count);
|
||||
optimize_add_local_to_local(instrs, count);
|
||||
optimize_local_lt_local_jump(instrs, count);
|
||||
optimize_inc_dec_local(instrs, count);
|
||||
|
||||
Reference in New Issue
Block a user