Skip to content

Commit 64efff7

Browse files
authored
perf: ffi performance improvements (#19)
1 parent 8557aa8 commit 64efff7

File tree

3 files changed

+54
-47
lines changed

3 files changed

+54
-47
lines changed

lua/fzf_lib.lua

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ ffi.cdef[[
2525
size_t cap;
2626
} fzf_position_t;
2727

28-
fzf_position_t *fzf_get_positions(char *text, fzf_pattern_t *pattern, fzf_slab_t *slab);
28+
fzf_position_t *fzf_get_positions(const char *text, fzf_pattern_t *pattern, fzf_slab_t *slab);
2929
void fzf_free_positions(fzf_position_t *pos);
30-
int32_t fzf_get_score(char *text, fzf_pattern_t *pattern, fzf_slab_t *slab);
30+
int32_t fzf_get_score(const char *text, fzf_pattern_t *pattern, fzf_slab_t *slab);
3131

3232
fzf_pattern_t *fzf_parse_pattern(int32_t case_mode, bool normalize, char *pattern, bool fuzzy);
3333
void fzf_free_pattern(fzf_pattern_t *pattern);
@@ -39,15 +39,11 @@ ffi.cdef[[
3939
local fzf = {}
4040

4141
fzf.get_score = function(input, pattern_struct, slab)
42-
local text = ffi.new("char[?]", #input + 1)
43-
ffi.copy(text, input)
44-
return native.fzf_get_score(text, pattern_struct, slab)
42+
return native.fzf_get_score(input, pattern_struct, slab)
4543
end
4644

4745
fzf.get_pos = function(input, pattern_struct, slab)
48-
local text = ffi.new("char[?]", #input + 1)
49-
ffi.copy(text, input)
50-
local pos = native.fzf_get_positions(text, pattern_struct, slab)
46+
local pos = native.fzf_get_positions(input, pattern_struct, slab)
5147
local res = {}
5248
for i = 1, tonumber(pos.size) do
5349
res[i] = pos.data[i - 1] + 1

src/fzf.c

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
gen_slice(i16, int16_t);
3838
gen_simple_slice(i32, int32_t);
39-
gen_slice(str, char);
39+
gen_slice(str, const char);
4040
#undef gen_slice
4141
#undef gen_simple_slice
4242

@@ -64,7 +64,7 @@ typedef enum {
6464
} char_types;
6565

6666
typedef struct {
67-
char *data;
67+
const char *data;
6868
size_t size;
6969
} fzf_string_t;
7070

@@ -124,16 +124,18 @@ static char *trim_left(char *str, size_t *len, char trim) {
124124
return str;
125125
}
126126

127-
static bool has_prefix(char *str, char *prefix, size_t prefix_len) {
127+
static bool has_prefix(const char *str, const char *prefix, size_t prefix_len) {
128128
return strncmp(prefix, str, prefix_len) == 0;
129129
}
130130

131-
static bool has_suffix(char *str, size_t len, char *suffix, size_t suffix_len) {
131+
static bool has_suffix(const char *str, size_t len, const char *suffix,
132+
size_t suffix_len) {
132133
return len >= suffix_len &&
133134
strncmp(slice_str(str, len - suffix_len, len).data, suffix,
134135
suffix_len) == 0;
135136
}
136137

138+
// TODO(conni2461): REFACTOR
137139
static char *str_replace(char *orig, char *rep, char *with) {
138140
char *result, *ins, *tmp;
139141
size_t len_rep, len_with, len_front, count;
@@ -173,6 +175,7 @@ static char *str_replace(char *orig, char *rep, char *with) {
173175
return result;
174176
}
175177

178+
// TODO(conni2461): REFACTOR
176179
static char *str_tolower(char *str, size_t size) {
177180
char *lower_str = (char *)malloc((size + 1) * sizeof(char));
178181
for (size_t i = 0; i < size; i++) {
@@ -339,7 +342,7 @@ static int32_t try_skip(fzf_string_t *input, bool case_sensitive, byte b,
339342
return from + idx;
340343
}
341344

342-
static bool is_ascii(char *runes, size_t size) {
345+
static bool is_ascii(const char *runes, size_t size) {
343346
// TODO(conni2461): future use
344347
/* for (size_t i = 0; i < size; i++) { */
345348
/* if (runes[i] >= 256) { */
@@ -349,7 +352,7 @@ static bool is_ascii(char *runes, size_t size) {
349352
return true;
350353
}
351354

352-
static int32_t ascii_fuzzy_index(fzf_string_t *input, char *pattern,
355+
static int32_t ascii_fuzzy_index(fzf_string_t *input, const char *pattern,
353356
size_t size, bool case_sensitive) {
354357
if (!is_ascii(pattern, size)) {
355358
return -1;
@@ -498,8 +501,8 @@ static fzf_result_t __fuzzy_match_v1(bool case_sensitive, bool normalize,
498501
}
499502

500503
fzf_result_t fzf_fuzzy_match_v1(bool case_sensitive, bool normalize,
501-
char *input, char *pattern, bool with_pos,
502-
fzf_slab_t *slab) {
504+
const char *input, const char *pattern,
505+
bool with_pos, fzf_slab_t *slab) {
503506
fzf_string_t input_wrap = {.data = input, .size = strlen(input)};
504507
fzf_string_t pattern_wrap = {.data = pattern, .size = strlen(pattern)};
505508
return __fuzzy_match_v1(case_sensitive, normalize, &input_wrap, &pattern_wrap,
@@ -753,8 +756,8 @@ static fzf_result_t __fuzzy_match_v2(bool case_sensitive, bool normalize,
753756
}
754757

755758
fzf_result_t fzf_fuzzy_match_v2(bool case_sensitive, bool normalize,
756-
char *input, char *pattern, bool with_pos,
757-
fzf_slab_t *slab) {
759+
const char *input, const char *pattern,
760+
bool with_pos, fzf_slab_t *slab) {
758761
fzf_string_t input_wrap = {.data = input, .size = strlen(input)};
759762
fzf_string_t pattern_wrap = {.data = pattern, .size = strlen(pattern)};
760763
return __fuzzy_match_v2(case_sensitive, normalize, &input_wrap, &pattern_wrap,
@@ -829,8 +832,8 @@ static fzf_result_t __exact_match_naive(bool case_sensitive, bool normalize,
829832
}
830833

831834
fzf_result_t fzf_exact_match_naive(bool case_sensitive, bool normalize,
832-
char *input, char *pattern, bool with_pos,
833-
fzf_slab_t *slab) {
835+
const char *input, const char *pattern,
836+
bool with_pos, fzf_slab_t *slab) {
834837
fzf_string_t input_wrap = {.data = input, .size = strlen(input)};
835838
fzf_string_t pattern_wrap = {.data = pattern, .size = strlen(pattern)};
836839
return __exact_match_naive(case_sensitive, normalize, &input_wrap,
@@ -872,8 +875,9 @@ static fzf_result_t __prefix_match(bool case_sensitive, bool normalize,
872875
return (fzf_result_t){(int32_t)start, (int32_t)end, score, NULL};
873876
}
874877

875-
fzf_result_t fzf_prefix_match(bool case_sensitive, bool normalize, char *input,
876-
char *pattern, bool with_pos, fzf_slab_t *slab) {
878+
fzf_result_t fzf_prefix_match(bool case_sensitive, bool normalize,
879+
const char *input, const char *pattern,
880+
bool with_pos, fzf_slab_t *slab) {
877881
fzf_string_t input_wrap = {.data = input, .size = strlen(input)};
878882
fzf_string_t pattern_wrap = {.data = pattern, .size = strlen(pattern)};
879883
return __prefix_match(case_sensitive, normalize, &input_wrap, &pattern_wrap,
@@ -918,8 +922,9 @@ static fzf_result_t __suffix_match(bool case_sensitive, bool normalize,
918922
return (fzf_result_t){(int32_t)start, (int32_t)end, score, NULL};
919923
}
920924

921-
fzf_result_t fzf_suffix_match(bool case_sensitive, bool normalize, char *input,
922-
char *pattern, bool with_pos, fzf_slab_t *slab) {
925+
fzf_result_t fzf_suffix_match(bool case_sensitive, bool normalize,
926+
const char *input, const char *pattern,
927+
bool with_pos, fzf_slab_t *slab) {
923928
fzf_string_t input_wrap = {.data = input, .size = strlen(input)};
924929
fzf_string_t pattern_wrap = {.data = pattern, .size = strlen(pattern)};
925930
return __suffix_match(case_sensitive, normalize, &input_wrap, &pattern_wrap,
@@ -944,10 +949,9 @@ static fzf_result_t __equal_match(bool case_sensitive, bool normalize,
944949
bool match = true;
945950
if (normalize) {
946951
// TODO(conni2461): to rune
947-
char *runes = text->data;
948952
for (size_t idx = 0; idx < len_pattern; idx++) {
949953
char pchar = pattern->data[idx];
950-
char c = runes[trimmed_len + idx];
954+
char c = text->data[trimmed_len + idx];
951955
if (!case_sensitive) {
952956
c = (char)tolower(c);
953957
}
@@ -958,11 +962,9 @@ static fzf_result_t __equal_match(bool case_sensitive, bool normalize,
958962
}
959963
} else {
960964
// TODO(conni2461): to rune
961-
char *runes = text->data;
962-
963965
for (size_t idx = 0; idx < len_pattern; idx++) {
964966
char pchar = pattern->data[idx];
965-
char c = runes[trimmed_len + idx];
967+
char c = text->data[trimmed_len + idx];
966968
if (!case_sensitive) {
967969
c = (char)tolower(c);
968970
}
@@ -982,8 +984,9 @@ static fzf_result_t __equal_match(bool case_sensitive, bool normalize,
982984
return (fzf_result_t){-1, -1, 0, NULL};
983985
}
984986

985-
fzf_result_t fzf_equal_match(bool case_sensitive, bool normalize, char *input,
986-
char *pattern, bool with_pos, fzf_slab_t *slab) {
987+
fzf_result_t fzf_equal_match(bool case_sensitive, bool normalize,
988+
const char *input, const char *pattern,
989+
bool with_pos, fzf_slab_t *slab) {
987990
fzf_string_t input_wrap = {.data = input, .size = strlen(input)};
988991
fzf_string_t pattern_wrap = {.data = pattern, .size = strlen(pattern)};
989992
return __equal_match(case_sensitive, normalize, &input_wrap, &pattern_wrap,
@@ -1039,6 +1042,7 @@ static fzf_result_t fzf_call_alg(fzf_term_t *term, bool normalize,
10391042
(fzf_string_t *)term->text, with_pos, slab);
10401043
}
10411044

1045+
// TODO(conni2461): REFACTOR
10421046
/* assumption (maybe i change that later)
10431047
* - always v2 alg
10441048
* - bool extended always true (thats the whole point of this isn't it)
@@ -1182,7 +1186,8 @@ void fzf_free_pattern(fzf_pattern_t *pattern) {
11821186
free(pattern);
11831187
}
11841188

1185-
int32_t fzf_get_score(char *text, fzf_pattern_t *pattern, fzf_slab_t *slab) {
1189+
int32_t fzf_get_score(const char *text, fzf_pattern_t *pattern,
1190+
fzf_slab_t *slab) {
11861191
fzf_string_t input = {.data = text, .size = strlen(text)};
11871192

11881193
if (pattern->only_inv) {
@@ -1225,7 +1230,7 @@ int32_t fzf_get_score(char *text, fzf_pattern_t *pattern, fzf_slab_t *slab) {
12251230
return total_score;
12261231
}
12271232

1228-
fzf_position_t *fzf_get_positions(char *text, fzf_pattern_t *pattern,
1233+
fzf_position_t *fzf_get_positions(const char *text, fzf_pattern_t *pattern,
12291234
fzf_slab_t *slab) {
12301235
fzf_string_t input = {.data = text, .size = strlen(text)};
12311236

src/fzf.h

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,27 +69,33 @@ typedef struct {
6969
bool only_inv;
7070
} fzf_pattern_t;
7171

72-
fzf_result_t fzf_fuzzy_match_v1(bool case_sensitive, bool normalize, char *text,
73-
char *pattern, bool with_pos, fzf_slab_t *slab);
74-
fzf_result_t fzf_fuzzy_match_v2(bool case_sensitive, bool normalize, char *text,
75-
char *pattern, bool with_pos, fzf_slab_t *slab);
72+
fzf_result_t fzf_fuzzy_match_v1(bool case_sensitive, bool normalize,
73+
const char *text, const char *pattern,
74+
bool with_pos, fzf_slab_t *slab);
75+
fzf_result_t fzf_fuzzy_match_v2(bool case_sensitive, bool normalize,
76+
const char *text, const char *pattern,
77+
bool with_pos, fzf_slab_t *slab);
7678
fzf_result_t fzf_exact_match_naive(bool case_sensitive, bool normalize,
77-
char *text, char *pattern, bool with_pos,
78-
fzf_slab_t *slab);
79-
fzf_result_t fzf_prefix_match(bool case_sensitive, bool normalize, char *text,
80-
char *pattern, bool with_pos, fzf_slab_t *slab);
81-
fzf_result_t fzf_suffix_match(bool case_sensitive, bool normalize, char *text,
82-
char *pattern, bool with_pos, fzf_slab_t *slab);
83-
fzf_result_t fzf_equal_match(bool case_sensitive, bool normalize, char *text,
84-
char *pattern, bool with_pos, fzf_slab_t *slab);
79+
const char *text, const char *pattern,
80+
bool with_pos, fzf_slab_t *slab);
81+
fzf_result_t fzf_prefix_match(bool case_sensitive, bool normalize,
82+
const char *text, const char *pattern,
83+
bool with_pos, fzf_slab_t *slab);
84+
fzf_result_t fzf_suffix_match(bool case_sensitive, bool normalize,
85+
const char *text, const char *pattern,
86+
bool with_pos, fzf_slab_t *slab);
87+
fzf_result_t fzf_equal_match(bool case_sensitive, bool normalize,
88+
const char *text, const char *pattern,
89+
bool with_pos, fzf_slab_t *slab);
8590

8691
/* interface */
8792
fzf_pattern_t *fzf_parse_pattern(fzf_case_types case_mode, bool normalize,
8893
char *pattern, bool fuzzy);
8994
void fzf_free_pattern(fzf_pattern_t *pattern);
9095

91-
int32_t fzf_get_score(char *text, fzf_pattern_t *pattern, fzf_slab_t *slab);
92-
fzf_position_t *fzf_get_positions(char *text, fzf_pattern_t *pattern,
96+
int32_t fzf_get_score(const char *text, fzf_pattern_t *pattern,
97+
fzf_slab_t *slab);
98+
fzf_position_t *fzf_get_positions(const char *text, fzf_pattern_t *pattern,
9399
fzf_slab_t *slab);
94100
void fzf_free_positions(fzf_position_t *pos);
95101

0 commit comments

Comments
 (0)