procfile optimizations
Costa Tsaousis (ktsaou) committed
Feb 17, 2018 at 21:31 UTC
0f05ff52413b445545e84c8aabd4280c9245a1a2
2 files changed
+44
-41
src/apps_plugin.c
+1
-1
@@ -3588,7 +3588,7 @@ int main(int argc, char **argv) {
3588
#warning "compiling for profiling"
3589
static int profiling_count=0;
3590
profiling_count++;
3591
- if(unlikely(profiling_count > 1000)) exit(0);
3591
+ if(unlikely(profiling_count > 2000)) exit(0);
3592
usec_t dt = update_every * USEC_PER_SEC;
3593
#else
3594
usec_t dt = heartbeat_next(&hb, step);
src/procfile.c
+43
-40
@@ -139,7 +139,25 @@ void procfile_close(procfile *ff) {
139
freez(ff);
140
}
141
142
-static inline void procfile_parser(procfile *ff) {
142
+__attribute__((noinline))
143
+static void _procfile_parser_add_word(procfile *ff, size_t line, char *begin, char *end) {
144
+ *end = '\0';
145
+ ff->words = pfwords_add(ff->words, begin);
146
+ ff->lines->lines[line].words++;
147
+}
148
+
149
+__attribute__((noinline))
150
+static char *_procfile_parser_skip_word(char *begin, char *end, PF_CHAR_TYPE *ct, PF_CHAR_TYPE *separators) {
151
+ PF_CHAR_TYPE c = PF_CHAR_IS_NEWLINE;
152
+
153
+ while(begin < end && (c = separators[(unsigned char)(*begin)]) == PF_CHAR_IS_WORD)
154
+ begin++;
155
+
156
+ *ct = c;
157
+ return begin;
158
+}
159
+
160
+static void procfile_parser(procfile *ff) {
161
// debug(D_PROCFILE, PF_PREFIX ": Parsing file '%s'", ff->filename);
162
163
char *s = ff->data // our current position
@@ -156,52 +174,46 @@ static inline void procfile_parser(procfile *ff) {
174
, w = 0 // counts the number of words we added
175
, opened = 0; // counts the number of open parenthesis
176
177
+ PF_CHAR_TYPE ct;
178
+
179
ff->lines = pflines_add(ff->lines, w);
180
161
- while(likely(s < e)) {
181
+ while((s = _procfile_parser_skip_word(s, e, &ct, separators)) < e) {
182
// we are not at the end
163
- PF_CHAR_TYPE ct = separators[(unsigned char)(*s)];
183
184
// this is faster than a switch()
185
if(likely(ct == PF_CHAR_IS_WORD)) {
186
s++;
187
}
188
else if(likely(ct == PF_CHAR_IS_SEPARATOR)) {
170
- if(unlikely(quote || opened)) {
189
+ if(likely(!quote && !opened)) {
190
+ if (likely(s != t)) {
191
+ // ending separator
192
+ _procfile_parser_add_word(ff, l, t, s);
193
+ w++;
194
+ t = ++s;
195
+ }
196
+ else {
197
+ // starting separator
198
+ // skip it
199
+ t = ++s;
200
+ }
201
+ }
202
+ else {
203
// we are inside a quote
204
s++;
173
- continue;
205
}
175
-
176
- if(unlikely(s == t)) {
177
- // skip all leading white spaces
178
- t = ++s;
179
- continue;
180
- }
181
-
182
- // end of word
183
- *s = '\0';
184
-
185
- ff->words = pfwords_add(ff->words, t);
186
- ff->lines->lines[l].words++;
187
- w++;
188
-
189
- t = ++s;
206
}
207
else if(likely(ct == PF_CHAR_IS_NEWLINE)) {
208
// end of line
193
- *s = '\0';
194
-
195
- ff->words = pfwords_add(ff->words, t);
196
- ff->lines->lines[l].words++;
209
+ _procfile_parser_add_word(ff, l, t, s);
210
w++;
211
+ t = ++s;
212
213
// debug(D_PROCFILE, PF_PREFIX ": ended line %d with %d words", l, ff->lines->lines[l].words);
214
215
ff->lines = pflines_add(ff->lines, w);
216
l++;
203
-
204
- t = ++s;
217
}
218
else if(likely(ct == PF_CHAR_IS_QUOTE)) {
219
if(unlikely(!quote && s == t)) {
@@ -213,11 +225,8 @@ static inline void procfile_parser(procfile *ff) {
225
// quote closed
226
quote = 0;
227
216
- *s = '\0';
217
- ff->words = pfwords_add(ff->words, t);
218
- ff->lines->lines[l].words++;
228
+ _procfile_parser_add_word(ff, l, t, s);
229
w++;
220
-
230
t = ++s;
231
}
232
else
@@ -240,11 +249,8 @@ static inline void procfile_parser(procfile *ff) {
249
opened--;
250
251
if(!opened) {
243
- *s = '\0';
244
- ff->words = pfwords_add(ff->words, t);
245
- ff->lines->lines[l].words++;
252
+ _procfile_parser_add_word(ff, l, t, s);
253
w++;
247
-
254
t = ++s;
255
}
256
else
@@ -259,15 +265,12 @@ static inline void procfile_parser(procfile *ff) {
265
266
if(likely(s > t && t < e)) {
267
// the last word
262
- if(likely(ff->len < ff->size))
263
- *s = '\0';
264
- else {
268
+ if(unlikely(ff->len >= ff->size)) {
269
// we are going to loose the last byte
266
- ff->data[ff->size - 1] = '\0';
270
+ s = &ff->data[ff->size - 1];
271
}
272
269
- ff->words = pfwords_add(ff->words, t);
270
- ff->lines->lines[l].words++;
273
+ _procfile_parser_add_word(ff, l, t, s);
274
}
275
}
276