The problem grep is solving
Suppose you search a 2 GB log for one word. A naive program would read the file one line at a time, split each line into characters, and compare the pattern at every position. Each step costs a little, and a few billion small costs add up to minutes. GNU grep is written to avoid nearly all of that work. It is a useful case study because each speed-up is a general idea that you can apply in your own code.
The starting point is the operating system. Reading a file through a large buffer costs far less than reading it through many small calls, so grep asks the kernel for big chunks of data at a time. The cost of a system call is paid once per chunk, not once per line.
Search the buffer, not each line
Rather than handing one line at a time to the matcher, grep works on the buffer. It searches the whole chunk for the pattern, and only when it finds a candidate does it work out the line boundaries around that position. Most of a typical log contains no match, so most of the bytes are skipped in a single scan. The line structure is computed only where it is needed.
You can see the effect in the behavior of simple options. With -q, grep stops at the first match, so it never reads the rest of a file that already answered the question. That early exit is why grep -q in an if is cheap even on huge inputs.
Fixed strings: a skip table
When the pattern is a fixed string, grep can use an algorithm based on a skip table, in the family of Boyer-Moore searches. The matcher compares the last character of the pattern first. When it sees a mismatch, it can jump ahead by a distance taken from a table built from the pattern, rather than moving one position at a time. For long words, most positions are skipped without being examined. This is why grep -F on a plain word is often faster than a regex that means the same thing.
GNU grep keeps this logic in a module for keyword sets, which also handles several fixed strings at once. You can feel the difference by comparing a word search with the same search written as a regular expression on a large file.
Regular expressions: a compiled state machine
For a true regex, grep cannot skip characters in the same way. Instead it compiles the pattern into a deterministic finite automaton, sometimes called a DFA. The automaton is a table of states. Reading one byte moves the matcher to the next state with a single table lookup, so the cost per byte is constant and small. Building the table is the expensive part, and it happens once before the search starts.
Lazy construction helps further. States are built as they are first reached, so the matcher does not pay for combinations of characters that never appear in the input. For most patterns the search cost stays close to the input length, which is far more predictable than backtracking engines, although a DFA can use a lot of memory for unusual patterns.
Backreferences such as \(a\)\1 cannot be expressed as a plain automaton. When a pattern uses them, grep falls back to a slower matcher, which is why they are noticeably slower than other regexes.
Literal prefilters
A clever shortcut is to look for a literal part of a pattern first. In a pattern such as error.*timeout, the word error is a required substring, so the search can use the fast fixed-string matcher to find lines that contain error, and only run the full automaton on those. Lines without the literal are rejected without any regex work. This is one of the main reasons that a pattern with a clear literal core is fast.
Avoiding decoding work
In a UTF-8 locale, the characters in a file have variable width, and matching must respect character boundaries. grep therefore checks whether the input is plain ASCII. For ASCII data the byte and character views are the same, and grep takes faster paths. Setting LC_ALL=C makes that explicit, which is a practical speed-up for large searches on data you know is ASCII. The trade-off is that character classes then follow bytes, not letters in other scripts.
LC_ALL=C grep -c ERROR app.logOn ASCII input this prints the same count as the default run, and it is the safe choice when speed matters. The lab input here is tiny, so you will not see a timing difference, but the habit is useful on big files.
What to take from it
The lessons generalize beyond grep. Read in large blocks. Skip work for data that cannot match. Compile a pattern once and reuse the compiled form. Stop as soon as the answer is known. When you write your own pipeline, these choices decide whether a tool takes a second or a minute, and when a command feels slow, the first question to ask is which of these steps it is doing.