如何缓存1000s的大型C ++对象 [英] how to cache 1000s of large C++ objects

查看:144
本文介绍了如何缓存1000s的大型C ++对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境:
Windows 8 64位,Windows 2008服务器64位
Visual Studio(专业版)2012 64位

Environment: Windows 8 64 bit, Windows 2008 server 64 bit Visual Studio (professional) 2012 64 bits

//我在我的程序中有1000个大型CMyObject,我的缓存,由我的Windows服务程序中的不同线程共享。

list L; //I have 1000s of large CMyObject in my program that I cache, which is shared by different threads in my windows service program.

对于我们的SaaS中间件产品,在内存1000s的大型C ++对象(只读const对象,每个大小约4MB),它运行系统内存不足。我们可以将磁盘文件(或其他一些操作系统管理的持久化机制)与我们的C ++对象相关联吗?不需要共享/进程间通信。

For our SaaS middleware product, we cache in memory 1000s of large C++ objects (read only const objects, each about 4MB in size), which runs the system out of memory. Can we associate a disk file (or some other persistent mechanism that is OS managed) to our C++ objects? There is no need for sharing / inter-process communication.

如果磁盘文件在进程(我们的Windows服务程序)期间工作,则磁盘文件就足够了。只读的const C ++对象由同一个Windows服务中的不同线程共享。

The disk file will suffice if it works for the duration of the process (our windows service program). The read-only const C++ objects are shared by different threads in the same windows service.

我甚至考虑使用对象数据库(如mongoDB)来存储对象,然后在每次使用时加载/卸载对象。虽然比读取我们的序列化文件(希望)更快,但它仍会破坏性能。

I was even considering using object databases (like mongoDB) to store the objects, which will then be loaded / unloaded at each use. Though faster than reading our serialized file (hopefully), it will still spoil the performance.

目的是为了性能原因保留C ++对象的缓存,避免每次加载/卸载序列化C ++对象。

The purpose is to retain caching of C++ objects for performance reason and avoid having to load / unload the serialized C++ object every time. It would be great if this disk file is OS managed and requires minimal tweaking in our code.

预先感谢您的回复。

推荐答案

因此,你的成千上万的巨大的对象有构造函数,析构函数,虚函数和指针。这意味着您无法轻松地 将其弹出。操作系统可以为你做,所以你最实用的方法是简单地增加更多的物理内存,可能是SSD交换卷,并使用该64位地址空间。 (我不知道在你的操作系统上实际可以寻址多少,但大概足以适合你的〜4G的对象)。

So your thousands of massive objects have constructor, destructor, virtual functions and pointers. This means you can't easily page them out. The OS can do it for you though, so your most practical approach is simply to add more physical memory, possibly an SSD swap volume, and use that 64-bit address space. (I don't know how much is actually addressable on your OS, but presumably enough to fit your ~4G of objects).

你的第二个选择是找到一种方式只是保存一些内存。这可能使用专门的分配器来减少松弛,或删除间接层。您尚未提供足够的资料,我可以针对您的资料提出具体的建议。

Your second option is to find a way to just save some memory. This might be using a specialized allocator to reduce slack, or removing layers of indirection. You haven't given enough information about your data for me to make concrete suggestions on this.

第三个选择,假设您可以适合您的程式在记忆体,只是加快你的反序列化。您可以将格式更改为可以更有效地解析的内容吗?你能以某种方式根据需要快速反序列化对象吗?

A third option, assuming you can fit your program in memory, is simply to speed up your deserialization. Can you change the format to something you can parse more efficiently? Can you somehow deserialize objects quickly on-demand?

最后一个选项,最多的工作是手动管理交换文件。作为第一步,将大量多态类分成两个是合理的:一个多态的flyweight(每个具体子类型有一个实例)和一个扁平的聚合上下文结构。

The final option, and the most work, is to manually manage a swapfile. It would be sensible as a first step to split your massive polymorphic classes into two: a polymorphic flyweight (with one instance per concrete subtype), and a flattened aggregate context structure. This aggregate is the part you can swap in and out of your address space safely.

现在你只需要一个内存映射分页机制,某种类型的缓存跟踪哪些页面当前映射,可能是一个智能指针替换您的原始指针与一个页面+偏移量可以按需映射数据等。再次,您没有给出足够的信息,您的数据结构和访问模式,以提出更详细的建议。

Now you just need a memory-mapped paging mechanism, some kind of cache tracking which pages are currently mapped, possibly a smart pointer replacing your raw pointer with a page+offset which can map data in on-demand, etc. Again, you haven't given enough information on your data structure and access patterns to make more detailed suggestions.

这篇关于如何缓存1000s的大型C ++对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆