我的项目(在 Delphi 6 上运行!)需要一个内存分配列表 (TMemoryAllocation),我将其存储在一个对象中,该对象还保存有关分配大小 (FSize) 以及分配是否正在使用或空闲 (FUused) 的信息。我基本上使用它作为垃圾收集器和一种让我的应用程序始终分配/解除分配内存的方法(并且需要大量的分配/解除分配)。
每当我的项目需要分配时,它都会查找列表以找到适合所需大小的免费分配。为了实现这一点,我使用了一个简单的 for 循环:
for I := 0 to FAllocationList.Count - 1 do
begin
if MemoryAllocation.FUsed and (MemoryAllocation.FSize = Size) then
...
我的应用程序运行的时间越长,这个列表就会增长到数千个项目,并且当我非常频繁地运行它时(每秒几次),它的速度会大大减慢。
我正在尝试找到一种方法来加速这个解决方案。我考虑过按分配大小对 TList 进行排序。如果我这样做,我应该使用某种智能方式来访问每次调用时所需的特定大小的列表。 有一些简单的方法可以做到这一点吗?
我正在考虑的另一种方法是有两个 TList。一份用于未使用的分配,一份用于已使用的分配。这意味着我必须从一个列表中提取 TList.Items 并始终添加到另一个列表中。我仍然需要使用 for 循环来浏览(现在)较小的列表。 这是正确的方法吗?
也非常欢迎其他建议!
My project (running on Delphi 6!) requires a list of memory allocations (TMemoryAllocation) which I store within an object that also holds the information about the size of the allocation (FSize) and if the allocation is in use or free (FUsed). I use this basically as a GarbageCollector and a way to keep my application of allocating/deallocating memory all the time (and there are plenty allocations/deallocations needed).
Whenever my project needs an allocation it looks up the list to find a free allocation that fits the required size. To achieve that I use a simple for loop:
for I := 0 to FAllocationList.Count - 1 do
begin
if MemoryAllocation.FUsed and (MemoryAllocation.FSize = Size) then
...
The longer my application runs this list grows to several thousand items and it slows down considerably as I run it up very frequently (several times per second).
I am trying to find a way to accelerate this solution. I thought about sorting the TList by size of allocation. If I do that I should use some intelligent way of accessing the list for the particular size I need at every call. Is there some easy way to do this?
Another way I was thinking about was to have two TLists. One for the Unused and one of the Used allocations. That would mean though that I would have to Extract TList.Items from one list and add to the other all the time. And I would still need to use a for-loop to go through the (now) smaller list. Would this be the right way?
Other suggestions are VERY welcome as well!
发布评论
评论(1)
您有几种可能性:
如果您的应用程序对内存分配非常敏感,也许有一些关于重新发明轮子的反馈:
这绝对不是那么简单,因此您可能需要先查看一些现有代码,或者只是依赖现有的库。我认为您不必在应用程序中编写此内存分配代码,除非 FastMM4 对您来说不够快,对此我非常怀疑,因为这是一段很棒的代码!
You have several possibilities:
If your application is very sensitive to memory allocation, perhaps some feedback about reinventing the wheel:
It's definitively not so simple, so you may want to look at some existing code before, or just rely on existing libraries. I think you don't have to code this memory allocation in your application, unless FastMM4 is not fast enough for you, which I'll doubt very much, because this is a great piece of code!