Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reallocting location array only when bytecode size is greater than the location size. #84

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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