More trivial optimizations to a function well outside the critical path

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@92896 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Douglas Gregor 2010-01-07 02:24:06 +00:00
parent 4039bd0550
commit 2772ea831c

View File

@ -8,7 +8,6 @@
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringRef.h"
#include <cstring>
using namespace llvm; using namespace llvm;
@ -52,21 +51,18 @@ unsigned StringRef::edit_distance(llvm::StringRef Other,
size_type m = size(); size_type m = size();
size_type n = Other.size(); size_type n = Other.size();
unsigned SmallPrevious[32]; const unsigned SmallBufferSize = 64;
unsigned SmallCurrent[32]; unsigned SmallBuffer[SmallBufferSize];
unsigned *Allocated = 0;
unsigned *previous = SmallPrevious; unsigned *previous = SmallBuffer;
unsigned *current = SmallCurrent; if (2*(n + 1) > SmallBufferSize)
if (n + 1 > 32) { Allocated = previous = new unsigned [2*(n+1)];
previous = new unsigned [n+1]; unsigned *current = previous + (n + 1);
current = new unsigned [n+1];
}
for (unsigned i = 0; i <= n; ++i) for (unsigned i = 0; i <= n; ++i)
previous[i] = i; previous[i] = i;
for (size_type y = 1; y <= m; ++y) { for (size_type y = 1; y <= m; ++y) {
std::memset(current, 0, (n + 1) * sizeof(unsigned));
current[0] = y; current[0] = y;
for (size_type x = 1; x <= n; ++x) { for (size_type x = 1; x <= n; ++x) {
if (AllowReplacements) { if (AllowReplacements) {
@ -85,10 +81,7 @@ unsigned StringRef::edit_distance(llvm::StringRef Other,
} }
unsigned Result = previous[n]; unsigned Result = previous[n];
if (n + 1 > 32) { delete [] Allocated;
delete [] previous;
delete [] current;
}
return Result; return Result;
} }