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

[image] New image cache #1310

Merged
merged 28 commits into from
Apr 13, 2023
Merged

[image] New image cache #1310

merged 28 commits into from
Apr 13, 2023

Conversation

mugulmd
Copy link
Contributor

@mugulmd mugulmd commented Dec 6, 2022

Description

This PR introduces a new image cache, associated with unit tests and a sample application.
The caching mechanism implemented is a combination of the LRU (least-recently-used) policy and reference counting with shared pointers (as we do not wish to delete images already used in some part of the application).

Features

  • image cache class structure and interface
  • thread-safe methods
  • support all pixel types
  • reload when image's last write time on disk has changed
  • unit tests to cover all basic functionalities
  • sample application to simulate various situations

Tests

The caching mechanism has been tested using 3 image sequences with the following characteristics:

  1. dimensions: 1920x1080, nb images: 50
  2. dimensions: 3424x2202, nb images: 20
  3. dimensions: 3870x5796, nb images: 10

under the following situations (each several time, with varying cache memory limits, number of threads, pixel types and half-sampling levels):

  • load a sequence twice
  • load a sequence 6 times, each time using a different pixel type
  • load all 3 sequences
  • load a sequence and then reload some elements of it to check that they become MRU
  • load a sequence, re-write the images on disk and reload them.

@mugulmd mugulmd self-assigned this Dec 6, 2022
@mugulmd mugulmd marked this pull request as ready for review December 9, 2022 18:18
src/aliceVision/image/caching.hpp Outdated Show resolved Hide resolved
src/aliceVision/image/caching.hpp Outdated Show resolved Hide resolved
@fabiencastan fabiencastan added this to the 2.5.0 milestone Feb 8, 2023
@mugulmd
Copy link
Contributor Author

mugulmd commented Feb 9, 2023

Rebased on develop and re-tested with tests in PR description

Copy link
Member

@gregoire-dl gregoire-dl left a comment

Choose a reason for hiding this comment

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

Tested with multiple images (read with ImageCache + write).
Tested with multiple shared pointer usages.
Tested with multiple threads.

Works fine.

I think the use of a larger type for the ImageCache capacity and for memory calculations in bytes should be done before the merge.

src/aliceVision/image/caching.hpp Outdated Show resolved Hide resolved
src/aliceVision/image/caching.hpp Outdated Show resolved Hide resolved
src/samples/imageCaching/main_imageCaching.cpp Outdated Show resolved Hide resolved
src/aliceVision/image/imageAlgo.cpp Outdated Show resolved Hide resolved
src/aliceVision/image/caching.hpp Outdated Show resolved Hide resolved
src/aliceVision/image/caching.hpp Outdated Show resolved Hide resolved
src/aliceVision/image/caching.hpp Outdated Show resolved Hide resolved
src/aliceVision/image/caching.hpp Outdated Show resolved Hide resolved
src/aliceVision/image/resampling.hpp Outdated Show resolved Hide resolved
src/aliceVision/image/CMakeLists.txt Outdated Show resolved Hide resolved
@mugulmd mugulmd marked this pull request as draft February 27, 2023 16:58
@mugulmd mugulmd marked this pull request as ready for review March 2, 2023 16:23
@mugulmd
Copy link
Contributor Author

mugulmd commented Mar 2, 2023

Just rebased on develop and force-pushed to make sure there would be no merge conflicts 😄

@fabiencastan fabiencastan modified the milestones: 3.0.0, 3.0.1 Mar 11, 2023
@fabiencastan fabiencastan modified the milestones: 3.2.0, 3.1.0 Apr 3, 2023
}

std::string memUsageDesc = "\nMemory usage: "
"\n * capacity: " + std::to_string(_info.capacity) +
Copy link
Member

Choose a reason for hiding this comment

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

Need a dedicated function to print in a human readable format, to print with MB, GB, etc depending on the values.

*/
struct CacheInfo
{
/// memory usage limits
Copy link
Member

Choose a reason for hiding this comment

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

precise unit

return _imagePtrs.at(k).useCount() == 1;
});

if (it != _keys.end())
Copy link
Member

Choose a reason for hiding this comment

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

if(it == _keys.end())
{
    break;
}

Then the main code don't need to be in a sub-scope, which is better for readability.

}

// add image to cache if it fits in maxSize
if (memSize + _info.contentSize <= _info.maxSize)
Copy link
Member

Choose a reason for hiding this comment

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

Same here, inverse the condition.
The code on the top level scope should be the main one, then you deal with errors in if sub-scope.

if (memSize + _info.contentSize <= _info.maxSize)
{
    THROW...
}
load..
return _imagePtrs.at(....):

@fabiencastan fabiencastan merged commit f3edd8a into develop Apr 13, 2023
@fabiencastan fabiencastan deleted the mug/imageCaching branch April 13, 2023 17:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants