olokelo / jxlpy

Cython bindings and Pillow plugin for JPEG XL
MIT License
45 stars 12 forks source link

Implementing multithreading #9

Closed C0rn3j closed 2 years ago

C0rn3j commented 2 years ago

I took a music cover and ran the 2x example test, generating a 13 MB JXL.

This takes a VERY long time to load via jxlpy

image

In comparison for example with nomacs, which takes less than five times what jxlpy does due to using multiple threads.

image

Would it be feasible to make this library use multiple threads? I was trying to use an application with Pillow and this extension, and loading this image made the startup time very long.

olokelo commented 2 years ago

Hi!

Firstly, sorry for late response, I've got a rather busy week. Secondly, Thank you so much for adding my library to AUR :)

I also have some good news, your issue can be easily resolved by providing num_threads parameter to JXLPyDecoder.

import jxlpy
import time
import multiprocessing

CPU_COUNT = multiprocessing.cpu_count()

with open('cover_2x.jxl', 'rb') as f:
    data = f.read()

def decode_default():
    dec = jxlpy.JXLPyDecoder(data)
    output = dec.get_frame()
    dec.close()

def decode_multi():
    dec = jxlpy.JXLPyDecoder(data, num_threads=CPU_COUNT)
    output = dec.get_frame()
    dec.close()

for fun in (decode_default, decode_multi):
    old_time = time.time()
    fun()
    print('{}: {}'.format(fun.__name__, time.time()-old_time))
decode_default: 8.677569150924683
decode_multi: 0.7362372875213623