Home  >  Article  >  System Tutorial  >  Optimize curl's memory allocation operation

Optimize curl's memory allocation operation

王林
王林forward
2023-12-27 14:23:32894browse

Today I made another small change inside libcurl[1] to make it do less malloc. This time, generic linked list functions are converted into less mallocs (that's how linked list functions should be, really).

Research malloc

A few weeks ago I started looking into memory allocation. This is easy because we've had memory debugging and logging systems in curl for years. Use the debug version of curl and run this script in my build directory:

#!/bin/sh
export CURL_MEMDEBUG=$HOME/tmp/curlmem.log
./src/curl http://localhost
./tests/memanalyze.pl -v $HOME/tmp/curlmem.log

For curl 7.53.1, this is approximately 115 memory allocations. Is this too much or too little?

Memory logging is very basic. To give you an idea, here's a sample snippet:

MEM getinfo.c:70 free((nil))
MEM getinfo.c:73 free((nil))
MEM url.c:294 free((nil))
MEM url.c:297 strdup(0x559e7150d616) (24) = 0x559e73760f98
MEM url.c:294 free((nil))
MEM url.c:297 strdup(0x559e7150d62e) (22) = 0x559e73760fc8
MEM multi.c:302 calloc(1,480) = 0x559e73760ff8
MEM hash.c:75 malloc(224) = 0x559e737611f8
MEM hash.c:75 malloc(29152) = 0x559e737a2bc8
MEM hash.c:75 malloc(3104) = 0x559e737a9dc8
Check log

Then I dug deeper into the logs and I realized that many small memory allocations were made on the same lines of code. We obviously have some pretty stupid code patterns where we allocate a struct, then add that struct to a linked list or hash, and then that code then adds another little struct, and so on, often doing it in a loop. (What I say here is we, not to blame anyone, of course, most of the responsibility lies with myself...)

These two allocation operations will always appear in pairs and be released at the same time. I decided to solve these problems. Doing very small (less than 32 bytes) allocations is also wasteful, since a lot of data will be used (within the malloc system) to keep track of that tiny area of ​​memory. Not to mention the pile of debris.

So fixing that hash and linked list code to not use malloc is a quick and easy way to eliminate over 20% of mallocs for the simplest "curl http://localhost" transfer.

At this point, I sort all memory allocation operations by size and check all the smallest allocation operations. A prominent part is in curl_multi_wait(), which is a function that is typically called repeatedly in the main curl transfer loop. For most typical cases I convert this to using stack [2]. It's a good thing to avoid malloc in a lot of repeated function calls.

Recount

Now, as shown in the script above, the same curl localhost command dropped from 115 allocation operations with curl 7.53.1 to 80 allocation operations without sacrificing anything. Easily a 26% improvement. Not bad at all!

Since I modified curl_multi_wait(), I also wanted to see how it actually improved some slightly more advanced transfers. I used the multi-double.c[3] example code, added a call to initialize the memory record, had it use curl_multi_wait(), and downloaded the two URLs in parallel:

http://www.example.com/
http://localhost/512M

The second file is 512 megabytes of zeros, the first file is a 600 byte public html page. This is count-malloc.c code [4].

First, I used 7.53.1 to test the above example and checked using the memanalyze script:

Mallocs: 33901
Reallocs: 5
Callocs: 24
Strdups: 31
Wcsdups: 0
Frees: 33956
Allocations: 33961
Maximum allocated: 160385

Okay, so it used a total of 160KB of memory and allocated over 33900 times. And it downloads over 512 megabytes of data, so it has a malloc for every 15KB of data. Is it good or bad?

Back to git master, it is now version 7.54.1-DEV - since we are not quite sure which version number it will be when we release the next version. It could be 7.54.1 or 7.55.0, it's not confirmed yet. I digress, I ran the same modified multi-double.c example again, ran memanalyze on the memory log again, and here comes the report:

Mallocs: 69
Reallocs: 5
Callocs: 24
Strdups: 31
Wcsdups: 0
Frees: 124
Allocations: 129
Maximum allocated: 153247

I watched it twice in disbelief. What happened? To double check, I'd better run it again. No matter how many times I run it, the result is still the same.

33961 vs 129

In a typical transfercurl_multi_wait() is called many times, and at least one normal memory allocation operation is performed during the transfer, so removing that single tiny allocation operation has a very large impact on the counter Impact. Normal transfers also do some moving data in and out of linked lists and hashing operations, but they are mostly malloc-free now as well. Simply put: the remaining allocation operations are not performed in the transfer loop, so they are of little importance.

The previous curl allocated 263 times the number of operations as the current example. In other words: the new one is 0.37% of the number of allocation operations of the old one.

另外还有一点好处,新的内存分配量更少,总共减少了 7KB(4.3%)。

malloc 重要吗?

在几个 G 内存的时代里,在传输中有几个 malloc 真的对于普通人有显著的区别吗?对 512MB 数据进行的 33832 个额外的 malloc 有什么影响?

为了衡量这些变化的影响,我决定比较 localhost 的 HTTP 传输,看看是否可以看到任何速度差异。localhost 对于这个测试是很好的,因为没有网络速度限制,更快的 curl 下载也越快。服务器端也会相同的快/慢,因为我将使用相同的测试集进行这两个测试。

我相同方式构建了 curl 7.53.1 和 curl 7.54.1-DEV,并运行这个命令:

curl http://localhost/80GB -o /dev/null

下载的 80GB 的数据会尽可能快地写到空设备中。

我获得的确切数字可能不是很有用,因为它将取决于机器中的 CPU、使用的 HTTP 服务器、构建 curl 时的优化级别等,但是相对数字仍然应该是高度相关的。新代码对决旧代码!

7.54.1-DEV 反复地表现出更快 30%!我的早期版本是 2200MB/秒增加到当前版本的超过 2900 MB/秒。

这里的要点当然不是说它很容易在我的机器上使用单一内核以超过 20GB/秒的速度来进行 HTTP 传输,因为实际上很少有用户可以通过 curl 做到这样快速的传输。关键在于 curl 现在每个字节的传输使用更少的 CPU,这将使更多的 CPU 转移到系统的其余部分来执行任何需要做的事情。或者如果设备是便携式设备,那么可以省电。

关于 malloc 的成本:512MB 测试中,我使用旧代码发生了 33832 次或更多的分配。旧代码以大约 2200MB/秒的速率进行 HTTP 传输。这等于每秒 145827 次 malloc - 现在它们被消除了!600 MB/秒的改进意味着每秒钟 curl 中每个减少的 malloc 操作能额外换来多传输 4300 字节。

去掉这些 malloc 难吗?

一点也不难,非常简单。然而,有趣的是,在这个旧项目中,仍然有这样的改进空间。我有这个想法已经好几年了,我很高兴我终于花点时间来实现。感谢我们的测试套件,我可以有相当大的信心做这个“激烈的”内部变化,而不会引入太可怕的回归问题。由于我们的 API 很好地隐藏了内部,所以这种变化可以完全不改变任何旧的或新的应用程序……

(是的,我还没在版本中发布该变更,所以这还有风险,我有点后悔我的“这很容易”的声明……)

注意数字

curl 的 git 仓库从 7.53.1 到今天已经有 213 个提交。即使我没有别的想法,可能还会有一次或多次的提交,而不仅仅是内存分配对性能的影响。

还有吗?

还有其他类似的情况么?

也许。我们不会做很多性能测量或比较,所以谁知道呢,我们也许会做更多的愚蠢事情,我们可以收手并做得更好。有一个事情是我一直想做,但是从来没有做,就是添加所使用的内存/malloc 和 curl 执行速度的每日“监视” ,以便更好地跟踪我们在这些方面不知不觉的回归问题。

补遗,4/23

(关于我在 hacker news、Reddit 和其它地方读到的关于这篇文章的评论)

有些人让我再次运行那个 80GB 的下载,给出时间。我运行了三次新代码和旧代码,其运行“中值”如下:

旧代码:

real    0m36.705s
user    0m20.176s
sys     0m16.072s

新代码:

real    0m29.032s
user    0m12.196s
sys     0m12.820s

承载这个 80GB 文件的服务器是标准的 Apache 2.4.25,文件存储在 SSD 上,我的机器的 CPU 是 i7 3770K 3.50GHz 。

有些人也提到 alloca() 作为该补丁之一也是个解决方案,但是 alloca() 移植性不够,只能作为一个孤立的解决方案,这意味着如果我们要使用它的话,需要写一堆丑陋的 #ifdef


The above is the detailed content of Optimize curl's memory allocation operation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:linuxprobe.com. If there is any infringement, please contact admin@php.cn delete