I have made a chess engine, and it seems from cProfile that my most expensive function is the one that calculates attack squares for both players. Here is the cProfile results for a perft function I wrote that goes 4 ply deep: https://i.stack.imgur.com/TADwV.jpg
The reason why my calculate attack squares function is so expensive is because every time a move is made in perft, the position is a new one, and attack squares for both players have to be recalculated. My question is this: Is there a more efficient way to be calculating attack squares for both players? Is there a way I can avoid having to recalculate attack squares for all 32 pieces every time a move is made in perft? Thank you.
https://repl.it/@kasparov/ImportantNotedCoderesource My code for anyone who's curious