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

Glide Optimisation #4629

Open
mwshubham opened this issue Sep 7, 2021 · 2 comments
Open

Glide Optimisation #4629

mwshubham opened this issue Sep 7, 2021 · 2 comments

Comments

@mwshubham
Copy link

mwshubham commented Sep 7, 2021

Glide Version:
Integration libraries:

    api 'com.github.bumptech.glide:glide:4.11.0'
    api 'com.github.bumptech.glide:okhttp3-integration:4.11.0'
    kapt 'com.github.bumptech.glide:compiler:4.11.0'

Issue details / Repro steps / Use case background:
I am getting an unidentified error that Images are not loading for some cases.
I checked in Mi/Xiaomi Device this is happening while not on Samsung.

Recent optimization that we shared earlier is

  1. Reduced Disk Cache Size
  2. Using DiskCache Strategy to Resource

Will any of this affect image loading somehow?

@GlideModule
class MainAppGlideModule : AppGlideModule() {

    override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
        registry.prepend(String::class.java, Bitmap::class.java, VideoThumbnailModelLoaderFactory())
        val builder = OkHttpClient.Builder()
        StethoUtils.addNetworkInterceptor(builder)
        registry.replace(
            GlideUrl::class.java,
            InputStream::class.java,
            OkHttpUrlLoader.Factory(builder.build())
        )
    }

    override fun applyOptions(context: Context, builder: GlideBuilder) {
        super.applyOptions(context, builder)
        if (RemoteConfigUtils.enableGlideOptimisation()) {
            val diskCacheSizeBytes = 1024 * 1024 * 150 // 150 MB
            builder.setDiskCache(
                InternalCacheDiskCacheFactory(
                    context,
                    diskCacheSizeBytes.toLong()
                )
            )
        }
    }
}

.diskCacheStrategy(DiskCacheStrategy.RESOURCE)

Observations
There is no network call performed to fetch the image as we have integrated Glide Image with Okhttp to review the images loading.

Removing VideoThumbnailModelLoaderFactory seems to be working.

public class VideoThumbnailModelLoaderFactory implements ModelLoaderFactory<String, Bitmap> {

    @NotNull
    @Override
    public ModelLoader<String, Bitmap> build(@NotNull MultiModelLoaderFactory unused) {
        return new VideoThumbnailModelLoader();
    }

    @Override
    public void teardown() {
        // Do nothing.
    }
}
public final class VideoThumbnailModelLoader implements ModelLoader<String, Bitmap> {

    @Nullable
    @Override
    public LoadData<Bitmap> buildLoadData(@NotNull String model, int width, int height, @NotNull Options options) {
        return new LoadData<>(new ObjectKey(model), new VideoThumbnailDataFetcher(model));
    }

    @Override
    public boolean handles(String model) {
        return model.contains(".mp4") && !model.contains(".png") && !model.contains(".jpg") && !model.contains(".jpeg");
    }
}
public class VideoThumbnailDataFetcher implements DataFetcher<Bitmap> {

    private final String model;
    private MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();

    VideoThumbnailDataFetcher(String model) {
        this.model = model;
    }

    @Override
    public void loadData(@NonNull Priority priority, @NonNull DataCallback<? super Bitmap> callback) {
        Bitmap bitmap = null;
        try {
            mediaMetadataRetriever.setDataSource(model, new HashMap<>());
            bitmap = mediaMetadataRetriever.getFrameAtTime();
            callback.onDataReady(bitmap);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            release();
        }
    }

    @Override
    public void cleanup() {
        release();
    }

    @Override
    public void cancel() {
        //empty
    }

    private void release() {
        if (mediaMetadataRetriever != null) {
            try {
                mediaMetadataRetriever.release();
            } catch (Exception e) {
                e.printStackTrace();
            }
            mediaMetadataRetriever = null;
        }
    }

    @NonNull
    @Override
    public Class<Bitmap> getDataClass() {
        return Bitmap.class;
    }

    @NonNull
    @Override
    public DataSource getDataSource() {
        return DataSource.LOCAL;
    }
}

Does VideoThumbnailDataFetcher download the mp4 video fully or only the single frame? Can you suggest us some optimization, if any?

This Looks related -> #4021

Glide Logs.pdf

@mwshubham
Copy link
Author

DiskLruCache Journal File

libcore.io.DiskLruCache
1
1
1

DIRTY 657f374d246ad33288fe98914a36356de01cded319ec823aa51139b3c43e5809
DIRTY 1af7f214861b49259985854fe9e017ae17b833c1f35c1bdb7cbfaeae0def7cc3
DIRTY 1199588aadaf322793fd1468c6ce9a5460b147e1358a42ac478fac3f60c03c25
CLEAN 1af7f214861b49259985854fe9e017ae17b833c1f35c1bdb7cbfaeae0def7cc3 2155
CLEAN 657f374d246ad33288fe98914a36356de01cded319ec823aa51139b3c43e5809 2372
CLEAN 1199588aadaf322793fd1468c6ce9a5460b147e1358a42ac478fac3f60c03c25 2154
DIRTY 53b0e84438014b7074083b61cad1da148199e9a9bad22e6c33f32131d45b975f
CLEAN 53b0e84438014b7074083b61cad1da148199e9a9bad22e6c33f32131d45b975f 2982
DIRTY 5a5b9a3b1ac73cc1fadfcb9ea9c3d89eb149109741c34564b1379768695f20a4
CLEAN 5a5b9a3b1ac73cc1fadfcb9ea9c3d89eb149109741c34564b1379768695f20a4 8922
DIRTY 552b8f0ed4088f1f6d51bb6e24bbd2bd58cf92f75db741792cca3b6f81980294
CLEAN 552b8f0ed4088f1f6d51bb6e24bbd2bd58cf92f75db741792cca3b6f81980294 10711
DIRTY 3200196d890214ee6ef1cc48f14778db509c19e86ee7c5f17987e7f74af90902
CLEAN 3200196d890214ee6ef1cc48f14778db509c19e86ee7c5f17987e7f74af90902 9557
DIRTY 0c7d80dc2a62d3317badb71d2b27a44bf979f4870fdc041968fc13b190fedb2d
DIRTY 4514c2f4c204aad4aa142d5b492faefcd557d24d12c005737962c5fef7d2f186
CLEAN 4514c2f4c204aad4aa142d5b492faefcd557d24d12c005737962c5fef7d2f186 5664
CLEAN 0c7d80dc2a62d3317badb71d2b27a44bf979f4870fdc041968fc13b190fedb2d 10762
DIRTY 80002c6dc175f226d506695b46329c5ffc9e9212c0662728541c56e241125c62
CLEAN 80002c6dc175f226d506695b46329c5ffc9e9212c0662728541c56e241125c62 11097
DIRTY f0f770a4bc449e1e72ed91e9522ab86de98a6ee9b6e069d084ecd5a6023f1531
CLEAN f0f770a4bc449e1e72ed91e9522ab86de98a6ee9b6e069d084ecd5a6023f1531 13552
DIRTY 5e4c6dfdea39c7af66f6e12dbd11522084b397bcf2860f5ec9dd408d9190d40c
DIRTY a415e54f0bd3ed07a336632869dbe3d4de804aa0bcbf1fa3f858364e83191b40
DIRTY 90ff738b28d8d951cc8dee2a83f12b95676716e7a388dfa23d2f76da4348f7ae
CLEAN 90ff738b28d8d951cc8dee2a83f12b95676716e7a388dfa23d2f76da4348f7ae 6472
CLEAN 5e4c6dfdea39c7af66f6e12dbd11522084b397bcf2860f5ec9dd408d9190d40c 17368
CLEAN a415e54f0bd3ed07a336632869dbe3d4de804aa0bcbf1fa3f858364e83191b40 15282
DIRTY 8e223d0005d1625f003368bae94ca3cc1935c591b6124229d5e43e388226c376
CLEAN 8e223d0005d1625f003368bae94ca3cc1935c591b6124229d5e43e388226c376 20440
DIRTY 1d2360ee6ae1892c44bc22fe45a60a1590e74b233b19f1a1ebf591a827036d00
DIRTY 84ec2172aef6f042f0e2eecf4e544167a48ad401ddd3e266d46c585b8d37dad6
CLEAN 1d2360ee6ae1892c44bc22fe45a60a1590e74b233b19f1a1ebf591a827036d00 52145
CLEAN 84ec2172aef6f042f0e2eecf4e544167a48ad401ddd3e266d46c585b8d37dad6 55250
DIRTY fab5174e5e5303230c2dda716f275264d1cb15061689d25b017d3094c47e0436
DIRTY f48f60d899b661446b2e9d177864d138f26176b4e5f613f64c041b8411aa322f
CLEAN fab5174e5e5303230c2dda716f275264d1cb15061689d25b017d3094c47e0436 44178
CLEAN f48f60d899b661446b2e9d177864d138f26176b4e5f613f64c041b8411aa322f 45068
DIRTY 554f68318d6057ae1f76dd0165961a185148d14c043c4a9a95e825cb3bc19a17
CLEAN 554f68318d6057ae1f76dd0165961a185148d14c043c4a9a95e825cb3bc19a17 9890
READ 554f68318d6057ae1f76dd0165961a185148d14c043c4a9a95e825cb3bc19a17
DIRTY 42e71f0b3b237f5e6506ab02e9fd02ab1f9143741ef37f64cd993cef53cf7516
CLEAN 42e71f0b3b237f5e6506ab02e9fd02ab1f9143741ef37f64cd993cef53cf7516 18460
DIRTY 9a95db9d2725b0b9fb9e8fe8a53b438bc4d016a11b9de40a46a7819c20258997
CLEAN 9a95db9d2725b0b9fb9e8fe8a53b438bc4d016a11b9de40a46a7819c20258997 5634
DIRTY 5d1c2896c513566e01eea6e699ed6c9ab9d7d3aac26bf9e3fee799736627549c
DIRTY 56e485e62075b705778e79aa421e4145c1f349f81dcae0af64672ca343b8fc36
CLEAN 56e485e62075b705778e79aa421e4145c1f349f81dcae0af64672ca343b8fc36 8419
CLEAN 5d1c2896c513566e01eea6e699ed6c9ab9d7d3aac26bf9e3fee799736627549c 21562
DIRTY 04668a0480f6011534c979f831c96b02dcf8ddbe621ebecca8e67c25a9ad5872
DIRTY e6e8cbd15050db0d85e226b43c77e26fbb64e8110992b38454e3ffd0935b0e24
DIRTY e406845269d756d9877e80cb71dbca1d02cd428c79f9bcba0b4d61697ded33f7
CLEAN 04668a0480f6011534c979f831c96b02dcf8ddbe621ebecca8e67c25a9ad5872 12670
DIRTY 3d5b6677cfb6714691abdff16de6f946fa07f8fc804d76996bdf899773103db8
CLEAN e406845269d756d9877e80cb71dbca1d02cd428c79f9bcba0b4d61697ded33f7 21979
CLEAN e6e8cbd15050db0d85e226b43c77e26fbb64e8110992b38454e3ffd0935b0e24 24905
CLEAN 3d5b6677cfb6714691abdff16de6f946fa07f8fc804d76996bdf899773103db8 13322
DIRTY ae1bd6123e596f89611e2a8c4630e9a0d8d84c304fdeda421d23a07f1ffce839
DIRTY acf03956be7a91e9345377677e8876c60eb8928b674f66a1796db0e3305271ba
DIRTY 016fbfa620e282094607e05103d0a2389614f2a74ac572834f132df5e7af4bd3
CLEAN acf03956be7a91e9345377677e8876c60eb8928b674f66a1796db0e3305271ba 1639
CLEAN ae1bd6123e596f89611e2a8c4630e9a0d8d84c304fdeda421d23a07f1ffce839 66944
CLEAN 016fbfa620e282094607e05103d0a2389614f2a74ac572834f132df5e7af4bd3 6526
DIRTY 2be5c03200e2aed40690a6d909b14b34e8f1135672fb8bb2d2a878f941e87656
CLEAN 2be5c03200e2aed40690a6d909b14b34e8f1135672fb8bb2d2a878f941e87656 74212
DIRTY b35c77e856776ef2578985c08b9611db006eb164e0b68376497597007c3d588c
CLEAN b35c77e856776ef2578985c08b9611db006eb164e0b68376497597007c3d588c 2319
DIRTY 9b2f7c19c0740725b7a53f476229fb9ef72cbeead5b8f9887cb7275edd06966b
CLEAN 9b2f7c19c0740725b7a53f476229fb9ef72cbeead5b8f9887cb7275edd06966b 3872
DIRTY 36d4587c72daad3a3264ad06e62d84f4ef72cff0a8190b13ec0c516e031e865c
CLEAN 36d4587c72daad3a3264ad06e62d84f4ef72cff0a8190b13ec0c516e031e865c 5271

@ArtRoman
Copy link

ArtRoman commented Sep 8, 2021

api 'com.github.bumptech.glide:glide:4.11.0'

Why are you using outdated version of library? There is Glide 4.12.0 with some fixes and improvements.

Does VideoThumbnailDataFetcher download the mp4 video fully or only the single frame? Can you suggest us some optimization, if any?

setDataSource(FileDescriptor fd) calls setDataSource(fd, 0, 0x7ffffffffffffffL) with notification "This method may be time-consuming." So it seems it loads all video for the first frame. Also, first frame can be black, and this thumb will be cached as preview.

Maybe related to #673 ?

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

No branches or pull requests

2 participants