facebookresearch / nougat

Implementation of Nougat Neural Optical Understanding for Academic Documents
https://facebookresearch.github.io/nougat/
MIT License
8.81k stars 561 forks source link

Replace for-enum loops with for-range loops for better performance #130

Closed urunsiyabend closed 1 year ago

urunsiyabend commented 1 year ago

Memory and runtime optimization for punctation search functions in postprocessing. For-range loops with char lists replaced with for-enum loops and set of chars. Enum loops comes up with better performance when dealing large processes. Enumerate function chunks the given string and do iterating over the chunk. Also it is more bytecode optimized according to range function. And sets are more runtime optimized to iterate over. I ran my own benchmark with very large text content and different iteration counts. I got 30-40% more runtime performance.

Benchmarking find_next_punc input has char size: 28628348
find_next_punc_with_range with 1048576 iter: 0.8537705999915488
find_next_punc_with_enum with 1048576 iter: 0.5067238999763504
find_next_punc_with_range with 2097152 iter: 1.6588582000113092
find_next_punc_with_enum with 2097152 iter: 0.9833648999920115
find_next_punc_with_range with 4194304 iter: 3.3352730000042357
find_next_punc_with_enum with 4194304 iter: 1.9485082000028342
find_next_punc_with_range with 8388608 iter: 6.637666399998125
find_next_punc_with_enum with 8388608 iter: 3.9066971999709494
find_next_punc_with_range with 16777216 iter: 13.280268100032117
find_next_punc_with_enum with 16777216 iter: 7.822930899972562
find_next_punc_with_range with 33554432 iter: 27.017093500006013
find_next_punc_with_enum with 33554432 iter: 15.765664300008211
find_next_punc_with_range with 67108864 iter: 53.380492399970535
find_next_punc_with_enum with 67108864 iter: 31.416383500036318
find_next_punc_with_range with 134217728 iter: 108.29947710002307
find_next_punc_with_enum with 134217728 iter: 62.54330650001066

Benchmarking find_last_punc input has char size: 28628348
find_last_punc_with_range with 1048576 iter: 0.8297756999963894
find_last_punc_with_enum with 1048576 iter: 0.4875224999850616
find_last_punc_with_range with 2097152 iter: 1.6662189000053331
find_last_punc_with_enum with 2097152 iter: 0.9801366000319831
find_last_punc_with_range with 4194304 iter: 3.328882900008466
find_last_punc_with_enum with 4194304 iter: 1.9468706999905407
find_last_punc_with_range with 8388608 iter: 6.617804699984845
find_last_punc_with_enum with 8388608 iter: 3.894040099985432
find_last_punc_with_range with 16777216 iter: 13.795860000012908
find_last_punc_with_enum with 16777216 iter: 7.827177800005302
find_last_punc_with_range with 33554432 iter: 26.675790999957826
find_last_punc_with_enum with 33554432 iter: 15.71281799999997
find_last_punc_with_range with 67108864 iter: 54.92636119999224
find_last_punc_with_enum with 67108864 iter: 31.446818800002802
find_last_punc_with_range with 134217728 iter: 119.90056409995304
find_last_punc_with_enum with 134217728 iter: 73.19332070002565
lukas-blecher commented 1 year ago

was not able to reproduce image