Skip to content

Commit

Permalink
perf: reduce usage of strlen on each line to improve performance in b…
Browse files Browse the repository at this point in the history
…indings

luajit uses fat pointer to store size, so it should be more performant
to access the already available size of a str, rather than doing a
strlen on each line
  • Loading branch information
Conni2461 committed Sep 24, 2023
1 parent 6c921ca commit 74ebd27
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 18 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ fzf_slab_t *slab = fzf_make_default_slab();
fzf_pattern_t *pattern = fzf_parse_pattern(CaseSmart, false, "src | lua !.c$", true);

/* you can get the score/position for as many items as you want */
int score = fzf_get_score(line, pattern, slab);
fzf_position_t *pos = fzf_get_positions(line, pattern, slab);
int score = fzf_get_score(line, strlen(line), pattern, slab);
fzf_position_t *pos = fzf_get_positions(line, strlen(line), pattern, slab);

fzf_free_positions(pos);
fzf_free_pattern(pattern);
Expand All @@ -141,10 +141,10 @@ local pattern_obj = fzf.parse_pattern(pattern, case_mode, fuzzy)
-- you can get the score/position for as many items as you want
-- line: string
-- score: number
local score = fzf.get_score(line, pattern_obj, slab)
local score = fzf.get_score(line, #line, pattern_obj, slab)
-- table (does not have to be freed)
local pos = fzf.get_pos(line, pattern_obj, slab)
local pos = fzf.get_pos(line, #line, pattern_obj, slab)
fzf.free_pattern(pattern_obj)
fzf.free_slab(slab)
Expand Down
8 changes: 4 additions & 4 deletions lua/fzf_lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ ffi.cdef [[
size_t cap;
} fzf_position_t;

fzf_position_t *fzf_get_positions(const char *text, fzf_pattern_t *pattern, fzf_slab_t *slab);
fzf_position_t *fzf_get_positions(const char *text, size_t text_len, fzf_pattern_t *pattern, fzf_slab_t *slab);
void fzf_free_positions(fzf_position_t *pos);
int32_t fzf_get_score(const char *text, fzf_pattern_t *pattern, fzf_slab_t *slab);
int32_t fzf_get_score(const char *text, size_t text_len, fzf_pattern_t *pattern, fzf_slab_t *slab);

fzf_pattern_t *fzf_parse_pattern(int32_t case_mode, bool normalize, char *pattern, bool fuzzy);
void fzf_free_pattern(fzf_pattern_t *pattern);
Expand All @@ -44,11 +44,11 @@ ffi.cdef [[
local fzf = {}

fzf.get_score = function(input, pattern_struct, slab)
return native.fzf_get_score(input, pattern_struct, slab)
return native.fzf_get_score(input, #input, pattern_struct, slab)
end

fzf.get_pos = function(input, pattern_struct, slab)
local pos = native.fzf_get_positions(input, pattern_struct, slab)
local pos = native.fzf_get_positions(input, #input, pattern_struct, slab)
if pos == nil then
return
end
Expand Down
10 changes: 5 additions & 5 deletions src/fzf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1141,15 +1141,15 @@ void fzf_free_pattern(fzf_pattern_t *pattern) {
SFREE(pattern);
}

int32_t fzf_get_score(const char *text, fzf_pattern_t *pattern,
int32_t fzf_get_score(const char *text, size_t text_len, fzf_pattern_t *pattern,
fzf_slab_t *slab) {
// If the pattern is an empty string then pattern->ptr will be NULL and we
// basically don't want to filter. Return 1 for telescope
if (pattern->ptr == NULL) {
return 1;
}

fzf_string_t input = {.data = text, .size = strlen(text)};
fzf_string_t input = {.data = text, .size = text_len};
if (pattern->only_inv) {
int final = 0;
for (size_t i = 0; i < pattern->size; i++) {
Expand Down Expand Up @@ -1194,15 +1194,15 @@ int32_t fzf_get_score(const char *text, fzf_pattern_t *pattern,
return total_score;
}

fzf_position_t *fzf_get_positions(const char *text, fzf_pattern_t *pattern,
fzf_slab_t *slab) {
fzf_position_t *fzf_get_positions(const char *text, size_t text_len,
fzf_pattern_t *pattern, fzf_slab_t *slab) {
// If the pattern is an empty string then pattern->ptr will be NULL and we
// basically don't want to filter. Return 1 for telescope
if (pattern->ptr == NULL) {
return NULL;
}

fzf_string_t input = {.data = text, .size = strlen(text)};
fzf_string_t input = {.data = text, .size = text_len};
fzf_position_t *all_pos = fzf_pos_array(0);
for (size_t i = 0; i < pattern->size; i++) {
fzf_term_set_t *term_set = pattern->ptr[i];
Expand Down
6 changes: 3 additions & 3 deletions src/fzf.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ fzf_pattern_t *fzf_parse_pattern(fzf_case_types case_mode, bool normalize,
char *pattern, bool fuzzy);
void fzf_free_pattern(fzf_pattern_t *pattern);

int32_t fzf_get_score(const char *text, fzf_pattern_t *pattern,
int32_t fzf_get_score(const char *text, size_t text_len, fzf_pattern_t *pattern,
fzf_slab_t *slab);

fzf_position_t *fzf_pos_array(size_t len);
fzf_position_t *fzf_get_positions(const char *text, fzf_pattern_t *pattern,
fzf_slab_t *slab);
fzf_position_t *fzf_get_positions(const char *text, size_t text_len,
fzf_pattern_t *pattern, fzf_slab_t *slab);
void fzf_free_positions(fzf_position_t *pos);

fzf_slab_t *fzf_make_slab(fzf_slab_config_t config);
Expand Down
6 changes: 4 additions & 2 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,8 @@ static void score_wrapper(char *pattern, char **input, int *expected) {
fzf_slab_t *slab = fzf_make_default_slab();
fzf_pattern_t *pat = fzf_parse_pattern(CaseSmart, false, pattern, true);
for (size_t i = 0; input[i] != NULL; ++i) {
ASSERT_EQ(expected[i], fzf_get_score(input[i], pat, slab));
ASSERT_EQ(expected[i],
fzf_get_score(input[i], strlen(input[i]), pat, slab));
}
fzf_free_pattern(pat);
fzf_free_slab(slab);
Expand Down Expand Up @@ -674,7 +675,8 @@ static void pos_wrapper(char *pattern, char **input, int **expected) {
fzf_slab_t *slab = fzf_make_default_slab();
fzf_pattern_t *pat = fzf_parse_pattern(CaseSmart, false, pattern, true);
for (size_t i = 0; input[i] != NULL; ++i) {
fzf_position_t *pos = fzf_get_positions(input[i], pat, slab);
fzf_position_t *pos =
fzf_get_positions(input[i], strlen(input[i]), pat, slab);
if (!pos) {
ASSERT_EQ((void *)pos, expected[i]);
continue;
Expand Down

0 comments on commit 74ebd27

Please sign in to comment.