Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dexsearch optimization #10536

Merged
merged 15 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions server/chat-plugins/datasearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1104,8 +1104,23 @@ function runDexsearch(target: string, cmd: string, canAll: boolean, message: str
Object.values(search).reduce(accumulateKeyCount, 0)
));

// Prepare move validator and pokemonSource outside the hot loop
// but don't prepare them at all if there are no moves to check...
// These only ever get accessed if there are moves to filter by.
let validator;
let pokemonSource;
if (Object.values(searches).some(search => Object.keys(search.moves).length !== 0)) {
const format = Object.entries(Dex.data.Rulesets).find(([a, f]) => f.mod === usedMod)?.[1].name || 'gen9ou';
larry-the-table-guy marked this conversation as resolved.
Show resolved Hide resolved
const ruleTable = Dex.formats.getRuleTable(Dex.formats.get(format));
const additionalRules = [];
if (nationalSearch && !ruleTable.has('standardnatdex')) additionalRules.push('standardnatdex');
if (nationalSearch && ruleTable.valueRules.has('minsourcegen')) additionalRules.push('!!minsourcegen=3');
validator = TeamValidator.get(`${format}${additionalRules.length ? `@@@${additionalRules.join(',')}` : ''}`);
pokemonSource = validator.allSources();
}
for (const alts of searches) {
if (alts.skip) continue;
const altsMoves = Object.keys(alts.moves).map(x => mod.moves.get(x)).filter(move => move.gen <= mod.gen);
for (const mon in dex) {
let matched = false;
if (alts.gens && Object.keys(alts.gens).length) {
Expand Down Expand Up @@ -1266,20 +1281,12 @@ function runDexsearch(target: string, cmd: string, canAll: boolean, message: str
}
if (matched) continue;

const format = Object.entries(Dex.data.Rulesets).find(([a, f]) => f.mod === usedMod);
const formatStr = format ? format[1].name : 'gen9ou';
const ruleTable = Dex.formats.getRuleTable(Dex.formats.get(formatStr));
const additionalRules = [];
if (nationalSearch && !ruleTable.has('standardnatdex')) additionalRules.push('standardnatdex');
if (nationalSearch && ruleTable.valueRules.has('minsourcegen')) additionalRules.push('!!minsourcegen=3');
const validator = TeamValidator.get(`${formatStr}${additionalRules.length ? `@@@${additionalRules.join(',')}` : ''}`);
const pokemonSource = validator.allSources();
for (const move of Object.keys(alts.moves).map(x => mod.moves.get(x))) {
if (move.gen <= mod.gen && !validator.checkCanLearn(move, dex[mon], pokemonSource) === alts.moves[move.id]) {
for (const move of altsMoves) {
if (validator && !validator.checkCanLearn(move, dex[mon], pokemonSource) === alts.moves[move.id]) {
matched = true;
break;
}
if (!pokemonSource.size()) break;
if (pokemonSource && !pokemonSource.size()) break;
}
if (matched) continue;

Expand Down
6 changes: 4 additions & 2 deletions sim/team-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2391,7 +2391,7 @@ export class TeamValidator {
const baseSpecies = dex.species.get(originalSpecies);

const format = this.format;
const ruleTable = dex.formats.getRuleTable(format);
const ruleTable = this.ruleTable;
const level = set.level || 100;

let cantLearnReason = null;
Expand Down Expand Up @@ -2693,7 +2693,9 @@ export class TeamValidator {
if (!setSources.restrictiveMoves) {
setSources.restrictiveMoves = [];
}
setSources.restrictiveMoves.push(move.name);
if (!setSources.restrictiveMoves.includes(move.name)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to change restrictiveMoves from an array to a Set. The lengths I've observed are 1 - 40, heavily weighted towards the low end. Linear scan on a small array isn't too bad, and sometimes better than a hashset. I haven't checked all the usages to see if switching to a Set is reasonable, and can't be sure that there isn't some scenario that grows restrictiveMoves to hundreds or thousands of elements.

setSources.restrictiveMoves.push(move.name);
}

const checkedSpecies = babyOnly ? fullLearnset[fullLearnset.length - 1].species : baseSpecies;
if (checkedSpecies && setSources.isFromPokemonGo &&
Expand Down
Loading