Skip to content

Commit 4f71262

Browse files
committed
Break up automodeling into batches
1 parent 6791ddb commit 4f71262

File tree

4 files changed

+49
-41
lines changed

4 files changed

+49
-41
lines changed

extensions/ql-vscode/src/data-extensions-editor/auto-model-v2.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ import { ExternalApiUsage, MethodSignature } from "./external-api-usage";
88
import { ModeledMethod } from "./modeled-method";
99
import { groupMethods, sortGroupNames, sortMethods } from "./shared/sorting";
1010

11-
// Soft limit on the number of candidates to send to the model.
12-
// Note that the model may return fewer than this number of candidates.
13-
const candidateLimit = 20;
1411
/**
1512
* Return the candidates that the model should be run on. This includes limiting the number of
1613
* candidates to the candidate limit and filtering out anything that is already modeled and respecting
@@ -41,11 +38,6 @@ export function getCandidates(
4138
type: "none",
4239
};
4340

44-
// If we have reached the max number of candidates then stop
45-
if (candidates.length >= candidateLimit) {
46-
break;
47-
}
48-
4941
// Anything that is modeled is not a candidate
5042
if (modeledMethod.type !== "none") {
5143
continue;

extensions/ql-vscode/src/data-extensions-editor/auto-modeler.ts

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,21 @@ import { QueryRunner } from "../query-server";
1616
import { DatabaseItem } from "../databases/local-databases";
1717
import { Mode } from "./shared/mode";
1818

19+
// Limit the number of candidates we send to the model in each request
20+
// to avoid long requests.
21+
// Note that the model may return fewer than this number of candidates.
22+
const candidateBatchSize = 20;
23+
1924
export class AutoModeler {
2025
constructor(
2126
private readonly app: App,
2227
private readonly cliServer: CodeQLCliServer,
2328
private readonly queryRunner: QueryRunner,
2429
private readonly queryStorageDir: string,
2530
private readonly databaseItem: DatabaseItem,
31+
private readonly setInProgressMethods: (
32+
inProgressMethods: string[],
33+
) => Promise<void>,
2634
private readonly addModeledMethods: (
2735
modeledMethods: Record<string, ModeledMethod>,
2836
) => Promise<void>,
@@ -52,26 +60,52 @@ export class AutoModeler {
5260
await withProgress(async (progress) => {
5361
const maxStep = 3000;
5462

55-
progress({
56-
step: 0,
57-
maxStep,
58-
message: "Retrieving usages",
59-
});
60-
6163
// Fetch the candidates to send to the model
62-
const candidateMethods = getCandidates(
64+
const allCandidateMethods = getCandidates(
6365
mode,
6466
externalApiUsages,
6567
modeledMethods,
6668
);
6769

6870
// If there are no candidates, there is nothing to model and we just return
69-
if (candidateMethods.length === 0) {
71+
if (allCandidateMethods.length === 0) {
7072
void extLogger.log("No candidates to model. Stopping.");
7173
return;
7274
}
7375

74-
await this.modelCandidates(candidateMethods, mode, progress, maxStep);
76+
// Find number of slices to make
77+
const batchNumber = Math.ceil(
78+
allCandidateMethods.length / candidateBatchSize,
79+
);
80+
try {
81+
for (let i = 0; i < batchNumber; i++) {
82+
const start = i * candidateBatchSize;
83+
const end = start + candidateBatchSize;
84+
const candidatesToProcess = allCandidateMethods.slice(start, end);
85+
86+
await this.setInProgressMethods(
87+
candidatesToProcess.map((c) => c.signature),
88+
);
89+
90+
progress({
91+
step: 1800 + i * 100,
92+
maxStep,
93+
message: `Automodeling candidates, batch ${
94+
i + 1
95+
} of ${batchNumber}`,
96+
});
97+
98+
await this.modelCandidates(
99+
candidatesToProcess,
100+
mode,
101+
progress,
102+
maxStep,
103+
);
104+
}
105+
} finally {
106+
// Clear out in progress methods
107+
await this.setInProgressMethods([]);
108+
}
75109
});
76110
}
77111

extensions/ql-vscode/src/data-extensions-editor/data-extensions-editor-view.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ export class DataExtensionsEditorView extends AbstractWebview<
8282
queryRunner,
8383
queryStorageDir,
8484
databaseItem,
85+
async (inProgressMethods) => {
86+
await this.postMessage({
87+
t: "setInProgressMethods",
88+
inProgressMethods,
89+
});
90+
},
8591
async (modeledMethods) => {
8692
await this.postMessage({ t: "addModeledMethods", modeledMethods });
8793
},

extensions/ql-vscode/test/unit-tests/data-extensions-editor/auto-model-v2.test.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -165,28 +165,4 @@ describe("getCandidates", () => {
165165
);
166166
expect(candidates.length).toEqual(1);
167167
});
168-
169-
it("respects the limit", () => {
170-
const externalApiUsages: ExternalApiUsage[] = [];
171-
for (let i = 0; i < 30; i++) {
172-
externalApiUsages.push({
173-
library: "my.jar",
174-
signature: `org.my.A#x${i}()`,
175-
packageName: "org.my",
176-
typeName: "A",
177-
methodName: `x${i}`,
178-
methodParameters: "()",
179-
supported: false,
180-
supportedType: "none",
181-
usages: [],
182-
});
183-
}
184-
const modeledMethods = {};
185-
const candidates = getCandidates(
186-
Mode.Application,
187-
externalApiUsages,
188-
modeledMethods,
189-
);
190-
expect(candidates.length).toEqual(20);
191-
});
192168
});

0 commit comments

Comments
 (0)