Closed Li-Weiwei closed 6 years ago
Before the code is modified, print('print_times:%d' %(print_times)) runs about 9000 times in 60 seconds. But after modification, it can run about 30000 times in 60 seconds.
That sounds reasonable. Before your patch, WIZCHIP_EXPORT(recvfrom) was sitting in a busy-wait loop polling the Wiznet device. It was therefore using about 50% of the CPU, and the other 50% was used by work_thread(). After your patch, WIZCHIP_EXPORT(recvfrom) was yielding to the thread scheduler, telling it to schedule another waiting thread (namely work_thread()). So in your case pretty much 99% of the CPU time was spent in work_thread().
In fact, once the socket has been added to a program without that modification, it will result in a much lower program efficiency.I'm not sure if there are other places like this.Will the problem be fixed in the future?
Fixed by above PR.
In #3302, I found that when I call the recvfrom function of the udp socket, I made the micropython no longer running. Now, it is fixed by cc7fece309b0ce6d361cade8690b6c3a162d7378, like this:
I found the message of cc7fece309b0ce6d361cade8690b6c3a162d7378 said that "accept already releases the GIL because it calls mp_hal_delay_ms() within its busy-wait loop." So I modified the code like this below:
I find that before and after the modification, micropython performance changes greatly. I write a script to test this phenomenon:
Before the code is modified, print('print_times:%d' %(print_times)) runs about 9000 times in 60 seconds. But after modification, it can run about 30000 times in 60 seconds.
I do not know much about micropython, so I do not know if this is right or wrong. @dpgeorge