[PATCH] D156046: [Support] Rewrite GlobPattern

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 23 00:21:38 PDT 2023


MaskRay created this revision.
MaskRay added reviewers: andrewng, ellis, kyulee, phosek, rupprecht.
Herald added subscribers: hiraditya, kristof.beyls, krytarowski.
Herald added a project: All.
MaskRay requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

The current implementation has two primary issues:

- Matching `a*a*a*b` against `aaaaaa` has exponential complexity.
- BitVector harms data cache and is inefficient for literal matching.

Switch to an O(|S|*|P|) greedy algorithm instead: factor the pattern
into segments split by '*'. The segment is matched sequentianlly by
finding the first occurrence past the end of the previous match. This
algorithm is used by lots of fnmatch implementations, including musl and
NetBSD's.

In addition, `optional<StringRef> Exact, Suffix, Prefix` wastes space.
Instead, match the non-metacharacter prefix against the haystack, then
match the pattern with the rest.

In practice `*suffix` style patterns are less common and our new
algorithm is fast enough, so don't bother storing the non-metacharacter
suffix.

Note: brace expansions (D153587 <https://reviews.llvm.org/D153587>) can leverage the `matchOne` function.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156046

Files:
  llvm/include/llvm/Support/GlobPattern.h
  llvm/lib/Support/GlobPattern.cpp
  llvm/unittests/Support/GlobPatternTest.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156046.543258.patch
Type: text/x-patch
Size: 9481 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230723/5a9dd602/attachment.bin>


More information about the llvm-commits mailing list