Skip to content

Commit 6778daf

Browse files
committed
Implement sorted levenshtein distance checking
Based on sidsethupathi@c9e32ba
1 parent 398a5e9 commit 6778daf

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

scripts/update_benchmarks.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import fs from 'fs';
77
// Application
88
import { BLOCKLISTED_GPUS } from '../src/internal/blocklistedGPUS';
99
import { getGPUVersion } from '../src/internal/getGPUVersion';
10+
import { tokenizeForLevenshteinDistance } from '../src/internal/getLevenshteinDistance';
1011
import { internalBenchmarkResults } from './internalBenchmarkResults';
1112
import { BenchmarkRow } from './types';
1213

@@ -120,6 +121,7 @@ type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
120121
({ gpu, gpuVersion, blocklisted, devices }) => [
121122
gpu,
122123
gpuVersion,
124+
tokenizeForLevenshteinDistance(gpu),
123125
blocklisted ? 1 : 0,
124126
devices.map(({ width, height, fps, device }) =>
125127
isMobile ? [width, height, fps, device] : [width, height, fps]

src/index.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ import { BLOCKLISTED_GPUS } from './internal/blocklistedGPUS';
66
import { cleanRenderer } from './internal/cleanRenderer';
77
import { deobfuscateRenderer } from './internal/deobfuscateRenderer';
88
import { deviceInfo } from './internal/deviceInfo';
9-
import { getLevenshteinDistance } from './internal/getLevenshteinDistance';
9+
import { OutdatedBenchmarksError } from './internal/error';
1010
import { getGPUVersion } from './internal/getGPUVersion';
11+
import {
12+
getLevenshteinDistance,
13+
tokenizeForLevenshteinDistance
14+
} from './internal/getLevenshteinDistance';
1115
import { getWebGLContext } from './internal/getWebGLContext';
1216
import { isSSR } from './internal/ssr';
1317
import { isDefined } from './internal/util';
14-
import { OutdatedBenchmarksError } from './internal/error';
1518

1619
// Types
1720
export interface GetGPUTier {
@@ -87,7 +90,7 @@ export type TierResult = {
8790

8891
export type ModelEntryScreen = [number, number, number, string | undefined];
8992

90-
export type ModelEntry = [string, string, 0 | 1, ModelEntryScreen[]];
93+
export type ModelEntry = [string, string, string, 0 | 1, ModelEntryScreen[]];
9194

9295
const debug = false ? console.log : undefined;
9396

@@ -196,13 +199,20 @@ export const getGPUTier = async ({
196199
return;
197200
}
198201

202+
const tokenizedRenderer = tokenizeForLevenshteinDistance(renderer);
199203
// eslint-disable-next-line prefer-const
200-
let [gpu, , , fpsesByPixelCount] =
204+
let [gpu, , , , fpsesByPixelCount] =
201205
matchCount > 1
202206
? matched
203207
.map(
204208
(match) =>
205-
[match, getLevenshteinDistance(renderer, match[0])] as const
209+
[
210+
match,
211+
getLevenshteinDistance(
212+
tokenizedRenderer,
213+
match[2]
214+
),
215+
] as const
206216
)
207217
.sort(([, a], [, b]) => a - b)[0][0]
208218
: matched[0];
@@ -240,7 +250,7 @@ export const getGPUTier = async ({
240250
const [, , fps, device] = closest!;
241251

242252
return [minDistance, fps, gpu, device] as const;
243-
};
253+
}
244254

245255
const toResult = (
246256
tier: number,

src/internal/getLevenshteinDistance.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,12 @@ export function getLevenshteinDistance(left: string, right: string): number {
8383

8484
return result;
8585
}
86+
87+
export function tokenizeForLevenshteinDistance(str: string): string {
88+
return str
89+
.split(/[.,()\[\]/\s]/g)
90+
.sort()
91+
// Remove duplicates
92+
.filter((item, pos, arr) => pos === 0 || item !== arr[pos - 1])
93+
.join(' ');
94+
}

test/index.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ for (const { input, expected } of [
9292
},
9393
{
9494
expected: {
95-
gpu: 'nvidia geforce rtx 2080 ti rev. a',
95+
gpu: 'nvidia geforce rtx 2080 ti',
9696
},
9797
input: {
9898
isMobile: false,
@@ -168,7 +168,7 @@ for (const { input, expected } of [
168168
},
169169
{
170170
expected: {
171-
gpu: 'nvidia geforce gtx 750 ti',
171+
gpu: 'nvidia geforce gtx 750',
172172
},
173173
input: {
174174
isMobile: false,
@@ -231,7 +231,7 @@ for (const { input, expected } of [
231231
},
232232
{
233233
expected: {
234-
gpu: 'nvidia evga geforce gtx 970',
234+
gpu: 'nvidia geforce gtx 970',
235235
},
236236
input: {
237237
isMobile: false,

0 commit comments

Comments
 (0)