[PATCH] D127070: [ADT][NFC] Early bail out for ComputeEditDistance

Nathan James via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 5 04:20:58 PDT 2022


njames93 created this revision.
njames93 added reviewers: dblaikie, chandlerc.
Herald added a project: All.
njames93 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

The minimun bound for number of edits is the size difference between the 2 arrays.
If MaxEditDistance is smaller than this, we can bail out early without needing to traverse any of the arrays.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D127070

Files:
  llvm/include/llvm/ADT/edit_distance.h


Index: llvm/include/llvm/ADT/edit_distance.h
===================================================================
--- llvm/include/llvm/ADT/edit_distance.h
+++ llvm/include/llvm/ADT/edit_distance.h
@@ -61,6 +61,15 @@
   typename ArrayRef<T>::size_type m = FromArray.size();
   typename ArrayRef<T>::size_type n = ToArray.size();
 
+  if (MaxEditDistance) {
+    // If the difference in size between the 2 arrays is larger than the max
+    // distance allowed, we can bail out as we will always need at least
+    // MaxEditDistance insertions or removals.
+    typename ArrayRef<T>::size_type AbsDiff = m > n ? m - n : n - m;
+    if (AbsDiff > MaxEditDistance)
+      return MaxEditDistance + 1;
+  }
+
   const unsigned SmallBufferSize = 64;
   unsigned SmallBuffer[SmallBufferSize];
   std::unique_ptr<unsigned[]> Allocated;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127070.434320.patch
Type: text/x-patch
Size: 828 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220605/3f0e21ac/attachment.bin>


More information about the llvm-commits mailing list