Skip to content

Commit

Permalink
Make is_binary a little faster.
Browse files Browse the repository at this point in the history
  • Loading branch information
ggreer committed Mar 20, 2012
1 parent 864b741 commit 7bdb00e
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/util.c
Expand Up @@ -84,6 +84,7 @@ int invert_matches(match matches[], int matches_len, const int buf_len) {
return(matches_len + 1);
}

/* This function is very hot. It's called on every file. */
int is_binary(const void* buf, const int buf_len) {
int suspicious_bytes = 0;
int total_bytes = buf_len > 1024 ? 1024 : buf_len;
Expand All @@ -101,6 +102,10 @@ int is_binary(const void* buf, const int buf_len) {
}
else if ((buf_c[i] < 7 || buf_c[i] > 14) && (buf_c[i] < 32 || buf_c[i] > 127)) {
suspicious_bytes++;
/* disk IO is so slow that it's worthwhile to do this calculation after every suspicious byte */
if ((suspicious_bytes * 100) / total_bytes > 10) {
return(1);
}
}
}

Expand Down

0 comments on commit 7bdb00e

Please sign in to comment.