<div class="gmail_quote"><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Date: Tue, 19 Oct 2010 19:39:10 -0000<br>
From: Douglas Gregor <<a href="mailto:dgregor@apple.com">dgregor@apple.com</a>><br>
Subject: [cfe-commits] r116849 - /cfe/trunk/lib/Sema/SemaLookup.cpp<br>
To: <a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
Message-ID: <<a href="mailto:20101019193911.0338D2A6C12C@llvm.org">20101019193911.0338D2A6C12C@llvm.org</a>><br>
Content-Type: text/plain; charset="utf-8"<br>
<br>
Author: dgregor<br>
Date: Tue Oct 19 14:39:10 2010<br>
New Revision: 116849<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=116849&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=116849&view=rev</a><br>
Log:<br>
Improve the performance of typo correction, by using a simple<br>
computation to compute the lower bound of the edit distance, so that<br>
we can avoid computing the edit distance for names that will clearly<br>
be rejected later. Since edit distance is such an expensive algorithm<br>
(M x N), this leads to a 7.5x speedup when correcting NSstring -><br>
NSString in the presence of a Cocoa PCH.<br>
<br>
Modified:<br>
    cfe/trunk/lib/Sema/SemaLookup.cpp<br>
<br>
Modified: cfe/trunk/lib/Sema/SemaLookup.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaLookup.cpp?rev=116849&r1=116848&r2=116849&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaLookup.cpp?rev=116849&r1=116848&r2=116849&view=diff</a><br>

==============================================================================<br>
--- cfe/trunk/lib/Sema/SemaLookup.cpp (original)<br>
+++ cfe/trunk/lib/Sema/SemaLookup.cpp Tue Oct 19 14:39:10 2010<br>
@@ -2730,6 +2730,12 @@<br>
 }<br>
<br>
 void TypoCorrectionConsumer::FoundName(llvm::StringRef Name) {<br>
+  // Use a simple length-based heuristic to determine the minimum possible<br>
+  // edit distance. If the minimum isn't good enough, bail out early.<br>
+  unsigned MinED = abs((int)Name.size() - (int)Typo.size());<br>
+  if (MinED > BestEditDistance || (MinED && Typo.size() / MinED < 3))<br>
+    return;<br>
+<br>
   // Compute the edit distance between the typo and the name of this<br>
   // entity. If this edit distance is not worse than the best edit<br>
   // distance we've seen so far, add it to the list of results.<br>
<br></blockquote></div>Hi Doug,<br><br>another simple optimization could be to count the number of occurences of each character in both names, then add the absolute difference for each character. If the sum of absolute differences is superior to the best edit distance so far, then no combination of addition / deletion / substitution (in this limit) can morph one string to another.<br>
<br>I've not measured it though, so it might slow down the general case.<br><br>I was also wondering if this optimization would not be better suited in `StringRef::edit_distance` method ? (so that all users may benefit from it)<br>
<br>Thanks for your work :)<br>Matthieu<br><br>