3 ms·
> What is grep going to do while it waits for data? Two things: - Search the data it’s already read in. If data is coming in fast enough, it’s better to read
by comex 2y ago
> What is grep going to do while it waits for data?
Two things:
- Search the data it’s already read in. If data is coming in fast enough, it’s better to read and search in parallel rather than alternating.
- If this is a recursive grep, then list, open, or read from additional files.
Even so, thread pools work fine for this kind of thing. An optimized grep already wants to use threads to split the CPU work into parallelizable chunks (where helpful), so using threads for syscalls too shouldn’t make much difference.
However, for recursive grep, you might need to open large numbers of small files, in which case syscall latency might be a big enough factor that something like io_uring would be significantly faster.
Disclaimer: I’m mostly thinking about tools like ripgrep that are only grep-like. I’m not aware of any actual grep implementations that use parallelism to the same extent. But there’s no reason a grep implementation couldn’t do that; it’s just that most grep implementations were written in the age of single-core processors. Also note that I don’t actually know much about ripgrep’s implementation, so this post is mostly speculative.
- whiterknight 2y ago> Search the data it’s already read in The kernel handles that. Your program works on the data that’s available while the pipe is filling up again. > If this is a recursive grep As you said. This is modeled best by multiple threads or processes, each navigating through their structure.
- swatcoder 2y ago> It’s just that most grep implementations were written... ...with a design philosophy of composition. Rather than a hundred tools that each try to make too-clever predictions about how to parallelize your work, the idea is to have small streamlined tools that you can compose into the optimal solution for your task. If you need parallelization, you can introduce that in the ways you need to using other small, streamlined tools that provide that. It had nothing to do with some prevalence of "single-core processors" and was simply just a different way of building things.
- dwattttt 2y agoThat just pushes the task of optimising the workload up to you, complete with opportunities to forget about it & do it badly. I don't relish the idea of splitting sections of a file up into N chunks and running N grep's in parallel, and would much rather that kind of "smarts" to be in the grep tool
- whiterknight 2y agoIt has no choice but to read file data in chunks or exhaust memory. If you need to do n parallel searches what better arrangement do you propose?
- dwattttt 2y agoI propose the search tool decide how to split up the region I want searched, rather than me trying to compose simpler tools to try to achieve the same result.