Skip to content

Commit 1504e26

Browse files
apocelipesCarterLi
authored andcommitted
optimize(Packages): optimize reading large files
Using less syscalls and processing data in memory makes getNumStringsImpl ~300% faster. The code is much simpler.
1 parent a9c5c28 commit 1504e26

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

src/detection/packages/packages_linux.c

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,19 @@ static uint32_t getNumElements(FFstrbuf* baseDir, const char* dirname, unsigned
4242

4343
static uint32_t getNumStringsImpl(const char* filename, const char* needle)
4444
{
45-
FILE* file = fopen(filename, "r");
46-
if(file == NULL)
45+
FF_STRBUF_AUTO_DESTROY content = ffStrbufCreate();
46+
if (!ffReadFileBuffer(filename, &content))
4747
return 0;
4848

4949
uint32_t count = 0;
50-
51-
char* line = NULL;
52-
size_t len = 0;
53-
54-
while(getline(&line, &len, file) != EOF)
50+
char *iter = content.chars;
51+
size_t needleLength = strlen(needle);
52+
while ((iter = memmem(iter, content.length - (size_t)(iter - content.chars), needle, needleLength)) != NULL)
5553
{
56-
if(strstr(line, needle) != NULL)
57-
++count;
54+
++count;
55+
iter += needleLength;
5856
}
5957

60-
if(line != NULL)
61-
free(line);
62-
63-
fclose(file);
64-
6558
return count;
6659
}
6760

0 commit comments

Comments
 (0)