Skip to content

Commit

Permalink
Optimise the YkLocation buffer.
Browse files Browse the repository at this point in the history
Location array was reallocated for every instructions, which was very ineffective
since we only need to reallocate the buffer either when yk location is set to NULL
or when the number of bytecode instructions is greater than number of location
in the yk location array. This was causing around 1000x slowdown in benchmark
test such as verybig.lua. The fix improves verybig.lua by 300x and
brings performance from 198sec to 0.650sec when JIT is off.
  • Loading branch information
nmdis1999 committed Mar 28, 2024
1 parent 5ba2794 commit 6c06223
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/lyk.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,24 @@ void set_location(Proto *f, int i) {
}

inline void yk_on_instruction_loaded(Proto *f, Instruction i, int idx) {
// YKOPT: Reallocating for every instruction is inefficient.
YkLocation **new_locations = calloc(f->sizecode, sizeof(YkLocation *));
lua_assert(new_locations != NULL && "Expected yklocs to be defined!");
if (f->yklocs == NULL) {
// Allocate initial array
f->yklocs = calloc(f->sizecode, sizeof(YkLocation *));
lua_assert(f->yklocs != NULL && "Expected yklocs to be defined!");
f->yklocs_size = f->sizecode;
} else if (f->sizecode > f->yklocs_size) {
// Extend the array
YkLocation **new_locations =
realloc(f->yklocs, f->sizecode * sizeof(YkLocation *));
lua_assert(new_locations != NULL && "Expected yklocs to be defined!");

// copy previous locations over
if (f->yklocs != NULL) {
for (int i = 0; i < f->yklocs_size; i++) {
if (f->yklocs[i] != NULL) {
new_locations[i] = f->yklocs[i];
} else {
new_locations[i] = NULL;
}
for (int i = f->yklocs_size; i < f->sizecode; i++) {
new_locations[i] = NULL;
}
free(f->yklocs);

f->yklocs = new_locations;
f->yklocs_size = f->sizecode;
}
f->yklocs = new_locations;
f->yklocs_size = f->sizecode;
if (is_loop_start(i)) {
set_location(f, idx);
}
Expand Down

0 comments on commit 6c06223

Please sign in to comment.