Skip to content

Commit

Permalink
optimize(Packages): optimize reading large files
Browse files Browse the repository at this point in the history
Using less syscalls and processing data in memory
makes getNumStringsImpl ~300% faster.

The code is much simpler.
  • Loading branch information
apocelipes authored and CarterLi committed Dec 7, 2023
1 parent a9c5c28 commit 1504e26
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions src/detection/packages/packages_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,19 @@ static uint32_t getNumElements(FFstrbuf* baseDir, const char* dirname, unsigned

static uint32_t getNumStringsImpl(const char* filename, const char* needle)
{
FILE* file = fopen(filename, "r");
if(file == NULL)
FF_STRBUF_AUTO_DESTROY content = ffStrbufCreate();
if (!ffReadFileBuffer(filename, &content))
return 0;

uint32_t count = 0;

char* line = NULL;
size_t len = 0;

while(getline(&line, &len, file) != EOF)
char *iter = content.chars;
size_t needleLength = strlen(needle);
while ((iter = memmem(iter, content.length - (size_t)(iter - content.chars), needle, needleLength)) != NULL)
{
if(strstr(line, needle) != NULL)
++count;
++count;
iter += needleLength;
}

if(line != NULL)
free(line);

fclose(file);

return count;
}

Expand Down

0 comments on commit 1504e26

Please sign in to comment.