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

Create a subclass of NSCache using a weak cache. #2228

Merged
merged 1 commit into from Feb 25, 2018

Conversation

dreampiggy
Copy link
Contributor

@dreampiggy dreampiggy commented Feb 23, 2018

Only remove the cache when memory warning and sync back the alive instance from weak cache into cache.

New Pull Request Checklist

  • I have read and understood the CONTRIBUTING guide

  • I have read the Documentation

  • I have searched for a similar pull request in the project and found none

  • I have updated this branch with the latest master to avoid conflicts (via merge from master or rebase)

  • I have added the required tests to prove the fix/feature I am adding

  • I have updated the documentation (if necessary)

  • I have run the tests and they pass

  • I have run the lint and it passes (pod lib lint)

This merge request fixes / reffers to the following issues: #791 #2227

Pull Request Description

See the description in #791. We can add a weak cache into memory cache for a little performance improvement on iOS/tvOS.

This is useful when the memory warning, the cache was purged. However, the image instance can be retained by other instance such as imageViews and alive. At this case, we can sync weak cache back and do not need to load from disk cache.

For example, you are showing a big imageView with a image, and a tableView contains the thumb image(Through the UIImage instance is the same one, only size and scale on UIImageView different). When memory warning and cache purged, you can also directlly get the image from weak cache without async query disk or re-download.

For other normal manually access method such as removeObjectForKey:, removeAllObjects, we should also remove that weak cache to sync the state.

@dreampiggy dreampiggy added this to the 4.3.1 milestone Feb 23, 2018
@dreampiggy dreampiggy force-pushed the improvement_weak_cache branch 2 times, most recently from 564e6da to 2107e7e Compare February 23, 2018 15:31
…e when memory warning and sync back the alive instance from weak cache into cache.
@codecov-io
Copy link

codecov-io commented Feb 23, 2018

Codecov Report

Merging #2228 into master will decrease coverage by 0.01%.
The diff coverage is 85.18%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master   #2228      +/-   ##
=========================================
- Coverage   78.42%   78.4%   -0.02%     
=========================================
  Files          36      36              
  Lines        3685    3733      +48     
  Branches      340     341       +1     
=========================================
+ Hits         2890    2927      +37     
- Misses        772     783      +11     
  Partials       23      23
Impacted Files Coverage Δ
SDWebImage/UIView+WebCacheOperation.m 70.96% <100%> (ø) ⬆️
SDWebImage/SDImageCache.m 68.8% <84.9%> (+1.72%) ⬆️
SDWebImage/SDWebImageDownloaderOperation.m 91.2% <0%> (-0.98%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1986669...216dfeb. Read the comment docs.

@@ -26,7 +26,7 @@ - (SDOperationsDictionary *)sd_operationDictionary {
if (operations) {
return operations;
}
operations = [[NSMapTable alloc] initWithKeyOptions:NSMapTableCopyIn valueOptions:NSMapTableWeakMemory capacity:0];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Acutally copy is useless. Because we always pass a immutable NSString but not NSMutableString. Use strong-weak is OK.

@dreampiggy dreampiggy merged commit 37e93a1 into SDWebImage:master Feb 25, 2018
@dreampiggy dreampiggy deleted the improvement_weak_cache branch February 25, 2018 03:47
@bpoplauschi
Copy link
Member

Nice @dreampiggy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants