From c20e8f6880d398dc31759ebc3293586e23905b15 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 25 Oct 2019 15:05:34 -0600 Subject: [PATCH] perf: Speed up detection of text files We reduce how much of the buffer we walk twice which should speed up large files. We still load the entire file into memory which will still hurt binary files. This is part of #34. --- typos/src/checks.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/typos/src/checks.rs b/typos/src/checks.rs index 618ab2f..f6bc4c2 100644 --- a/typos/src/checks.rs +++ b/typos/src/checks.rs @@ -121,7 +121,8 @@ impl<'d, 'p> Checks<'d, 'p> { } let buffer = std::fs::read(path)?; - if !explicit && !self.binary && buffer.find_byte(b'\0').is_some() { + let null_max = std::cmp::min(buffer.len(), 1024); + if !explicit && !self.binary && buffer[0..null_max].find_byte(b'\0').is_some() { let msg = report::BinaryFile { path, non_exhaustive: (),