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

android_jni: Set threads to 2 instead of CPU count #2472

Merged
merged 1 commit into from
Oct 9, 2024

Conversation

vigneshvg
Copy link
Collaborator

Empirically, on Android devices with more than 1 core, it is
almost always better to use 2 threads than to use
number_of_cpu_cores threads.

Update the public java API function documentation to reflect this
change.

The new behavior is:

  • negative values map to number of cpu cores.
  • zero maps to 1 or 2 depending on single core or multi core devices.
  • value >=0 maps to value.

This change ensures that users who use the library via JNI take the
optimal path when using the default value of 0.

* documentation for maxThreads variable in avif.h.
* @param threads Number of threads to be used for the AVIF decode. Zero means use the library
* determined optimal value as the thread count. Negative values mean use number of CPU cores
* as the thread count. When this value is > 0, it is simply mapped to the maxThreads
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sentence "When this value is > 0, it is simply mapped to the maxThreads parameter in libavif." is a little confusing. It seems to imply that the maxThreads parameter in libavif is only set when the value of the threads parameter is > 0.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the sentence about > 0.

@wantehchang
Copy link
Collaborator

In Chrome's avif_image_decoder.cc, I also set libavif's maxThreads parameter to the hardcoded value of 2. I have two reasons:

  1. Almost all computers are multicore today, so I just assume a multicore computer. Not using more than two cores avoids the performance core vs. efficiency core issue we observed when working on libgav1 multithreading.
  2. Chrome is likely to decode many images at the same time, so it does not make sense to use many cores for each image.

@vigneshvg
Copy link
Collaborator Author

In Chrome's avif_image_decoder.cc, I also set libavif's maxThreads parameter to the hardcoded value of 2. I have two reasons:

  1. Almost all computers are multicore today, so I just assume a multicore computer. Not using more than two cores avoids the performance core vs. efficiency core issue we observed when working on libgav1 multithreading.
  2. Chrome is likely to decode many images at the same time, so it does not make sense to use many cores for each image.

Yes, this change is also along the same lines. The only difference is that there are several low-end Android devices in the market which are single core. So i chose min(core_count, 2) as the default value instead of always 2.

Copy link
Collaborator

@wantehchang wantehchang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

* simply mapped to the maxThreads parameter in libavif. For more details, see the
* documentation for maxThreads variable in avif.h.
* @param threads Number of threads to be used for the AVIF decode. Zero means use the library
* determined optimal value as the thread count. Negative values mean use number of CPU cores
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously negative values were invalid. I guess you want to provide a way to specify "use the number of CPU cores", so you now use negative values for that purpose?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes that is correct.

* simply mapped to the maxThreads parameter in libavif. For more details, see the
* documentation for maxThreads variable in avif.h.
* @param threads Number of threads to be used for the AVIF decode. Zero means use the library
* determined optimal value as the thread count. Negative values mean use number of CPU cores
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Add "the" before "number of CPU cores".

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Also removed the comment about failure because of negative values to threads.

Empirically, on Android devices with more than 1 core, it is
almost always better to use 2 threads than to use
number_of_cpu_cores threads.

Update the public java API function documentation to reflect this
change.

The new behavior is:
* negative values map to number of cpu cores.
* zero maps to 1 or 2 depending on single core or multi core devices.
* value >=0 maps to value.

This change ensures that users who use the library via JNI take the
optimal path when using the default value of 0.
@vigneshvg vigneshvg merged commit a12f203 into AOMediaCodec:main Oct 9, 2024
18 checks passed
@vigneshvg vigneshvg deleted the cl_android_threads branch October 9, 2024 16:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants