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

Data: Fix huge perf bugs in randbat tests, part 1 #10616

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

larry-the-table-guy
Copy link
Contributor

@larry-the-table-guy larry-the-table-guy commented Oct 13, 2024

A lot of time in npm run full-test is spent in randbats code.

Fixes

  • Cache frequently constructed pools in RandomTeams etc.
  • testSet and testTeam previously constructed a generator every round, just to change the seed.
  • getGenerator previously called toID multiple times on the same format (least significant change)

Times

added --v8-prof flag to mocha command for full-test in package.json.

npm run full-test

stage total ticks
baseline 313K
sizeLimit movepool 247K
setSeed generator 185K
toID format once 185K
revert sizeLimit, add caching to RandomTeams 170K
precompute status move list for just gen9 155K
precompute status move list for all 140K

npm run full-test, just ./test/random-battles/**/*.js

Baseline -> head: 223K -> 57K


old stats

Action: npm run full-test
Base commit: ca27f79

Reporting granularity:
randomNPokemon: 512 calls
getMovePool: 64K calls

Function old total time # calls new total time
randomNPokemon 87s 6,656 - 7,168 16s
getMovePool 82s 2,949,120 - 3,014,656 10s

2,841,615+ calls to getMovePool came from randomNPokemon.

console dump for pre times

unit: milliseconds.
each row is 512 or 64K calls.

getMovePool 1860.3069046624005
randomNPokemon 3554.415757998824
getMovePool 1874.9457025416195
getMovePool 1830.1477849967778
getMovePool 1858.6934965401888
getMovePool 1840.7724690623581
randomNPokemon 7989.962926980108
randomNPokemon 1305.8674189262092
randomNPokemon 786.0779669731855
randomNPokemon 290.5737610012293
randomNPokemon 430.87063897401094
randomNPokemon 559.6572600342333
getMovePool 1898.5179619304836
getMovePool 1801.655199110508
getMovePool 1797.9789628982544
getMovePool 1843.0572230406106
randomNPokemon 7623.162248980254
getMovePool 1826.8817325383425
getMovePool 1815.0325373522937
getMovePool 1806.841587126255
getMovePool 1795.539395660162
getMovePool 1834.562273029238
getMovePool 1830.4044391438365
getMovePool 1787.4613492004573
randomNPokemon 12839.203042011708
getMovePool 1790.8939850777388
getMovePool 1795.4548127576709
getMovePool 1793.9000857174397
getMovePool 1828.0785682573915
getMovePool 1813.2182871960104
getMovePool 1820.6180104464293
getMovePool 1809.1440160237253
randomNPokemon 12796.57172903046
getMovePool 1809.9712127484381
getMovePool 1826.1999765187502
getMovePool 1820.1126325018704
getMovePool 1805.590091649443
getMovePool 1798.000423643738
getMovePool 1803.545040115714
getMovePool 1797.041264232248
randomNPokemon 12797.750146027654
getMovePool 1795.8108561635017
getMovePool 1814.0274211503565
getMovePool 1863.8975707776845
getMovePool 1880.2090921513736
getMovePool 1877.8822002001107
getMovePool 1836.0198221765459
randomNPokemon 13071.448024019599
getMovePool 1852.7785603702068
getMovePool 1819.9413214027882
getMovePool 1828.3486699350178
getMovePool 1799.6376006640494
getMovePool 1801.5483566522598
getMovePool 1924.642497483641
getMovePool 1852.5289650075138
randomNPokemon 13009.844617016613
getMovePool 1822.669201798737
getMovePool 1862.5755200907588

Something to check later in randomNPokemon:
Array .includes called on 1000 element arrays of ints. On the order of a billion elements are visited during npm run full-test. Int ops are pretty fast, so probably not an issue. But there's potential for a speed-up here.

size of pool in `randomNPokemon` when calling `.includes`

length : frequency

{
  '0': 2300,
  '1': 3400,
  '2': 4500,
  '3': 5800,
  '4': 2300,
  '5': 3400,
  '6': 5900,
  '7': 2300,
  '8': 2300,
  '9': 4700,
  '10': 3400,
  '11': 2300,
  '12': 3500,
  '13': 3400,
  '14': 2300,
  '15': 3500,
  '16': 2300,
  '17': 2300,
  '18': 3500,
  '19': 3500,
  '20': 5800,
  '21': 3400,
  '22': 3400,
  '23': 2300,
  '24': 4500,
  '25': 19100,
  '26': 3500,
  '27': 4600,
  '28': 3500,
  '29': 2300,
  '30': 2300,
  '31': 2300,
  '32': 2300,
  '33': 2300,
  '34': 2300,
  '35': 2300,
  '36': 2300,
  '37': 3500,
  '38': 3500,
  '39': 2300,
  '40': 2300,
  '41': 2300,
  '42': 3400,
  '43': 2300,
  '44': 2300,
  '45': 2300,
  '46': 3400,
  '47': 3400,
  '48': 3400,
  '49': 2300,
  '50': 3500,
  '51': 4600,
  '52': 5900,
  '53': 3500,
  '54': 2300,
  '55': 2300,
  '56': 2300,
  '57': 2300,
  '58': 3500,
  '59': 3500,
  '60': 2300,
  '61': 3400,
  '62': 3400,
  '63': 2300,
  '64': 2300,
  '65': 3500,
  '66': 3400,
  '67': 3400,
  '68': 4600,
  '69': 3400,
  '70': 3400,
  '71': 2300,
  '72': 5600,
  '73': 2300,
  '74': 3500,
  '75': 3500,
  '76': 3500,
  '77': 4600,
  '78': 4600,
  '79': 3500,
  '80': 4700,
  '81': 2300,
  '82': 2300,
  '83': 3500,
  '84': 2300,
  '85': 2300,
  '86': 2300,
  '87': 2300,
  '88': 3500,
  '89': 3500,
  '90': 2300,
  '91': 2300,
  '92': 2300,
  '93': 2300,
  '94': 4700,
  '95': 2300,
  '96': 2300,
  '97': 3400,
  '98': 3400,
  '99': 3500,
  '100': 3500,
  '101': 4600,
  '102': 2300,
  '103': 3500,
  '104': 2300,
  '105': 5800,
  '106': 2300,
  '107': 2300,
  '108': 2300,
  '109': 3400,
  '110': 3500,
  '111': 3400,
  '112': 2300,
  '113': 2300,
  '114': 2300,
  '115': 3500,
  '116': 2300,
  '117': 2300,
  '118': 2300,
  '119': 2300,
  '120': 3400,
  '121': 3400,
  '122': 3500,
  '123': 2300,
  '124': 3400,
  '125': 4500,
  '126': 3400,
  '127': 4600,
  '128': 10300,
  '129': 2300,
  '130': 3500,
  '131': 5700,
  '132': 2300,
  '133': 3500,
  '134': 2300,
  '135': 3400,
  '136': 2300,
  '137': 2300,
  '138': 2300,
  '139': 4500,
  '140': 3400,
  '141': 3400,
  '142': 3500,
  '143': 4600,
  '144': 3500,
  '145': 3500,
  '146': 3500,
  '147': 2300,
  '148': 2300,
  '149': 21000,
  '150': 4700,
  '151': 2300,
  '152': 2300,
  '153': 2300,
  '154': 2300,
  '155': 2300,
  '156': 2300,
  '157': 3500,
  '158': 2300,
  '159': 2300,
  '160': 2300,
  '161': 2300,
  '162': 2300,
  '163': 2300,
  '164': 5600,
  '165': 3400,
  '166': 2300,
  '167': 2300,
  '168': 2300,
  '169': 2300,
  '170': 2300,
  '171': 2300,
  '172': 2300,
  '173': 2300,
  '174': 2300,
  '175': 2300,
  '176': 2300,
  '177': 2300,
  '178': 3400,
  '179': 3400,
  '180': 3400,
  '181': 4600,
  '182': 3400,
  '183': 2300,
  '184': 2300,
  '185': 2300,
  '186': 2300,
  '187': 3400,
  '188': 2300,
  '189': 3400,
  '190': 3400,
  '191': 3400,
  '192': 2300,
  '193': 2300,
  '194': 3500,
  '195': 2300,
  '196': 2300,
  '197': 2300,
  '198': 2300,
  '199': 3500,
  '200': 2300,
  '201': 2300,
  '202': 2300,
  '203': 2300,
  '204': 2300,
  '205': 2300,
  '206': 2300,
  '207': 2300,
  '208': 3500,
  '209': 2300,
  '210': 2300,
  '211': 3500,
  '212': 3500,
  '213': 2300,
  '214': 3500,
  '215': 3500,
  '216': 2300,
  '217': 2300,
  '218': 2300,
  '219': 2300,
  '220': 2300,
  '221': 2300,
  '222': 3500,
  '223': 2300,
  '224': 2300,
  '225': 2300,
  '226': 2300,
  '227': 2300,
  '228': 2300,
  '229': 11200,
  '230': 2300,
  '231': 2300,
  '232': 2300,
  '233': 2300,
  '234': 2300,
  '235': 2300,
  '236': 3400,
  '237': 1200,
  '238': 1200,
  '239': 1200,
  '240': 1200,
  '241': 1200,
  '242': 1200,
  '243': 1200,
  '244': 1200,
  '245': 1200,
  '246': 1200,
  '247': 1200,
  '248': 2400,
  '249': 1200,
  '250': 1200,
  '251': 1200,
  '252': 1200,
  '253': 1200,
  '254': 2400,
  '255': 1200,
  '256': 1200,
  '257': 2400,
  '258': 1200,
  '259': 1200,
  '260': 2400,
  '261': 1200,
  '262': 1200,
  '263': 2400,
  '264': 2400,
  '265': 1200,
  '266': 1200,
  '267': 1200,
  '268': 1200,
  '269': 1200,
  '270': 1200,
  '271': 1200,
  '272': 1200,
  '273': 1200,
  '274': 1200,
  '275': 1200,
  '276': 1200,
  '277': 1200,
  '278': 1200,
  '279': 1200,
  '280': 1200,
  '281': 1200,
  '282': 2400,
  '283': 1200,
  '284': 1200,
  '285': 1200,
  '286': 1200,
  '287': 1200,
  '288': 1200,
  '289': 1200,
  '290': 1200,
  '291': 1200,
  '292': 1200,
  '293': 1200,
  '294': 1200,
  '295': 1200,
  '296': 1200,
  '297': 1200,
  '298': 1200,
  '299': 1200,
  '300': 1200,
  '301': 1200,
  '302': 2400,
  '303': 2400,
  '304': 1200,
  '305': 1200,
  '306': 2400,
  '307': 1200,
  '308': 2400,
  '309': 1200,
  '310': 2400,
  '311': 1200,
  '312': 1200,
  '313': 1200,
  '314': 1200,
  '315': 1200,
  '316': 1200,
  '317': 1200,
  '318': 1200,
  '319': 2400,
  '320': 1200,
  '321': 1200,
  '322': 1200,
  '323': 2400,
  '324': 1200,
  '325': 1200,
  '326': 1200,
  '327': 1200,
  '328': 1200,
  '329': 1200,
  '330': 1200,
  '331': 1200,
  '332': 1200,
  '333': 1200,
  '334': 2400,
  '335': 1200,
  '336': 1200,
  '337': 1200,
  '338': 1200,
  '339': 1200,
  '340': 1200,
  '341': 1200,
  '342': 1200,
  '343': 1200,
  '344': 1200,
  '345': 1200,
  '346': 1200,
  '347': 1200,
  '348': 1200,
  '349': 1200,
  '350': 1200,
  '351': 4800,
  '352': 1200,
  '353': 1200,
  '354': 2400,
  '355': 1200,
  '356': 1200,
  '357': 1200,
  '358': 1200,
  '359': 2400,
  '360': 1200,
  '361': 1200,
  '362': 2400,
  '363': 1200,
  '364': 1200,
  '365': 1200,
  '366': 1200,
  '367': 1200,
  '368': 1200,
  '369': 1200,
  '370': 1200,
  '371': 1200,
  '372': 1200,
  '373': 2400,
  '374': 1200,
  '375': 1200,
  '376': 2400,
  '377': 1200,
  '378': 1200,
  '379': 1200,
  '380': 2400,
  '381': 2400,
  '382': 2400,
  '383': 2400,
  '384': 2400,
  '385': 1200,
  '386': 4800,
  '387': 1200,
  '388': 1200,
  '389': 1200,
  '390': 1200,
  '391': 1200,
  '392': 1200,
  '393': 1200,
  '394': 1200,
  '395': 1200,
  '396': 1200,
  '397': 1200,
  '398': 1200,
  '399': 1200,
  '400': 1200,
  '401': 1200,
  '402': 1200,
  '403': 1200,
  '404': 1200,
  '405': 1200,
  '406': 1200,
  '407': 1200,
  '408': 1200,
  '409': 1200,
  '410': 1200,
  '411': 1200,
  '412': 1200,
  '413': 3600,
  '414': 1200,
  '415': 1200,
  '416': 1200,
  '417': 1200,
  '418': 1200,
  '419': 1200,
  '420': 1200,
  '421': 2400,
  '422': 1200,
  '423': 1200,
  '424': 1200,
  '425': 1200,
  '426': 1200,
  '427': 1200,
  '428': 2400,
  '429': 1200,
  '430': 1200,
  '431': 1200,
  '432': 1200,
  '433': 1200,
  '434': 1200,
  '435': 1200,
  '436': 1200,
  '437': 1200,
  '438': 1200,
  '439': 1200,
  '440': 1200,
  '441': 1200,
  '442': 1200,
  '443': 1200,
  '444': 1200,
  '445': 2400,
  '446': 1200,
  '447': 1200,
  '448': 2400,
  '449': 1200,
  '450': 1200,
  '451': 1200,
  '452': 1200,
  '453': 1200,
  '454': 1200,
  '455': 1200,
  '456': 1200,
  '457': 1200,
  '458': 1200,
  '459': 1200,
  '460': 2400,
  '461': 1200,
  '462': 1200,
  '463': 1200,
  '464': 1200,
  '465': 1200,
  '466': 1200,
  '467': 1200,
  '468': 1200,
  '469': 1200,
  '470': 1200,
  '471': 1200,
  '472': 1200,
  '473': 1200,
  '474': 1200,
  '475': 2400,
  '476': 1200,
  '477': 1200,
  '478': 1200,
  '479': 7200,
  '480': 1200,
  '481': 1200,
  '482': 1200,
  '483': 2400,
  '484': 2400,
  '485': 1200,
  '486': 1200,
  '487': 2400,
  '488': 1200,
  '489': 1200,
  '490': 1200,
  '491': 1200,
  '492': 2400,
  '493': 21600,
  '494': 1200,
  '495': 1200,
  '496': 1200,
  '497': 1200,
  '498': 1200,
  '499': 1200,
  '500': 1200,
  '501': 1200,
  '502': 1200,
  '503': 2400,
  '504': 1200,
  '505': 1200,
  '506': 1200,
  '507': 1200,
  '508': 1200,
  '509': 1200,
  '510': 1200,
  '511': 1200,
  '512': 1200,
  '513': 1200,
  '514': 1200,
  '515': 1200,
  '516': 1200,
  '517': 1200,
  '518': 1200,
  '519': 1200,
  '520': 1200,
  '521': 1200,
  '522': 1200,
  '523': 1200,
  '524': 1200,
  '525': 1200,
  '526': 1200,
  '527': 1200,
  '528': 1200,
  '529': 1200,
  '530': 1200,
  '531': 2400,
  '532': 1200,
  '533': 1200,
  '534': 1200,
  '535': 1200,
  '536': 1200,
  '537': 1200,
  '538': 1200,
  '539': 1200,
  '540': 1200,
  '541': 1200,
  '542': 1200,
  '543': 1200,
  '544': 1200,
  '545': 1200,
  '546': 1200,
  '547': 1200,
  '548': 1200,
  '549': 2400,
  '550': 3600,
  '551': 1200,
  '552': 1200,
  '553': 1200,
  '554': 2400,
  '555': 4800,
  '556': 1200,
  '557': 1200,
  '558': 1200,
  '559': 1200,
  '560': 1200,
  '561': 1200,
  '562': 2400,
  '563': 1200,
  '564': 1200,
  '565': 1200,
  '566': 1200,
  '567': 1200,
  '568': 1200,
  '569': 2400,
  '570': 2400,
  '571': 2400,
  '572': 1200,
  '573': 1200,
  '574': 1200,
  '575': 1200,
  '576': 1200,
  '577': 1200,
  '578': 1200,
  '579': 1200,
  '580': 1200,
  '581': 1200,
  '582': 1200,
  '583': 1200,
  '584': 1200,
  '585': 1200,
  '586': 1200,
  '587': 1200,
  '588': 1200,
  '589': 1200,
  '590': 1200,
  '591': 1200,
  '592': 1200,
  '593': 1200,
  '594': 1200,
  '595': 1200,
  '596': 1200,
  '597': 1200,
  '598': 1200,
  '599': 1200,
  '600': 1200,
  '601': 1200,
  '602': 1200,
  '603': 1200,
  '604': 1200,
  '605': 1200,
  '606': 1200,
  '607': 1200,
  '608': 1200,
  '609': 1200,
  '610': 1200,
  '611': 1200,
  '612': 1200,
  '613': 1200,
  '614': 1200,
  '615': 1200,
  '616': 1200,
  '617': 1200,
  '618': 2400,
  '619': 1200,
  '620': 1200,
  '621': 1200,
  '622': 1200,
  '623': 1200,
  '624': 1200,
  '625': 1200,
  '626': 1200,
  '627': 1200,
  '628': 2400,
  '629': 1200,
  '630': 1200,
  '631': 1200,
  '632': 1200,
  '633': 1200,
  '634': 1200,
  '635': 1200,
  '636': 1200,
  '637': 1200,
  '638': 1200,
  '639': 1200,
  '640': 1200,
  '641': 2400,
  '642': 2400,
  '643': 1200,
  '644': 1200,
  '645': 2400,
  '646': 3600,
  '647': 2400,
  '648': 2400,
  '649': 6000,
  '650': 1200,
  '651': 1200,
  '652': 1200,
  '653': 1200,
  '654': 1200,
  '655': 1200,
  '656': 1200,
  '657': 1200,
  '658': 3600,
  '659': 1200,
  '660': 1200,
  '661': 1200,
  '662': 1200,
  '663': 1200,
  '664': 1200,
  '665': 1200,
  '666': 3600,
  '667': 1200,
  '668': 1200,
  '669': 1200,
  '670': 2400,
  '671': 1200,
  '672': 1200,
  '673': 1200,
  '674': 1200,
  '675': 1200,
  '676': 1200,
  '677': 1200,
  '678': 2400,
  '679': 1200,
  '680': 1200,
  '681': 2400,
  '682': 1200,
  '683': 1200,
  '684': 1200,
  '685': 1200,
  '686': 1200,
  '687': 1200,
  '688': 1200,
  '689': 1200,
  '690': 1200,
  '691': 1200,
  '692': 1200,
  '693': 1200,
  '694': 1200,
  '695': 1200,
  '696': 1200,
  '697': 1200,
  '698': 1200,
  '699': 1200,
  '700': 1200,
  '701': 1200,
  '702': 1200,
  '703': 1200,
  '704': 1200,
  '705': 2400,
  '706': 2400,
  '707': 1200,
  '708': 1200,
  '709': 1200,
  '710': 4800,
  '711': 4800,
  '712': 1200,
  '713': 2400,
  '714': 1200,
  '715': 1200,
  '716': 1200,
  '717': 1200,
  '718': 3600,
  '719': 2400,
  '720': 2400,
  '721': 1200,
  '722': 1200,
  '723': 1200,
  '724': 2400,
  '725': 1200,
  '726': 1200,
  '727': 1200,
  '728': 1200,
  '729': 1200,
  '730': 1200,
  '731': 1200,
  '732': 1200,
  '733': 1200,
  '734': 1200,
  '735': 2400,
  '736': 1200,
  '737': 1200,
  '738': 2400,
  '739': 1200,
  '740': 1200,
  '741': 4800,
  '742': 1200,
  '743': 2400,
  '744': 1200,
  '745': 3600,
  '746': 2400,
  '747': 1200,
  '748': 1200,
  '749': 1200,
  '750': 1200,
  '751': 1200,
  '752': 2400,
  '753': 1200,
  '754': 2400,
  '755': 1200,
  '756': 1200,
  '757': 1200,
  '758': 2400,
  '759': 1200,
  '760': 1200,
  '761': 1200,
  '762': 1200,
  '763': 1200,
  '764': 1200,
  '765': 1200,
  '766': 1200,
  '767': 1200,
  '768': 1200,
  '769': 1200,
  '770': 1200,
  '771': 1200,
  '772': 1200,
  '773': 21600,
  '774': 2400,
  '775': 1200,
  '776': 1200,
  '777': 2400,
  '778': 4800,
  '779': 1200,
  '780': 1200,
  '781': 1200,
  '782': 1200,
  '783': 1200,
  '784': 2400,
  '785': 1200,
  '786': 1200,
  '787': 1200,
  '788': 1200,
  '789': 1200,
  '790': 1200,
  '791': 1200,
  '792': 1200,
  '793': 1200,
  '794': 1200,
  '795': 1200,
  '796': 1200,
  '797': 1200,
  '798': 1200,
  '799': 1200,
  '800': 4800,
  '801': 2400,
  '802': 1200,
  '803': 1200,
  '804': 1200,
  '805': 1200,
  '806': 1200,
  '807': 1200,
  '808': 1200,
  '809': 2400,
  '810': 1200,
  '811': 1200,
  '812': 2400,
  '813': 1200,
  '814': 1200,
  '815': 2400,
  '816': 1200,
  '817': 1200,
  '818': 2400,
  '819': 1200,
  '820': 1200,
  '821': 1200,
  '822': 1200,
  '823': 2400,
  '824': 1200,
  '825': 1200,
  '826': 2400,
  '827': 1200,
  '828': 1200,
  '829': 1200,
  '830': 1200,
  '831': 1200,
  '832': 1200,
  '833': 1200,
  '834': 2400,
  '835': 1200,
  '836': 1200,
  '837': 1200,
  '838': 1200,
  '839': 2400,
  '840': 1200,
  '841': 2400,
  '842': 2400,
  '843': 1200,
  '844': 2400,
  '845': 3600,
  '846': 1200,
  '847': 1200,
  '848': 1200,
  '849': 4800,
  '850': 1200,
  '851': 2400,
  '852': 1200,
  '853': 1200,
  '854': 2400,
  '855': 2400,
  '856': 1200,
  '857': 1200,
  '858': 2400,
  '859': 1200,
  '860': 1200,
  '861': 2400,
  '862': 1200,
  '863': 1200,
  '864': 1200,
  '865': 1200,
  '866': 1200,
  '867': 1200,
  '868': 1200,
  '869': 2400,
  '870': 1200,
  '871': 1200,
  '872': 1200,
  '873': 1200,
  '874': 1200,
  '875': 2400,
  '876': 2400,
  '877': 2400,
  '878': 1200,
  '879': 2400,
  '880': 1200,
  '881': 1200,
  '882': 1200,
  '883': 1200,
  '884': 2400,
  '885': 1200,
  '886': 1200,
  '887': 1200,
  '888': 2400,
  '889': 2400,
  '890': 2400,
  '891': 1200,
  '892': 4800,
  '893': 2400,
  '894': 1200,
  '895': 1200,
  '896': 1200,
  '897': 1200,
  '898': 3600,
  '899': 1200,
  '900': 1200,
  '901': 2400,
  '902': 2400,
  '903': 1200,
  '904': 1200,
  '905': 2400,
  '906': 1200,
  '907': 1200,
  '908': 1200,
  '909': 1200,
  '910': 1200,
  '911': 1200,
  '912': 1200,
  '913': 1200,
  '914': 1200,
  '915': 1200,
  '916': 2400,
  '917': 1200,
  '918': 1200,
  '919': 1200,
  '920': 1200,
  '921': 1200,
  '922': 1200,
  '923': 1200,
  '924': 1200,
  '925': 2400,
  '926': 1200,
  '927': 1200,
  '928': 1200,
  '929': 1200,
  '930': 1200,
  '931': 4800,
  '932': 1200,
  '933': 1200,
  '934': 1200,
  '935': 1200,
  '936': 1200,
  '937': 1200,
  '938': 1200,
  '939': 1200,
  '940': 1200,
  '941': 1200,
  '942': 1200,
  '943': 1200,
  '944': 1200,
  '945': 1200,
  '946': 1200,
  '947': 1200,
  '948': 1200,
  '949': 1200,
  '950': 1200,
  '951': 1200,
  '952': 1200,
  '953': 1200,
  '954': 1200,
  '955': 1200,
  '956': 1200,
  '957': 1200,
  '958': 1200,
  '959': 1200,
  '960': 1200,
  '961': 1200,
  '962': 1200,
  '963': 1200,
  '964': 2400,
  '965': 1200,
  '966': 1200,
  '967': 1200,
  '968': 1200,
  '969': 1200,
  '970': 1200,
  '971': 1200,
  '972': 1200,
  '973': 1200,
  '974': 1200,
  '975': 1200,
  '976': 1200,
  '977': 1200,
  '978': 1200,
  '979': 1200,
  '980': 1200,
  '981': 1200,
  '982': 2400,
  '983': 1200,
  '984': 1200,
  '985': 1200,
  '986': 1200,
  '987': 1200,
  '988': 1200,
  '989': 1200,
  '990': 1200,
  '991': 1200,
  '992': 1200,
  '993': 1200,
  '994': 1200,
  '995': 1200,
  '996': 1200,
  '997': 1200,
  '998': 1200,
  '999': 2400,
  '1000': 1200,
  '1001': 1200,
  '1002': 1200,
  '1003': 1200,
  '1004': 1200,
  '1005': 1200,
  '1006': 1200,
  '1007': 1200,
  '1008': 1200,
  '1009': 1200,
  '1010': 1200,
  '1011': 1200,
  '1012': 2400,
  '1013': 2400,
  '1014': 1200,
  '1015': 1200,
  '1016': 1200,
  '1017': 9600,
  '1018': 1200,
  '1019': 1200,
  '1020': 1200,
  '1021': 1200,
  '1022': 1200,
  '1023': 1200,
  '1024': 3600
}

@larry-the-table-guy larry-the-table-guy changed the title Data: Stopgap perf bug fix in Data: Stopgap perf bug fix in randomNPokemon Oct 13, 2024
@Slayer95
Copy link
Contributor

Slayer95 commented Oct 13, 2024

Awesome. By the way, I have been wondering whether switching randomNPokemon to use reservoir sampling would be valuable. This could be implemented as a Dex.species method that accepts a PRNG, a count, and optionally a filter.

@larry-the-table-guy
Copy link
Contributor Author

I haven't looked too hard into the algorithms being used here.

For randbats, it seems movesets are the next priority.
There's a couple parts of the test suite I'll look at next, then I can get back to normal optimizations (like toID etc) which help when slowness is smeared across the application.

Here's the new v8 prof for npm run full-test
 [Bottom up (heavy) profile]:
  Note: percentage shows a share of a particular caller in the total
  amount of its parent calls.
  Callers occupying less than 1.0% are not shown.

   ticks parent  name
  195007   82.3%  /opt/node-v18.20.4-linux-x64/bin/node
  92589   47.5%    /opt/node-v18.20.4-linux-x64/bin/node
  18795   20.3%      /opt/node-v18.20.4-linux-x64/bin/node
   6592   35.1%        LazyCompile: *cullMovePool redacted/dist/data/random-battles/gen9/teams.js:484:15
   4909   74.5%          LazyCompile: *addMove redacted/dist/data/random-battles/gen9/teams.js:688:10
   4542   92.5%            LazyCompile: *randomMoveset redacted/dist/data/random-battles/gen9/teams.js:731:16
    367    7.5%            Function: ^randomMoveset redacted/dist/data/random-battles/gen9/teams.js:731:16
   1312   19.9%          LazyCompile: *randomMoveset redacted/dist/data/random-battles/gen9/teams.js:731:16
   1197   91.2%            LazyCompile: *randomSet redacted/dist/data/random-battles/gen9/teams.js:1609:12
     97    7.4%            Function: ^<anonymous> redacted/test/random-battles/all-gens.js:232:71
     18    1.4%            Function: ^randomSet redacted/dist/data/random-battles/gen9/teams.js:1609:12
    252    3.8%          Function: ^addMove redacted/dist/data/random-battles/gen9/teams.js:688:10
    165   65.5%            LazyCompile: *randomMoveset redacted/dist/data/random-battles/gen9/teams.js:731:16
     87   34.5%            Function: ^randomMoveset redacted/dist/data/random-battles/gen9/teams.js:731:16
    119    1.8%          Function: ^randomMoveset redacted/dist/data/random-battles/gen9/teams.js:731:16
    103   86.6%            LazyCompile: *randomSet redacted/dist/data/random-battles/gen9/teams.js:1609:12
     14   11.8%            Function: ^<anonymous> redacted/test/random-battles/all-gens.js:232:71
      2    1.7%            Function: ^randomSet redacted/dist/data/random-battles/gen9/teams.js:1609:12
   1229    6.5%        LazyCompile: *cullMovePool redacted/dist/data/random-battles/gen6/teams.js:204:15
    966   78.6%          LazyCompile: *addMove redacted/dist/data/random-battles/gen7/teams.js:507:10
    788   81.6%            LazyCompile: *randomMoveset redacted/dist/data/random-battles/gen6/teams.js:371:16
    178   18.4%            Function: ^randomMoveset redacted/dist/data/random-battles/gen6/teams.js:371:16
    204   16.6%          LazyCompile: *randomMoveset redacted/dist/data/random-battles/gen6/teams.js:371:16
    146   71.6%            LazyCompile: *randomSet redacted/dist/data/random-battles/gen6/teams.js:982:12
     42   20.6%            Function: ^<anonymous> redacted/test/random-battles/all-gens.js:232:71
     16    7.8%            Function: ^randomSet redacted/dist/data/random-battles/gen6/teams.js:982:12
     59    4.8%          Function: ^randomMoveset redacted/dist/data/random-battles/gen6/teams.js:371:16
     41   69.5%            Function: ^<anonymous> redacted/test/random-battles/all-gens.js:232:71
     18   30.5%            LazyCompile: *randomSet redacted/dist/data/random-battles/gen6/teams.js:982:12
   1101    5.9%        LazyCompile: *cullMovePool redacted/dist/data/random-battles/gen7/teams.js:313:15
    824   74.8%          LazyCompile: *addMove redacted/dist/data/random-battles/gen7/teams.js:507:10
    581   70.5%            LazyCompile: *randomMoveset redacted/dist/data/random-battles/gen7/teams.js:545:16
    243   29.5%            Function: ^randomMoveset redacted/dist/data/random-battles/gen7/teams.js:545:16
    189   17.2%          LazyCompile: *randomMoveset redacted/dist/data/random-battles/gen7/teams.js:545:16
    112   59.3%            LazyCompile: *randomSet redacted/dist/data/random-battles/gen7/teams.js:1242:12
     43   22.8%            LazyCompile: *<anonymous> redacted/test/random-battles/all-gens.js:232:71
     34   18.0%            Function: ^randomSet redacted/dist/data/random-battles/gen7/teams.js:1242:12
     79    7.2%          Function: ^randomMoveset redacted/dist/data/random-battles/gen7/teams.js:545:16
     30   38.0%            LazyCompile: *<anonymous> redacted/test/random-battles/all-gens.js:232:71
     25   31.6%            Function: ^randomSet redacted/dist/data/random-battles/gen7/teams.js:1242:12
     24   30.4%            LazyCompile: *randomSet redacted/dist/data/random-battles/gen7/teams.js:1242:12
*SNIP* buncha small child nodes
  12466   13.5%      LazyCompile: *findPokemonEventHandlers redacted/dist/sim/battle.js:755:27
  12342   99.0%        LazyCompile: *findEventHandlers redacted/dist/sim/battle.js:707:20
  11646   94.4%          LazyCompile: *runEvent redacted/dist/sim/battle.js:526:11
   1198   10.3%            LazyCompile: *useMoveInner redacted/dist/sim/battle-actions.js:352:15
   1061    9.1%            LazyCompile: *getStat redacted/dist/sim/pokemon.js:307:10
    904    7.8%            LazyCompile: *runMove redacted/dist/sim/battle-actions.js:220:10
    903    7.8%            LazyCompile: *endTurn redacted/dist/sim/battle.js:1228:10
    664    5.7%            LazyCompile: *getDamage redacted/dist/sim/battle-actions.js:1449:12
    418    3.6%            LazyCompile: *modifyDamage redacted/dist/sim/battle-actions.js:1555:15
    351    3.0%            LazyCompile: *runSwitch redacted/dist/sim/battle-actions.js:175:12
    351    3.0%            LazyCompile: *hitStepAccuracy redacted/dist/sim/battle-actions.js:625:18
    339    2.9%            LazyCompile: *switchIn redacted/dist/sim/battle-actions.js:78:11
    284    2.4%            LazyCompile: *getActionSpeed redacted/dist/sim/battle.js:2099:17
    277    2.4%            LazyCompile: *getImmunity redacted/dist/sim/dex.js:212:14
    243    2.1%            LazyCompile: *spreadMoveHit redacted/dist/sim/battle-actions.js:963:16
    224    1.9%            LazyCompile: *resolveAction redacted/dist/sim/battle-queue.js:72:16
    216    1.9%            LazyCompile: *boost redacted/dist/sim/battle.js:1553:8
    209    1.8%            LazyCompile: *trySpreadMoveHit redacted/dist/sim/battle-actions.js:498:19
    201    1.7%            LazyCompile: *runMoveEffects redacted/dist/sim/battle-actions.js:1083:17
    191    1.6%            LazyCompile: *spreadDamage redacted/dist/sim/battle.js:1628:15
    189    1.6%            LazyCompile: *getMoveRequestData redacted/dist/sim/pokemon.js:725:21
    183    1.6%            LazyCompile: *hitStepInvulnerabilityEvent redacted/dist/sim/battle-actions.js:556:30
    157    1.3%            LazyCompile: *eachEvent redacted/dist/sim/battle.js:298:12
    152    1.3%            LazyCompile: *hitStepTypeImmunity redacted/dist/sim/battle-actions.js:592:22
    121    1.0%            Function: ^useMoveInner redacted/dist/sim/battle-actions.js:352:15
    119    1.0%            Function: ^runMove redacted/dist/sim/battle-actions.js:220:10
    614    5.0%          LazyCompile: *findEventHandlers redacted/dist/sim/battle.js:707:20
    614  100.0%            LazyCompile: *runEvent redacted/dist/sim/battle.js:526:11
   5774    6.2%      LazyCompile: *cullMovePool redacted/dist/data/random-battles/gen9/teams.js:484:15
   4262   73.8%        LazyCompile: *addMove redacted/dist/data/random-battles/gen9/teams.js:688:10
   3942   92.5%          LazyCompile: *randomMoveset redacted/dist/data/random-battles/gen9/teams.js:731:16
   3629   92.1%            LazyCompile: *randomSet redacted/dist/data/random-battles/gen9/teams.js:1609:12
    246    6.2%            Function: ^<anonymous> redacted/test/random-battles/all-gens.js:232:71
     67    1.7%            Function: ^randomSet redacted/dist/data/random-battles/gen9/teams.js:1609:12
    320    7.5%          Function: ^randomMoveset redacted/dist/data/random-battles/gen9/teams.js:731:16
    259   80.9%            LazyCompile: *randomSet redacted/dist/data/random-battles/gen9/teams.js:1609:12
     53   16.6%            Function: ^<anonymous> redacted/test/random-battles/all-gens.js:232:71
      8    2.5%            Function: ^randomSet redacted/dist/data/random-battles/gen9/teams.js:1609:12
   1165   20.2%        LazyCompile: *randomMoveset redacted/dist/data/random-battles/gen9/teams.js:731:16
   1062   91.2%          LazyCompile: *randomSet redacted/dist/data/random-battles/gen9/teams.js:1609:12
   1011   95.2%            LazyCompile: *randomTeam redacted/dist/data/random-battles/gen9/teams.js:1742:13
     37    3.5%            Function: ^randomTeam redacted/dist/data/random-battles/gen9/teams.js:1742:13
     14    1.3%            LazyCompile: *testSet redacted/test/random-battles/tools.js:20:17
     84    7.2%          Function: ^<anonymous> redacted/test/random-battles/all-gens.js:232:71
     84  100.0%            Function: ^callFn redacted/node_modules/mocha/lib/runnable.js:365:18
     19    1.6%          Function: ^randomSet redacted/dist/data/random-battles/gen9/teams.js:1609:12
     15   78.9%            Function: ^randomTeam redacted/dist/data/random-battles/gen9/teams.js:1742:13
      4   21.1%            LazyCompile: *randomTeam redacted/dist/data/random-battles/gen9/teams.js:1742:13
   3521    3.8%      Function: ^<anonymous> redacted/test/random-battles/all-gens.js:299:61
   3521  100.0%        Function: ^callFn redacted/node_modules/mocha/lib/runnable.js:365:18
   3521  100.0%          Function: ^Runnable.run redacted/node_modules/mocha/lib/runnable.js:270:34
   3521  100.0%            Function: ^Runner.runTest redacted/node_modules/mocha/lib/runner.js:658:36
   3351    3.6%      LazyCompile: *Format redacted/dist/sim/dex-formats.js:325:14
   3351  100.0%        LazyCompile: *get redacted/dist/sim/dex-formats.js:472:6
   2187   65.3%          LazyCompile: *getRuleTable redacted/dist/sim/dex-formats.js:520:15
   2085   95.3%            LazyCompile: *getRuleTable redacted/dist/sim/dex-formats.js:520:15
     49    2.2%            LazyCompile: *randomTeam redacted/dist/data/random-battles/gen8/teams.js:2082:13
     27    1.2%            LazyCompile: *TeamValidator redacted/dist/sim/team-validator.js:231:14
   1048   31.3%          LazyCompile: *validateRule redacted/dist/sim/dex-formats.js:711:15
   1036   98.9%            LazyCompile: *getRuleTable redacted/dist/sim/dex-formats.js:520:15
   3086    3.3%      LazyCompile: *assertSetValidity redacted/test/random-battles/tools.js:123:27
   2947   95.5%        LazyCompile: *<anonymous> redacted/test/random-battles/all-gens.js:286:34
   2754   93.5%          LazyCompile: *testTeam redacted/test/random-battles/tools.js:107:18
   2194   79.7%            Function: ^<anonymous> redacted/test/random-battles/all-gens.js:282:56
    560   20.3%            LazyCompile: ~<anonymous> redacted/test/random-battles/all-gens.js:282:56
    193    6.5%          Function: ^testTeam redacted/test/random-battles/tools.js:107:18
    193  100.0%            LazyCompile: ~<anonymous> redacted/test/random-battles/all-gens.js:282:56
    100    3.2%        Function: ^<anonymous> redacted/test/random-battles/all-gens.js:286:34
    100  100.0%          Function: ^testTeam redacted/test/random-battles/tools.js:107:18
    100  100.0%            LazyCompile: ~<anonymous> redacted/test/random-battles/all-gens.js:282:56
     39    1.3%        LazyCompile: ~<anonymous> redacted/test/random-battles/all-gens.js:286:34
     39  100.0%          Function: ^testTeam redacted/test/random-battles/tools.js:107:18
     39  100.0%            LazyCompile: ~<anonymous> redacted/test/random-battles/all-gens.js:282:56
   1983    2.1%      LazyCompile: *get redacted/dist/sim/dex-formats.js:472:6
   1090   55.0%        LazyCompile: *getRuleTable redacted/dist/sim/dex-formats.js:520:15
   1053   96.6%          LazyCompile: *getRuleTable redacted/dist/sim/dex-formats.js:520:15
    723   68.7%            LazyCompile: *getRuleTable redacted/dist/sim/dex-formats.js:520:15
    279   26.5%            LazyCompile: *RandomGen8Teams redacted/dist/data/random-battles/gen8/teams.js:137:14
     18    1.7%            LazyCompile: *randomTeam redacted/dist/data/random-battles/gen8/teams.js:2082:13
     14    1.3%            LazyCompile: *TeamValidator redacted/dist/sim/team-validator.js:231:14
    781   39.4%        LazyCompile: *validateRule redacted/dist/sim/dex-formats.js:711:15
    778   99.6%          LazyCompile: *getRuleTable redacted/dist/sim/dex-formats.js:520:15
    755   97.0%            LazyCompile: *getRuleTable redacted/dist/sim/dex-formats.js:520:15
     30    1.5%        LazyCompile: *validateLearnset redacted/test/random-battles/tools.js:178:26
     24   80.0%          LazyCompile: *<anonymous> redacted/test/random-battles/all-gens.js:299:61
     24  100.0%            Function: ^callFn redacted/node_modules/mocha/lib/runnable.js:365:18
      5   16.7%          Function: ^<anonymous> redacted/test/random-battles/all-gens.js:299:61
      5  100.0%            Function: ^callFn redacted/node_modules/mocha/lib/runnable.js:365:18
      1    3.3%          Function: ^<anonymous> redacted/test/random-battles/all-gens.js:167:53
      1  100.0%            Function: ^callFn redacted/node_modules/mocha/lib/runnable.js:365:18

I wish I could get flamegraphs working on Node but I keep getting garbage output :/.

Previously computed whole move pool just to see whether it's empty. It accounts for about 25% of the test suite runtime.
@larry-the-table-guy larry-the-table-guy force-pushed the data/randomNPokemon-movepool-perf-bug branch from e2f57ef to 2843aa4 Compare October 16, 2024 01:25
@larry-the-table-guy larry-the-table-guy changed the title Data: Stopgap perf bug fix in randomNPokemon Data: Fix huge perf bugs in randbat tests, part 1 Oct 16, 2024
@larry-the-table-guy larry-the-table-guy marked this pull request as draft October 18, 2024 23:49
@larry-the-table-guy
Copy link
Contributor Author

Got some more stats for ./data/random-battles/gen9/teams.ts
Time is in milliseconds

Function # calls^ total time avg time sub1time sub1desc sub2time sub2desc
randomTeam 11000 25263 2.30 22797 line 1635 (while loop) to function end 1222 in while loop, "increment our counters" to end of loop
randomSet 73000 20451 0.28 20625 "Get moves" to end 490 after randomMoveset and queryMoves
randomMoveset 49000 20336 0.415 6326 "choose remaining moves randomly from movepool" 573 up to "Enforce STAB priority"
randomCCTeam 3000 9316 3.11 793 after randomNPokemon call 9364 just randomNPokemon call
randomNPokemon 6000 12162 2.03 339 after populating speciesPool 8628 just 'isNotCustom' branch body

^My counters don't include calls with early returns (because those don't tend to be the expensive paths).
sub1 and sub2 may overlap.

queryMoves was measured but not significant.

Context:
randomSet is called by randomTeam.
randomMoveSet is called by randomSet.
randomNPokemon is called by randomCCTeam.

So, the hotspots are randomMoveSet and randomNPokemon.
I suspect getMovePool is still slow. I'll look into caching MovePool in DexSpecies - not a huge fan of that since it could be 50+MB if every species in every mod loads it. I guess in practice most won't be, but it seems better to consider max usage for a long-lived process like a server.


I tried replacing moves: Set<string> with moves: string[] since it's only up to 4 items long, but it wasn't measurably any better.

@larry-the-table-guy
Copy link
Contributor Author

@Slayer95 I've now read more of ./data/random-battles/gen9/teams.ts, and I don't think the sampling is the slow part. I think it's unfortunately much messier.

  • There's a ton of work being duplicated across iterations in the form of allocations, computing movepools, etc. For ex: gen9/teams.ts:queryMoves gets called 370k times, and always constructs a new Set and Utils.MultiSet.
  • Countless filters and heuristics that search arrays of strings. With int IDs, possibly in a bitset, these could be much cheaper.

Death by a thousand cuts, basically.


I think the next leap in performance for ./data/random-battles/ would require a rewrite. But there's thousands of lines of duplicated code, so for this added complexity to be sustainable, I think one would have to refactor all the ./data/random-battles/ code into some sort of engine that's configured by callbacks, and then figure out how to reuse objects across loop iterations.

It'd be nice if the test suite got faster, but that alone isn't enough motivation to do all that. I'll give it a couple more days to see if I find another freebie, but it's not looking too hopeful.


On the bright side, I am seeing ~10K fewer ticks (so, ~5%) when I merge in #10608 . So, maybe we can squeeze out a few more % with just sim changes.

@Slayer95
Copy link
Contributor

I think the next leap in performance for ./data/random-battles/ would require a rewrite.

In a context where there are already 2 actively used competing approaches to Random Battles (Battle Factory, and "CG Teams"), I don't know how to feel about this.

@larry-the-table-guy
Copy link
Contributor Author

Found a less invasive and more effective way to reduce the redundant work in randomNPokemon: LRU cache w/ size 1 for speciesPool and pool.

Total ticks went from 185K to 170K. The short-circuiting I had added for getMovePool no longer has a measurable benefit so I reverted it.

I have no strong opinions on the code style for the caches.

ATM I think you could break this by changing the dex field of a RandomTeams instance after construction. Could either prevent that by making dex readonly or by adding dex as part of the cache key.

New v8 prof snippets
 [Summary]:
   ticks  total  nonlib   name
  22711   13.4%   85.9%  JavaScript
   3489    2.1%   13.2%  C++
   3928    2.3%   14.9%  GC
 142420   84.3%          Shared libraries
    249    0.1%          Unaccounted

IME the GC stat doesn't fully reflect the cost of garbage - there's also the time it took to allocate & initialize the object, and worse memory locality, which is hard/impossible to attribute to a single line or function.

 [Bottom up (heavy) profile]:
  Note: percentage shows a share of a particular caller in the total
  amount of its parent calls.
  Callers occupying less than 1.0% are not shown.

   ticks parent  name
  140231   83.0%  /opt/node-v18.20.4-linux-x64/bin/node
  75762   54.0%    /opt/node-v18.20.4-linux-x64/bin/node
  16766   22.1%      /opt/node-v18.20.4-linux-x64/bin/node
   6448   38.5%        LazyCompile: *cullMovePool redacted/dist/data/random-battles/gen9/teams.js:487:15
   4899   76.0%          LazyCompile: *addMove redacted/dist/data/random-battles/gen9/teams.js:691:10
   4532   92.5%            LazyCompile: *randomMoveset redacted/dist/data/random-battles/gen9/teams.js:734:16
    367    7.5%            Function: ^randomMoveset redacted/dist/data/random-battles/gen9/teams.js:734:16
   1251   19.4%          LazyCompile: *randomMoveset redacted/dist/data/random-battles/gen9/teams.js:734:16
   1153   92.2%            LazyCompile: *randomSet redacted/dist/data/random-battles/gen9/teams.js:1612:12
     80    6.4%            Function: ^<anonymous> redacted/test/random-battles/all-gens.js:232:71
     18    1.4%            Function: ^randomSet redacted/dist/data/random-battles/gen9/teams.js:1612:12
    197    3.1%          Function: ^addMove redacted/dist/data/random-battles/gen9/teams.js:691:10
    129   65.5%            LazyCompile: *randomMoveset redacted/dist/data/random-battles/gen9/teams.js:734:16
     68   34.5%            Function: ^randomMoveset redacted/dist/data/random-battles/gen9/teams.js:734:16
    101    1.6%          Function: ^randomMoveset redacted/dist/data/random-battles/gen9/teams.js:734:16
     78   77.2%            LazyCompile: *randomSet redacted/dist/data/random-battles/gen9/teams.js:1612:12
     20   19.8%            Function: ^<anonymous> redacted/test/random-battles/all-gens.js:232:71
      3    3.0%            Function: ^randomSet redacted/dist/data/random-battles/gen9/teams.js:1612:12
   1175    7.0%        LazyCompile: *cullMovePool redacted/dist/data/random-battles/gen7/teams.js:313:15
    939   79.9%          LazyCompile: *addMove redacted/dist/data/random-battles/gen7/teams.js:507:10
    660   70.3%            LazyCompile: *randomMoveset redacted/dist/data/random-battles/gen7/teams.js:545:16
    279   29.7%            Function: ^randomMoveset redacted/dist/data/random-battles/gen7/teams.js:545:16
    174   14.8%          LazyCompile: *randomMoveset redacted/dist/data/random-battles/gen7/teams.js:545:16
    108   62.1%            LazyCompile: *randomSet redacted/dist/data/random-battles/gen7/teams.js:1242:12
     40   23.0%            LazyCompile: *<anonymous> redacted/test/random-battles/all-gens.js:232:71
     26   14.9%            Function: ^randomSet redacted/dist/data/random-battles/gen7/teams.js:1242:12
     54    4.6%          Function: ^randomMoveset redacted/dist/data/random-battles/gen7/teams.js:545:16
     21   38.9%            LazyCompile: *<anonymous> redacted/test/random-battles/all-gens.js:232:71
     18   33.3%            LazyCompile: *randomSet redacted/dist/data/random-battles/gen7/teams.js:1242:12
     15   27.8%            Function: ^randomSet redacted/dist/data/random-battles/gen7/teams.js:1242:12
   1163    6.9%        LazyCompile: *cullMovePool redacted/dist/data/random-battles/gen6/teams.js:204:15
**SNIP**
  11917   15.7%      LazyCompile: *findPokemonEventHandlers redacted/dist/sim/battle.js:755:27
  11796   99.0%        LazyCompile: *findEventHandlers redacted/dist/sim/battle.js:707:20
  11113   94.2%          LazyCompile: *runEvent redacted/dist/sim/battle.js:526:11
   1145   10.3%            LazyCompile: *useMoveInner redacted/dist/sim/battle-actions.js:352:15
   1026    9.2%            LazyCompile: *getStat redacted/dist/sim/pokemon.js:307:10
    823    7.4%            LazyCompile: *endTurn redacted/dist/sim/battle.js:1228:10
    790    7.1%            LazyCompile: *runMove redacted/dist/sim/battle-actions.js:220:10
    648    5.8%            LazyCompile: *getDamage redacted/dist/sim/battle-actions.js:1449:12
    431    3.9%            LazyCompile: *modifyDamage redacted/dist/sim/battle-actions.js:1555:15
    367    3.3%            LazyCompile: *runSwitch redacted/dist/sim/battle-actions.js:175:12
    332    3.0%            LazyCompile: *hitStepAccuracy redacted/dist/sim/battle-actions.js:625:18
    328    3.0%            LazyCompile: *switchIn redacted/dist/sim/battle-actions.js:78:11
    274    2.5%            LazyCompile: *getImmunity redacted/dist/sim/dex.js:212:14
    248    2.2%            LazyCompile: *getActionSpeed redacted/dist/sim/battle.js:2099:17
    242    2.2%            LazyCompile: *spreadMoveHit redacted/dist/sim/battle-actions.js:963:16
    211    1.9%            LazyCompile: *resolveAction redacted/dist/sim/battle-queue.js:72:16
    194    1.7%            LazyCompile: *boost redacted/dist/sim/battle.js:1553:8
    186    1.7%            LazyCompile: *hitStepInvulnerabilityEvent redacted/dist/sim/battle-actions.js:556:30
    183    1.6%            LazyCompile: *getMoveRequestData redacted/dist/sim/pokemon.js:725:21
    176    1.6%            LazyCompile: *trySpreadMoveHit redacted/dist/sim/battle-actions.js:498:19
    176    1.6%            LazyCompile: *runMoveEffects redacted/dist/sim/battle-actions.js:1083:17
    168    1.5%            LazyCompile: *spreadDamage redacted/dist/sim/battle.js:1628:15
    153    1.4%            Function: ^useMoveInner redacted/dist/sim/battle-actions.js:352:15
    137    1.2%            LazyCompile: *eachEvent redacted/dist/sim/battle.js:298:12
    122    1.1%            LazyCompile: *hitStepTypeImmunity redacted/dist/sim/battle-actions.js:592:22
    614    5.2%          LazyCompile: *findEventHandlers redacted/dist/sim/battle.js:707:20
    610   99.3%            LazyCompile: *runEvent redacted/dist/sim/battle.js:526:11
   5655    7.5%      LazyCompile: *cullMovePool redacted/dist/data/random-battles/gen9/teams.js:487:15
   4214   74.5%        LazyCompile: *addMove redacted/dist/data/random-battles/gen9/teams.js:691:10
   3919   93.0%          LazyCompile: *randomMoveset redacted/dist/data/random-battles/gen9/teams.js:734:16
   3610   92.1%            LazyCompile: *randomSet redacted/dist/data/random-battles/gen9/teams.js:1612:12
    259    6.6%            Function: ^<anonymous> redacted/test/random-battles/all-gens.js:232:71
     50    1.3%            Function: ^randomSet redacted/dist/data/random-battles/gen9/teams.js:1612:12
    295    7.0%          Function: ^randomMoveset redacted/dist/data/random-battles/gen9/teams.js:734:16
    241   81.7%            LazyCompile: *randomSet redacted/dist/data/random-battles/gen9/teams.js:1612:12
     48   16.3%            Function: ^<anonymous> redacted/test/random-battles/all-gens.js:232:71
      6    2.0%            Function: ^randomSet redacted/dist/data/random-battles/gen9/teams.js:1612:12
   1130   20.0%        LazyCompile: *randomMoveset redacted/dist/data/random-battles/gen9/teams.js:734:16

@larry-the-table-guy
Copy link
Contributor Author

I estimate ~5 out of 59 seconds (test/random-battles/**) can be saved by caching in getPokemonPool. After that, there's probably another 5 seconds worth I can squeeze out with small, local changes. I'll leave both of those for a later PR.

@larry-the-table-guy larry-the-table-guy marked this pull request as ready for review October 20, 2024 21:15
This is to prevent invalidating the caches derived from 'dex' at construction time
@Slayer95
Copy link
Contributor

Slayer95 commented Oct 21, 2024

  • Species pools caching is a bit funny, but I think it's worth.
  • I wanted to suggest caching status moves in dex, but Nature Power ruins the concept, and there is honestly no need to for dexes without randbats generators.

Let's Get This Merged 🚀

@@ -164,6 +164,12 @@ export class RandomTeams {
*/
moveEnforcementCheckers: {[k: string]: MoveEnforcementChecker};

/** Used by .getPools() */
private poolsCacheKey: any[] | undefined;
Copy link
Member

Choose a reason for hiding this comment

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

It feels like this can be more specific than any[].

Copy link
Member

Choose a reason for hiding this comment

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

[string, number, boolean, RuleTable] | undefined seems to be what it's supposed to be?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants