-
Notifications
You must be signed in to change notification settings - Fork 660
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
Move cache creation to be lazy #4781
Conversation
✅ Deploy Preview for apollo-android-docs canceled.
|
return MemoryCacheFactory().create() | ||
} | ||
}).build() | ||
assertNull(cacheCreateThreadName) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice test!
private val cache: OptimisticCache = OptimisticCache().chain(normalizedCacheFactory.createChain()) as OptimisticCache | ||
// Keeping this as lazy to avoid accessing the disk at initialization which usually happens on the main thread | ||
private val cache: OptimisticCache by lazy { | ||
OptimisticCache().chain(normalizedCacheFactory.createChain()) as OptimisticCache |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is lazy thread safe? I remember reading something or some rumour saying lazy
was generating a lot of bytecode. Might be worth making it a lateninit and use regular locks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By default by lazy
uses a synchronized block on the JVM and on native it looks like it's using a Lock.
For the bytecode yes it can be "verbose" (👀 this from 2019!)
Pushed a commit doing this manually LMKWYT!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks probably ok to keep lazy
here according to Chet guidance. It's quite an heavy object and allocation is done only once so both work for me!
if (_cache == null) { | ||
lock.write { | ||
if (_cache == null) { | ||
_cache = OptimisticCache().chain(normalizedCacheFactory.createChain()) as OptimisticCache | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That doesn't work though. The whole if {}
needs to be locked or you could create 2 caches
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah never mind, there's double if !
6161a94
to
cc5a2ec
Compare
Related to #4780