This code assumes use with the LÖVE framework!
This is just a showcase of how I tried to optimize collisions of a large number of objects. Applied optimizations: spatial hashing, chunking, multithreading, C structures instead of lua tables. Also, to draw objects efficiently, I used instancing.
Video demonstration: https://www.youtube.com/watch?v=Hk2bZtoRQbU
The algorithm is approximately as follows: I store the positions of objects in image data, which is accessed from lua via ffi pointer. The image data is shared between the threads in which the integration and collision resolution stage is performed. To draw objects, image data is converted to a texture and sent to a shader in which the positions of objects are read from a pixel. The spatial hashing algorithm was implemented under the influence of Matthias Müller's explanation from this video: https://www.youtube.com/watch?v=D2M8jTtKi44