Browse code

Precompute tolower in match_positions

This makes match_positions significantly faster on MacOS by calling
tolower() O(N) times instead of O(N*N)

Before:

$ time ./fzy -e linux --benchmark < linux_files.txt
./fzy -e linux --benchmark < linux_files.txt 13.24s user 0.03s system 381% cpu 3.483 total

After:

$ time ./fzy -e linux --benchmark < linux_files.txt
./fzy -e linux --benchmark < linux_files.txt 4.57s user 0.02s system 381% cpu 1.204 total

John Hawthorn authored on 13/10/2018 21:05:10
Showing 1 changed files

... ...
@@ -94,6 +94,15 @@ score_t match_positions(const char *needle, const char *haystack, size_t *positi
94 94
 		return SCORE_MIN;
95 95
 	}
96 96
 
97
+	char lower_needle[n];
98
+	char lower_haystack[m];
99
+
100
+	for (int i = 0; i < n; i++)
101
+		lower_needle[i] = tolower(needle[i]);
102
+
103
+	for (int i = 0; i < m; i++)
104
+		lower_haystack[i] = tolower(haystack[i]);
105
+
97 106
 	score_t match_bonus[m];
98 107
 	score_t D[n][m], M[n][m];
99 108
 
... ...
@@ -108,7 +117,7 @@ score_t match_positions(const char *needle, const char *haystack, size_t *positi
108 117
 		score_t gap_score = i == n - 1 ? SCORE_GAP_TRAILING : SCORE_GAP_INNER;
109 118
 
110 119
 		for (int j = 0; j < m; j++) {
111
-			if (tolower(needle[i]) == tolower(haystack[j])) {
120
+			if (lower_needle[i] == lower_haystack[j]) {
112 121
 				score_t score = SCORE_MIN;
113 122
 				if (!i) {
114 123
 					score = (j * SCORE_GAP_LEADING) + match_bonus[j];