[PATCH] D85538: [Clang options] Optimize optionMatches() runtime by removing mallocs

Nadav Rotem via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 7 10:17:32 PDT 2020


nadav created this revision.
nadav added a reviewer: modocache.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.
nadav requested review of this revision.

The method optionMatches() constructs 9865 std::string instances when comparing different
options. Many of these instances exceed the size of the internal storage and force memory
allocations. This patch adds an early exit check that eliminates almost most of the string allocations
while keeping the code simple.

Example inputs: 
Prefix: /, Name: Fr
Prefix: -, Name: Fr
Prefix: -, Name: fsanitize-address-field-padding=
Prefix: -, Name: fsanitize-address-globals-dead-stripping
Prefix: -, Name: fsanitize-address-poison-custom-array-cookie
Prefix: -, Name: fsanitize-address-use-after-scope
Prefix: -, Name: fsanitize-address-use-odr-indicator
Prefix: -, Name: fsanitize-blacklist=


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D85538

Files:
  llvm/lib/Option/OptTable.cpp


Index: llvm/lib/Option/OptTable.cpp
===================================================================
--- llvm/lib/Option/OptTable.cpp
+++ llvm/lib/Option/OptTable.cpp
@@ -198,8 +198,9 @@
 static bool optionMatches(const OptTable::Info &In, StringRef Option) {
   if (In.Prefixes)
     for (size_t I = 0; In.Prefixes[I]; I++)
-      if (Option == std::string(In.Prefixes[I]) + In.Name)
-        return true;
+      if (Option.endswith(In.Name))
+        if (Option == std::string(In.Prefixes[I]) + In.Name)
+          return true;
   return false;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D85538.283941.patch
Type: text/x-patch
Size: 555 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200807/d89fcedf/attachment.bin>


More information about the llvm-commits mailing list