Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize music loading threading and state updates #681

Open
4 tasks done
notzed opened this issue Jan 19, 2024 · 1 comment
Open
4 tasks done

Optimize music loading threading and state updates #681

notzed opened this issue Jan 19, 2024 · 1 comment
Assignees
Labels
bug Something isn't working critical Need to get this fixed right now music Related to music loading ui Related to app UI

Comments

@notzed
Copy link

notzed commented Jan 19, 2024

Describe the Bug/Crash

I was trying to profile the scanning process and noticed some less than ideal behaviour for larger libraries.

Firstly setText() seems to be using about 30% of cpu time on the main thread just updating the "Loading your music library... (xxx/xxx)" text. Secondly a rescan launches thousands of threads.

I tried using a threadpool executorservice in exoplayer/upstream/Loader, this about halved the threads created but it didn't affect wall-clock-time and android studio is so miserable and buggy it's hard to know if it improved cpu usage (I think it did though).

I don't know enough about android event loops to work out how to change exoplayer/MetadataRetriever to use a thread pool. That bit of code seems terribly complicated and designed for a different use case such as async retrieval of metadata from a small number of remote sources and not thousands of local files.

So I didn't have much luck improving performance, although I might suggest removing the numbers from "Loading your music library ... (xxx/xxx)" since all they do is jump around annoyingly and don't tell the user anything useful.

I did notice the following comes up a few times during a rescan in the unmodified version. I couldn't identify what causes it.

Handler (android.os.Handler) {5a20ac4} sending message to a Handler on a dead thread
java.lang.IllegalStateException: Handler (android.os.Handler) {5a20ac4} sending message to a Handler on a dead thread
	at android.os.MessageQueue.enqueueMessage(MessageQueue.java:567)
	at android.os.Handler.enqueueMessage(Handler.java:754)
	at android.os.Handler.sendMessageAtTime(Handler.java:703)
	at android.os.Handler.sendMessageDelayed(Handler.java:673)
	at android.os.Handler.post(Handler.java:403)
	at androidx.media3.exoplayer.source.ProgressiveMediaPeriod.onUpstreamFormatChanged(ProgressiveMediaPeriod.java:721)
	at androidx.media3.exoplayer.source.SampleQueue.format(SampleQueue.java:594)
	at androidx.media3.extractor.mp3.Mp3Extractor.readInternal(Mp3Extractor.java:277)
	at androidx.media3.extractor.mp3.Mp3Extractor.read(Mp3Extractor.java:242)
	at androidx.media3.exoplayer.source.BundledExtractorsAdapter.read(BundledExtractorsAdapter.java:133)
	at androidx.media3.exoplayer.source.ProgressiveMediaPeriod$ExtractingLoadable.load(ProgressiveMediaPeriod.java:1067)
	at androidx.media3.exoplayer.upstream.Loader$LoadTask.run(Loader.java:433)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
	at java.lang.Thread.run(Thread.java:919)

Describe the intended behavior

Better cpu usage and running time.

What android version do you use?

Android 13

What device model do you use?

pixel 4a with lineagos 20, but also a motog8+ with android 10.

Bug report

as above.

Duplicates

@notzed notzed added the bug Something isn't working label Jan 19, 2024
@OxygenCobalt
Copy link
Owner

Whoa! Thanks for this analysis. I'll look into this myself.

@OxygenCobalt OxygenCobalt changed the title Issues/observations with library (re)scan Loading inefficiency issue Jan 19, 2024
@OxygenCobalt OxygenCobalt added music Related to music loading ui Related to app UI labels Jan 20, 2024
@OxygenCobalt OxygenCobalt changed the title Loading inefficiency issue Optimize music loading threading and state updates Jan 25, 2024
@OxygenCobalt OxygenCobalt added the critical Need to get this fixed right now label Mar 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working critical Need to get this fixed right now music Related to music loading ui Related to app UI
Projects
None yet
Development

No branches or pull requests

2 participants