-
Notifications
You must be signed in to change notification settings - Fork 6.1k
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
Cleanup proposal for .fromBytes().load(byte[]) #437
Comments
A couple of things:
|
Currenlty ALL byte arrays are treated as equal, regardless of length and memory address, let alone contents. Did you by any chance confuse Imagine displaying a byte[] with a resize, a grayscale, a blur and a padding+shadow transformation. Would you still say it's not worth caching the result then? If the same byte[] is passed in I would expect the same Bitmap to come out without going through the full Glide pipeline. Repeated load may happen if you have a This issue came from a few hours of debugging why a particular part of my app wasn't working. The effects I lined up were assumptions I had, based on previous experience with other |
Ah sorry yes I did confuse the two. I think the goal was to try to encourage people to actually provide some meaningful id, since the only way we could actually cache the byte array is to both rely on the contents of the array never changing and to rely on the user to provide the same byte array instance for each subsequent load. I agree it's worth caching, I just don't think there's a great way for us to come up with a reasonable key. In most cases, users should be able to do so (where did they get the bytes from?), and can by just passing in an appropriate signature? The problem with putting anything in the id field is that users can no longer tell us the key should be. If they provide a signature, they still won't get a cache hit because they key will still be partially dependent on the byte array, rather than the id they provide. |
But the path to provide meaningful ID will disappear by removing the On the other hand everything else works as byte[] is not, currently.
I think this could be achieved if we remove both of those lines from |
I think there are three choices:
Each of these has it's own problem:
All of these error cases are relatively subtle too. Is there any way we can either eliminate one of the problems above, or make one of the failure cases more obvious? |
How about going with 3. in this way: ByteArrayFetcher.getId() {
return "";
}
/** add: You need to set a signature or disable all caching explicitly. */
RequestManager.fromBytes() {
return (DrawableTypeRequest<byte[]>) loadGeneric(byte[].class)
// remove: .signature(new StringSignature(UUID.randomUUID().toString()))
// remove: .diskCacheStrategy(DiskCacheStrategy.NONE)
// remove: .skipMemoryCache(true /*skipMemoryCache*/);
}
GenericRequest.init(...) {
// ...
if (model != null) {
// add: check for byte[] and require user to chose how to handle them
if (model instanceof byte[] && (isMemoryCacheable || diskCacheStrategy != NONE))
check("signature", signature == EmptySignature.obtain()? null : signature,
"try .signature(Key) or .skipMemoryCache(true).diskCacheStrategy(NONE)");
}
// ... existing stuff
}
} Would make sure the there's a signature, or forces the user to explicitly disable the diskCache and the memory cache. It means that load(byte[]) would throw by default, but we can consider it the same amount advanced as |
Glide Version: 3.5+ -> 4.0
Issue details:
StreamByteArrayLoader
constructor is deprecated and will be removed along with.load(byte[], id)
method so it won't be possible to pass theid
toByteArrayFetcher
without rewriting the removed deprecated code. So theByteArrayFetcher
constructor should be deprecated too in 3.6.0 and be removed in 4.0 along with the other proposed changes in this issue.The documentation fo
ByteArrayFetcher
says:which was probably true pre-
.signature()
, but that statement is not needed any more. Currently allbyte[]
s are treated equal, because the fetcher doesn't provide a sensible id (default is""
) inEngine.load
. Which means.fromBytes()
and.load(byte[])
can't be used with memory cache and also requires a signature to be passed in (I guess this is the reason why those two things are insidefromBytes
).To reach consistency with other methods I suggest:
This will have the following effects:
equivalent to file path or unique uri
ByteArrayFetcher.String id
field will be clearly useless, as should be, based on the.signature()
API(other Fetchers also have their model as
getId()
.signature(new StringSignature(UUID.randomUUID().toString()))
could be removed fromRequestManager.fromBytes()
since thebyte[]
's memory address will uniqely identify the dataall other uses of
signature
inRequestManager
is either a deprecation workaround or sensible default (ApplicationVersionSignature
).skipMemoryCache(true /*skipMemoryCache*/)
may be removed fromRequestManager.fromBytes()
, because there may be transformations and other operations like creating bitmaps, which are worth caching. It's currently not possible to sayfromBytes().skipMemoryCache(false)
and get valid results.byte[]
then modifiying the contents and loading it again need to provide a signature, but for most cases I see that won't be the case. And the cache gains are more useful.@sjudd Please let me know what you think about the idea.
The text was updated successfully, but these errors were encountered: