Skip to content

Cahce large Image Maps when rendering animations #395

@mlsomers

Description

@mlsomers

Summary

Parsing takes allot of time when loading huge image maps (height and color maps for planets for example). To speed up animation rendering these images could be cached for reuse.

Suggested Solution

I did an attempt of getting my feet wet in C++, not successful so far :-(

Here's some C# pseudo code of what I would like to do:

namespace pseudocode
{
  public class Parser // this would be pov_parser::Parser
  {
    public class Image { } // placeholder representing pov_base::Image


    // -- parser.h

    private struct ImageFileInfo // The Dictionary Value type
    {
      public DateTime LastModifiedDate;
      public Image Image;
    }

    private static Dictionary<string, ImageFileInfo> Cache = new Dictionary<string, ImageFileInfo>(); // static cache

    
    // -- parser.cpp

    // Replace the original Read_Image with a check if it is in the cache
    public Image Read_Image(int filetype, string filename, int options)
    {
      ImageFileInfo imageInfo;
      if (Cache.TryGetValue(filename, out imageInfo))
      {
        if(File.GetLastWriteTime(filename) == imageInfo.LastModifiedDate)
        return imageInfo.Image;
      }

      imageInfo = new ImageFileInfo { 
        LastModifiedDate = File.GetLastWriteTime(filename),
        Image = Original_Read_Image(filetype, filename, options)
      };

      Cache.Add(filename, imageInfo);
      return imageInfo.Image;
    }

    // Placeholder for the original Read_Image method...
    private Image Original_Read_Image(int filetype, string filename, int options) {
      return null;
    }

    // Allow UI to clear memory
    public void ClaerCache()
    {
      Cache.Clear();
    }
  }
}

This is my lousy attempt at C++ (for now omitting the LastWriteTime check)

parser.h:

+      static std::map<pov_base::UCS2, pov_base::Image> ImageCache;

       Image *Read_Image(int filetype, const UCS2 *filename, const ImageReadOptions& options);
+      Image *Original_Read_Image(int filetype, const UCS2* filename, const ImageReadOptions& options);

parser.cpp:

Image *Parser::Read_Image(int filetype, const UCS2* filename, const ImageReadOptions& options)
{
    std::map<UCS2, Image>::iterator idx = ImageCache.find(*filename);
    if (idx != ImageCache.end()) {
        Image& ret = ImageCache[*filename];
        return &ret;
    }

    Image *img = Original_Read_Image(filetype, filename, options);
    ImageCache[*filename] = *img;
    return img;
}

Image *Parser::Original_Read_Image(int filetype, const UCS2* filename, const ImageReadOptions& options)
{
    unsigned int stype;
    ...
}

So far I've been unable to figure out the pointer/reference stuff for filling the Cache, also I don't understand why the map is trying to create an instance... and how does the map know how to sort (C# uses a hash function for sorting).

Any way, even if I got this far I would have to find, and disable, the cleaning up code for the images, prevent them from being freed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureproposal for a new feature, or alleged bug triaged as intentional but warranting improvement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions