Skip to content

Commit a7016b3

Browse files
committed
Merge remote-tracking branch 'origin/distance-tester-dispatch-flatten' into develop
2 parents 000c2db + 9b5950d commit a7016b3

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

include/timbl/Testers.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ namespace Timbl{
9292
double ) override;
9393
private:
9494
std::vector<metricTestFunction*> metricTest;
95+
// Per-feature test info precomputed once, in permuted order, so the inner
96+
// test loop avoids the permutation indirection and, for plain Overlap
97+
// features (the common case), the virtual metricTestFunction / fvDistance
98+
// / metric->distance() chain -- which for Overlap only computes
99+
// (F==G ? 0 : weight).
100+
std::vector<metricTestFunction*> permTest; // metricTest in permuted order
101+
std::vector<char> isOverlap; // 1 if feature uses Overlap
95102
};
96103

97104
class SimilarityTester: public TesterClass {

src/Testers.cxx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,17 @@ namespace Timbl{
152152
metricTest[i] = new overlapTestFunction();
153153
}
154154
}
155+
// Precompute, in permuted order, the metric test function and an Overlap
156+
// flag for each feature, so test() can use a flat index and take a direct
157+
// path for Overlap features.
158+
permTest.resize(_size,0);
159+
isOverlap.resize(_size,0);
160+
for ( size_t j=0; j < _size; ++j ){
161+
Feature *feat = permFeatures[j];
162+
permTest[j] = metricTest[permutation[j]];
163+
isOverlap[j] = ( feat && !feat->Ignore()
164+
&& feat->getMetricType() == Overlap ) ? 1 : 0;
165+
}
155166
}
156167

157168
size_t DistanceTester::test( const vector<FeatureValue *>& G,
@@ -164,9 +175,15 @@ namespace Timbl{
164175
cerr << "feature " << TrueF << " (perm=" << permutation[TrueF]
165176
<< ")" << endl;
166177
#endif
167-
double result = metricTest[permutation[TrueF]]->test( (*FV)[TrueF],
168-
G[i],
169-
permFeatures[TrueF] );
178+
double result;
179+
if ( isOverlap[TrueF] ){
180+
// plain Overlap: distance is 0 for an exact value match, otherwise
181+
// the feature weight -- no virtual metric dispatch needed.
182+
result = ( (*FV)[TrueF] == G[i] ) ? 0.0 : permFeatures[TrueF]->Weight();
183+
}
184+
else {
185+
result = permTest[TrueF]->test( (*FV)[TrueF], G[i], permFeatures[TrueF] );
186+
}
170187
distances[i+1] = distances[i] + result;
171188
if ( distances[i+1] > Threshold ){
172189
#ifdef DBGTEST

0 commit comments

Comments
 (0)