{"id":83588,"date":"2023-11-26T06:46:10","date_gmt":"2023-11-25T22:46:10","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=83588"},"modified":"2023-11-26T06:46:10","modified_gmt":"2023-11-25T22:46:10","slug":"fewer-mallocs-in-curl","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/fewer-mallocs-in-curl.html","title":{"rendered":"curl \u4e2d\u51cf\u5c11\u5185\u5b58\u5206\u914d\u64cd\u4f5c"},"content":{"rendered":"

\u4eca\u5929\u6211\u5728 libcurl \u5185\u90e8\u53c8\u505a\u4e86\u4e00\u4e2a\u5c0f\u6539\u52a8<\/a>[1]<\/span>\uff0c\u4f7f\u5176\u505a\u66f4\u5c11\u7684 malloc\u3002\u8fd9\u4e00\u6b21\uff0c\u6cdb\u578b\u94fe\u8868\u51fd\u6570\u88ab\u8f6c\u6362\u6210\u66f4\u5c11\u7684 malloc (\u8fd9\u624d\u662f\u94fe\u8868\u51fd\u6570\u5e94\u6709\u7684\u65b9\u5f0f\uff0c\u771f\u7684)\u3002<\/p>\n

\u7814\u7a76 malloc<\/strong><\/div>\n

\u51e0\u5468\u524d\u6211\u5f00\u59cb\u7814\u7a76\u5185\u5b58\u5206\u914d\u3002\u8fd9\u5f88\u5bb9\u6613\uff0c\u56e0\u4e3a\u591a\u5e74\u524d\u6211\u4eec curl \u4e2d\u5c31\u5df2\u7ecf\u6709\u5185\u5b58\u8c03\u8bd5\u548c\u65e5\u5fd7\u8bb0\u5f55\u7cfb\u7edf\u4e86\u3002\u4f7f\u7528 curl \u7684\u8c03\u8bd5\u7248\u672c\uff0c\u5e76\u5728\u6211\u7684\u6784\u5efa\u76ee\u5f55\u4e2d\u8fd0\u884c\u6b64\u811a\u672c\uff1a<\/p>\n

#!\/bin\/sh\r\nexport CURL_MEMDEBUG=$HOME\/tmp\/curlmem.log\r\n.\/src\/curl http:\/\/localhost\r\n.\/tests\/memanalyze.pl -v $HOME\/tmp\/curlmem.log\r\n<\/pre>\n

\u5bf9\u4e8e curl 7.53.1\uff0c\u8fd9\u5927\u7ea6\u6709 115 \u6b21\u5185\u5b58\u5206\u914d\u3002\u8fd9\u7b97\u591a\u8fd8\u662f\u5c11\uff1f<\/p>\n

\u5185\u5b58\u65e5\u5fd7\u975e\u5e38\u57fa\u7840\u3002\u4e3a\u4e86\u8ba9\u4f60\u6709\u6240\u4e86\u89e3\uff0c\u8fd9\u662f\u4e00\u4e2a\u793a\u4f8b\u7247\u6bb5\uff1a<\/p>\n

MEM getinfo.c:70 free((nil))\r\nMEM getinfo.c:73 free((nil))\r\nMEM url.c:294 free((nil))\r\nMEM url.c:297 strdup(0x559e7150d616) (24) = 0x559e73760f98\r\nMEM url.c:294 free((nil))\r\nMEM url.c:297 strdup(0x559e7150d62e) (22) = 0x559e73760fc8\r\nMEM multi.c:302 calloc(1,480) = 0x559e73760ff8\r\nMEM hash.c:75 malloc(224) = 0x559e737611f8\r\nMEM hash.c:75 malloc(29152) = 0x559e737a2bc8\r\nMEM hash.c:75 malloc(3104) = 0x559e737a9dc8\r\n<\/pre>\n
\u68c0\u67e5\u65e5\u5fd7<\/strong><\/div>\n

\u7136\u540e\uff0c\u6211\u5bf9\u65e5\u5fd7\u8fdb\u884c\u4e86\u66f4\u6df1\u5165\u7684\u7814\u7a76\uff0c\u6211\u610f\u8bc6\u5230\u5728\u76f8\u540c\u7684\u4ee3\u7801\u884c\u505a\u4e86\u8bb8\u591a\u5c0f\u5185\u5b58\u5206\u914d\u3002\u6211\u4eec\u663e\u7136\u6709\u4e00\u4e9b\u76f8\u5f53\u611a\u8822\u7684\u4ee3\u7801\u6a21\u5f0f\uff0c\u6211\u4eec\u5206\u914d\u4e00\u4e2a\u7ed3\u6784\u4f53\uff0c\u7136\u540e\u5c06\u8be5\u7ed3\u6784\u6dfb\u52a0\u5230\u94fe\u8868\u6216\u54c8\u5e0c\uff0c\u7136\u540e\u8be5\u4ee3\u7801\u968f\u540e\u518d\u6dfb\u52a0\u53e6\u4e00\u4e2a\u5c0f\u7ed3\u6784\u4f53\uff0c\u5982\u6b64\u8fd9\u822c\uff0c\u800c\u4e14\u7ecf\u5e38\u5728\u5faa\u73af\u4e2d\u6267\u884c\u3002\uff08\u6211\u5728\u8fd9\u91cc\u8bf4\u7684\u662f\u6211\u4eec<\/em>\uff0c\u4e0d\u662f\u4e3a\u4e86\u8d23\u602a\u67d0\u4e2a\u4eba\uff0c\u5f53\u7136\u5927\u90e8\u5206\u7684\u8d23\u4efb\u662f\u6211\u81ea\u5df1\u2026\u2026\uff09<\/p>\n

\u8fd9\u4e24\u79cd\u5206\u914d\u64cd\u4f5c\u5c06\u603b\u662f\u6210\u5bf9\u5730\u51fa\u73b0\uff0c\u5e76\u88ab\u540c\u65f6\u91ca\u653e\u3002\u6211\u51b3\u5b9a\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u3002\u505a\u975e\u5e38\u5c0f\u7684\uff08\u5c0f\u4e8e 32 \u5b57\u8282\uff09\u7684\u5206\u914d\u4e5f\u662f\u6d6a\u8d39\u7684\uff0c\u56e0\u4e3a\u975e\u5e38\u591a\u7684\u6570\u636e\u5c06\u88ab\u7528\u4e8e\uff08\u5728 malloc \u7cfb\u7edf\u5185\uff09\u8ddf\u8e2a\u90a3\u4e2a\u5fae\u5c0f\u7684\u5185\u5b58\u533a\u57df\u3002\u66f4\u4e0d\u7528\u8bf4\u5806\u788e\u7247\u4e86\u3002<\/p>\n

\u56e0\u6b64\uff0c\u5c06\u8be5\u54c8\u5e0c\u548c\u94fe\u8868\u4ee3\u7801\u4fee\u590d\u4e3a\u4e0d\u4f7f\u7528 malloc \u662f\u5feb\u901f\u4e14\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u5bf9\u4e8e\u6700\u7b80\u5355\u7684 \u201ccurl http:\/\/localhost\u201d \u4f20\u8f93\uff0c\u5b83\u53ef\u4ee5\u6d88\u9664 20\uff05 \u4ee5\u4e0a\u7684 malloc\u3002<\/p>\n

\u6b64\u65f6\uff0c\u6211\u6839\u636e\u5927\u5c0f\u5bf9\u6240\u6709\u7684\u5185\u5b58\u5206\u914d\u64cd\u4f5c\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u68c0\u67e5\u6240\u6709\u6700\u5c0f\u7684\u5206\u914d\u64cd\u4f5c\u3002\u4e00\u4e2a\u7a81\u51fa\u7684\u90e8\u5206\u662f\u5728curl_multi_wait()<\/span> \u4e2d\uff0c\u5b83\u662f\u4e00\u4e2a\u5178\u578b\u7684\u5728 curl \u4f20\u8f93\u4e3b\u5faa\u73af\u4e2d\u88ab\u53cd\u590d\u8c03\u7528\u7684\u51fd\u6570\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u5178\u578b\u60c5\u51b5\uff0c\u6211\u5c06\u5176\u8f6c\u6362\u4e3a\u4f7f\u7528\u5806\u6808<\/a>[2]<\/span>\u3002\u5728\u5927\u91cf\u91cd\u590d\u7684\u8c03\u7528\u51fd\u6570\u4e2d\u907f\u514d malloc \u662f\u4e00\u4ef6\u597d\u4e8b\u3002<\/p>\n

\u91cd\u65b0\u8ba1\u6570<\/strong><\/div>\n

\u73b0\u5728\uff0c\u5982\u4e0a\u9762\u7684\u811a\u672c\u6240\u793a\uff0c\u540c\u6837\u7684 curl localhost<\/span> \u547d\u4ee4\u4ece curl 7.53.1 \u7684 115 \u6b21\u5206\u914d\u64cd\u4f5c\u4e0b\u964d\u5230 80 \u4e2a\u5206\u914d\u64cd\u4f5c\uff0c\u800c\u6ca1\u6709\u727a\u7272\u4efb\u4f55\u4e1c\u897f\u3002\u8f7b\u677e\u5730\u6709 26\uff05 \u7684\u6539\u5584\u3002\u4e00\u70b9\u4e5f\u4e0d\u5dee\uff01<\/p>\n

\u7531\u4e8e\u6211\u4fee\u6539\u4e86 curl_multi_wait()<\/span>\uff0c\u6211\u4e5f\u60f3\u770b\u770b\u5b83\u5b9e\u9645\u4e0a\u662f\u5982\u4f55\u6539\u8fdb\u4e00\u4e9b\u7a0d\u5fae\u66f4\u9ad8\u7ea7\u4e00\u4e9b\u7684\u4f20\u8f93\u3002\u6211\u4f7f\u7528\u4e86 multi-double.c<\/a>[3]<\/span> \u793a\u4f8b\u4ee3\u7801\uff0c\u6dfb\u52a0\u4e86\u521d\u59cb\u5316\u5185\u5b58\u8bb0\u5f55\u7684\u8c03\u7528\uff0c\u8ba9\u5b83\u4f7f\u7528 curl_multi_wait()<\/span>\uff0c\u5e76\u4e14\u5e76\u884c\u4e0b\u8f7d\u4e86\u8fd9\u4e24\u4e2a URL\uff1a<\/p>\n

http:\/\/www.example.com\/\r\nhttp:\/\/localhost\/512M\r\n<\/pre>\n

\u7b2c\u4e8c\u4e2a\u6587\u4ef6\u662f 512 \u5146\u5b57\u8282\u7684\u96f6\uff0c\u7b2c\u4e00\u4e2a\u6587\u4ef6\u662f\u4e00\u4e2a 600 \u5b57\u8282\u7684\u516c\u5171 html \u9875\u9762\u3002\u8fd9\u662f count-malloc.c \u4ee3\u7801<\/a>[4]<\/span>\u3002<\/p>\n

\u9996\u5148\uff0c\u6211\u4f7f\u7528 7.53.1 \u6765\u6d4b\u8bd5\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u5e76\u4f7f\u7528 memanalyze<\/span> \u811a\u672c\u68c0\u67e5\uff1a<\/p>\n

Mallocs: 33901\r\nReallocs: 5\r\nCallocs: 24\r\nStrdups: 31\r\nWcsdups: 0\r\nFrees: 33956\r\nAllocations: 33961\r\nMaximum allocated: 160385\r\n<\/pre>\n

\u597d\u4e86\uff0c\u6240\u4ee5\u5b83\u603b\u5171\u4f7f\u7528\u4e86 160KB \u7684\u5185\u5b58\uff0c\u5206\u914d\u64cd\u4f5c\u6b21\u6570\u8d85\u8fc7 33900 \u6b21\u3002\u800c\u5b83\u4e0b\u8f7d\u8d85\u8fc7 512 \u5146\u5b57\u8282\u7684\u6570\u636e\uff0c\u6240\u4ee5\u5b83\u6bcf 15KB \u6570\u636e\u6709\u4e00\u6b21 malloc\u3002\u662f\u597d\u662f\u574f\uff1f<\/p>\n

\u56de\u5230 git master\uff0c\u73b0\u5728\u662f 7.54.1-DEV \u7684\u7248\u672c - \u56e0\u4e3a\u6211\u4eec\u4e0d\u592a\u786e\u5b9a\u5f53\u6211\u4eec\u53d1\u5e03\u4e0b\u4e00\u4e2a\u7248\u672c\u65f6\u4f1a\u53d8\u6210\u54ea\u4e2a\u7248\u672c\u53f7\u3002\u5b83\u53ef\u80fd\u662f 7.54.1 \u6216 7.55.0\uff0c\u5b83\u8fd8\u5c1a\u672a\u786e\u5b9a\u3002\u6211\u79bb\u9898\u4e86\uff0c\u6211\u518d\u6b21\u8fd0\u884c\u76f8\u540c\u4fee\u6539\u7684 multi-double.c \u793a\u4f8b\uff0c\u518d\u6b21\u5bf9\u5185\u5b58\u65e5\u5fd7\u8fd0\u884c memanalyze\uff0c\u62a5\u544a\u6765\u4e86\uff1a<\/p>\n

Mallocs: 69\r\nReallocs: 5\r\nCallocs: 24\r\nStrdups: 31\r\nWcsdups: 0\r\nFrees: 124\r\nAllocations: 129\r\nMaximum allocated: 153247\r\n<\/pre>\n

\u6211\u4e0d\u6562\u7f6e\u4fe1\u5730\u53cd\u590d\u770b\u4e86\u4e24\u904d\u3002\u53d1\u751f\u4ec0\u4e48\u4e86\u5417\uff1f\u4e3a\u4e86\u4ed4\u7ec6\u68c0\u67e5\uff0c\u6211\u6700\u597d\u518d\u8fd0\u884c\u4e00\u6b21\u3002\u65e0\u8bba\u6211\u8fd0\u884c\u591a\u5c11\u6b21\uff0c\u7ed3\u679c\u8fd8\u662f\u4e00\u6837\u7684\u3002<\/p>\n

33961 vs 129<\/strong><\/div>\n

\u5728\u5178\u578b\u7684\u4f20\u8f93\u4e2d curl_multi_wait()<\/span> \u88ab\u8c03\u7528\u4e86\u5f88\u591a\u6b21\uff0c\u5e76\u4e14\u5728\u4f20\u8f93\u8fc7\u7a0b\u4e2d\u81f3\u5c11\u8981\u6b63\u5e38\u8fdb\u884c\u4e00\u6b21\u5185\u5b58\u5206\u914d\u64cd\u4f5c\uff0c\u56e0\u6b64\u5220\u9664\u90a3\u4e2a\u5355\u4e00\u7684\u5fae\u5c0f\u5206\u914d\u64cd\u4f5c\u5bf9\u8ba1\u6570\u5668\u6709\u975e\u5e38\u5927\u7684\u5f71\u54cd\u3002\u6b63\u5e38\u7684\u4f20\u8f93\u4e5f\u4f1a\u505a\u4e00\u4e9b\u5c06\u6570\u636e\u79fb\u5165\u6216\u79fb\u51fa\u94fe\u8868\u548c\u6563\u5217\u64cd\u4f5c\uff0c\u4f46\u662f\u5b83\u4eec\u73b0\u5728\u4e5f\u5927\u90fd\u662f\u65e0 malloc \u7684\u3002\u7b80\u5355\u5730\u8bf4\uff1a\u5269\u4f59\u7684\u5206\u914d\u64cd\u4f5c\u4e0d\u4f1a\u5728\u4f20\u8f93\u5faa\u73af\u4e2d\u6267\u884c\uff0c\u6240\u4ee5\u5b83\u4eec\u7684\u91cd\u8981\u6027\u4e0d\u5927\u3002<\/p>\n

\u4ee5\u524d\u7684 curl \u662f\u5f53\u524d\u793a\u4f8b\u5206\u914d\u64cd\u4f5c\u6570\u91cf\u7684 263 \u500d\u3002\u6362\u53e5\u8bdd\u8bf4\uff1a\u65b0\u7684\u662f\u65e7\u7684\u5206\u914d\u64cd\u4f5c\u6570\u91cf\u7684 0.37\uff05 \u3002<\/p>\n

\u53e6\u5916\u8fd8\u6709\u4e00\u70b9\u597d\u5904\uff0c\u65b0\u7684\u5185\u5b58\u5206\u914d\u91cf\u66f4\u5c11\uff0c\u603b\u5171\u51cf\u5c11\u4e86 7KB\uff084.3\uff05\uff09\u3002<\/p>\n

malloc \u91cd\u8981\u5417\uff1f<\/strong><\/div>\n

\u5728\u51e0\u4e2a G \u5185\u5b58\u7684\u65f6\u4ee3\u91cc\uff0c\u5728\u4f20\u8f93\u4e2d\u6709\u51e0\u4e2a malloc \u771f\u7684\u5bf9\u4e8e\u666e\u901a\u4eba\u6709\u663e\u8457\u7684\u533a\u522b\u5417\uff1f\u5bf9 512MB \u6570\u636e\u8fdb\u884c\u7684 33832 \u4e2a\u989d\u5916\u7684 malloc \u6709\u4ec0\u4e48\u5f71\u54cd\uff1f<\/p>\n

\u4e3a\u4e86\u8861\u91cf\u8fd9\u4e9b\u53d8\u5316\u7684\u5f71\u54cd\uff0c\u6211\u51b3\u5b9a\u6bd4\u8f83 localhost \u7684 HTTP \u4f20\u8f93\uff0c\u770b\u770b\u662f\u5426\u53ef\u4ee5\u770b\u5230\u4efb\u4f55\u901f\u5ea6\u5dee\u5f02\u3002localhost \u5bf9\u4e8e\u8fd9\u4e2a\u6d4b\u8bd5\u662f\u5f88\u597d\u7684\uff0c\u56e0\u4e3a\u6ca1\u6709\u7f51\u7edc\u901f\u5ea6\u9650\u5236\uff0c\u66f4\u5feb\u7684 curl \u4e0b\u8f7d\u4e5f\u8d8a\u5feb\u3002\u670d\u52a1\u5668\u7aef\u4e5f\u4f1a\u76f8\u540c\u7684\u5feb\/\u6162\uff0c\u56e0\u4e3a\u6211\u5c06\u4f7f\u7528\u76f8\u540c\u7684\u6d4b\u8bd5\u96c6\u8fdb\u884c\u8fd9\u4e24\u4e2a\u6d4b\u8bd5\u3002<\/p>\n

\u6211\u76f8\u540c\u65b9\u5f0f\u6784\u5efa\u4e86 curl 7.53.1 \u548c curl 7.54.1-DEV\uff0c\u5e76\u8fd0\u884c\u8fd9\u4e2a\u547d\u4ee4\uff1a<\/p>\n

curl http:\/\/localhost\/80GB -o \/dev\/null\r\n<\/pre>\n

\u4e0b\u8f7d\u7684 80GB \u7684\u6570\u636e\u4f1a\u5c3d\u53ef\u80fd\u5feb\u5730\u5199\u5230\u7a7a\u8bbe\u5907\u4e2d\u3002<\/p>\n

\u6211\u83b7\u5f97\u7684\u786e\u5207\u6570\u5b57\u53ef\u80fd\u4e0d\u662f\u5f88\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u5c06\u53d6\u51b3\u4e8e\u673a\u5668\u4e2d\u7684 CPU\u3001\u4f7f\u7528\u7684 HTTP \u670d\u52a1\u5668\u3001\u6784\u5efa curl \u65f6\u7684\u4f18\u5316\u7ea7\u522b\u7b49\uff0c\u4f46\u662f\u76f8\u5bf9\u6570\u5b57\u4ecd\u7136\u5e94\u8be5\u662f\u9ad8\u5ea6\u76f8\u5173\u7684\u3002\u65b0\u4ee3\u7801\u5bf9\u51b3\u65e7\u4ee3\u7801\uff01<\/p>\n

7.54.1-DEV \u53cd\u590d\u5730\u8868\u73b0\u51fa\u66f4\u5feb 30\uff05\uff01\u6211\u7684\u65e9\u671f\u7248\u672c\u662f 2200MB\/\u79d2\u589e\u52a0\u5230\u5f53\u524d\u7248\u672c\u7684\u8d85\u8fc7 2900 MB\/\u79d2\u3002<\/p>\n

\u8fd9\u91cc\u7684\u8981\u70b9\u5f53\u7136\u4e0d\u662f\u8bf4\u5b83\u5f88\u5bb9\u6613\u5728\u6211\u7684\u673a\u5668\u4e0a\u4f7f\u7528\u5355\u4e00\u5185\u6838\u4ee5\u8d85\u8fc7 20GB\/\u79d2\u7684\u901f\u5ea6\u6765\u8fdb\u884c HTTP \u4f20\u8f93\uff0c\u56e0\u4e3a\u5b9e\u9645\u4e0a\u5f88\u5c11\u6709\u7528\u6237\u53ef\u4ee5\u901a\u8fc7 curl \u505a\u5230\u8fd9\u6837\u5feb\u901f\u7684\u4f20\u8f93\u3002\u5173\u952e\u5728\u4e8e curl \u73b0\u5728\u6bcf\u4e2a\u5b57\u8282\u7684\u4f20\u8f93\u4f7f\u7528\u66f4\u5c11\u7684 CPU\uff0c\u8fd9\u5c06\u4f7f\u66f4\u591a\u7684 CPU \u8f6c\u79fb\u5230\u7cfb\u7edf\u7684\u5176\u4f59\u90e8\u5206\u6765\u6267\u884c\u4efb\u4f55\u9700\u8981\u505a\u7684\u4e8b\u60c5\u3002\u6216\u8005\u5982\u679c\u8bbe\u5907\u662f\u4fbf\u643a\u5f0f\u8bbe\u5907\uff0c\u90a3\u4e48\u53ef\u4ee5\u7701\u7535\u3002<\/p>\n

\u5173\u4e8e malloc \u7684\u6210\u672c\uff1a512MB \u6d4b\u8bd5\u4e2d\uff0c\u6211\u4f7f\u7528\u65e7\u4ee3\u7801\u53d1\u751f\u4e86 33832 \u6b21\u6216\u66f4\u591a\u7684\u5206\u914d\u3002\u65e7\u4ee3\u7801\u4ee5\u5927\u7ea6 2200MB\/\u79d2\u7684\u901f\u7387\u8fdb\u884c HTTP \u4f20\u8f93\u3002\u8fd9\u7b49\u4e8e\u6bcf\u79d2 145827 \u6b21 malloc - \u73b0\u5728\u5b83\u4eec\u88ab\u6d88\u9664\u4e86\uff01600 MB\/\u79d2\u7684\u6539\u8fdb\u610f\u5473\u7740\u6bcf\u79d2\u949f curl \u4e2d\u6bcf\u4e2a\u51cf\u5c11\u7684 malloc \u64cd\u4f5c\u80fd\u989d\u5916\u6362\u6765\u591a\u4f20\u8f93 4300 \u5b57\u8282\u3002<\/p>\n

\u53bb\u6389\u8fd9\u4e9b malloc \u96be\u5417\uff1f<\/strong><\/div>\n

\u4e00\u70b9\u4e5f\u4e0d\u96be\uff0c\u975e\u5e38\u7b80\u5355\u3002\u7136\u800c\uff0c\u6709\u8da3\u7684\u662f\uff0c\u5728\u8fd9\u4e2a\u65e7\u9879\u76ee\u4e2d\uff0c\u4ecd\u7136\u6709\u8fd9\u6837\u7684\u6539\u8fdb\u7a7a\u95f4\u3002\u6211\u6709\u8fd9\u4e2a\u60f3\u6cd5\u5df2\u7ecf\u597d\u51e0\u5e74\u4e86\uff0c\u6211\u5f88\u9ad8\u5174\u6211\u7ec8\u4e8e\u82b1\u70b9\u65f6\u95f4\u6765\u5b9e\u73b0\u3002\u611f\u8c22\u6211\u4eec\u7684\u6d4b\u8bd5\u5957\u4ef6\uff0c\u6211\u53ef\u4ee5\u6709\u76f8\u5f53\u5927\u7684\u4fe1\u5fc3\u505a\u8fd9\u4e2a\u201c\u6fc0\u70c8\u7684\u201d\u5185\u90e8\u53d8\u5316\uff0c\u800c\u4e0d\u4f1a\u5f15\u5165\u592a\u53ef\u6015\u7684\u56de\u5f52\u95ee\u9898\u3002\u7531\u4e8e\u6211\u4eec\u7684 API \u5f88\u597d\u5730\u9690\u85cf\u4e86\u5185\u90e8\uff0c\u6240\u4ee5\u8fd9\u79cd\u53d8\u5316\u53ef\u4ee5\u5b8c\u5168\u4e0d\u6539\u53d8\u4efb\u4f55\u65e7\u7684\u6216\u65b0\u7684\u5e94\u7528\u7a0b\u5e8f\u2026\u2026<\/p>\n

\uff08\u662f\u7684\uff0c\u6211\u8fd8\u6ca1\u5728\u7248\u672c\u4e2d\u53d1\u5e03\u8be5\u53d8\u66f4\uff0c\u6240\u4ee5\u8fd9\u8fd8\u6709\u98ce\u9669\uff0c\u6211\u6709\u70b9\u540e\u6094\u6211\u7684\u201c\u8fd9\u5f88\u5bb9\u6613\u201d\u7684\u58f0\u660e\u2026\u2026\uff09<\/p>\n

\u6ce8\u610f\u6570\u5b57<\/strong><\/div>\n

curl \u7684 git \u4ed3\u5e93\u4ece 7.53.1 \u5230\u4eca\u5929\u5df2\u7ecf\u6709 213 \u4e2a\u63d0\u4ea4\u3002\u5373\u4f7f\u6211\u6ca1\u6709\u522b\u7684\u60f3\u6cd5\uff0c\u53ef\u80fd\u8fd8\u4f1a\u6709\u4e00\u6b21\u6216\u591a\u6b21\u7684\u63d0\u4ea4\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u5185\u5b58\u5206\u914d\u5bf9\u6027\u80fd\u7684\u5f71\u54cd\u3002<\/p>\n

\u8fd8\u6709\u5417\uff1f<\/strong><\/div>\n

\u8fd8\u6709\u5176\u4ed6\u7c7b\u4f3c\u7684\u60c5\u51b5\u4e48\uff1f<\/p>\n

\u4e5f\u8bb8\u3002\u6211\u4eec\u4e0d\u4f1a\u505a\u5f88\u591a\u6027\u80fd\u6d4b\u91cf\u6216\u6bd4\u8f83\uff0c\u6240\u4ee5\u8c01\u77e5\u9053\u5462\uff0c\u6211\u4eec\u4e5f\u8bb8\u4f1a\u505a\u66f4\u591a\u7684\u611a\u8822\u4e8b\u60c5\uff0c\u6211\u4eec\u53ef\u4ee5\u6536\u624b\u5e76\u505a\u5f97\u66f4\u597d\u3002\u6709\u4e00\u4e2a\u4e8b\u60c5\u662f\u6211\u4e00\u76f4\u60f3\u505a\uff0c\u4f46\u662f\u4ece\u6765\u6ca1\u6709\u505a\uff0c\u5c31\u662f\u6dfb\u52a0\u6240\u4f7f\u7528\u7684\u5185\u5b58\/malloc \u548c curl \u6267\u884c\u901f\u5ea6\u7684\u6bcf\u65e5\u201c\u76d1\u89c6\u201d \uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u8ddf\u8e2a\u6211\u4eec\u5728\u8fd9\u4e9b\u65b9\u9762\u4e0d\u77e5\u4e0d\u89c9\u7684\u56de\u5f52\u95ee\u9898\u3002<\/p>\n

\u8865\u9057\uff0c4\/23<\/strong><\/div>\n

\uff08\u5173\u4e8e\u6211\u5728 hacker news\u3001Reddit \u548c\u5176\u5b83\u5730\u65b9\u8bfb\u5230\u7684\u5173\u4e8e\u8fd9\u7bc7\u6587\u7ae0\u7684\u8bc4\u8bba\uff09<\/p>\n

\u6709\u4e9b\u4eba\u8ba9\u6211\u518d\u6b21\u8fd0\u884c\u90a3\u4e2a 80GB \u7684\u4e0b\u8f7d\uff0c\u7ed9\u51fa\u65f6\u95f4\u3002\u6211\u8fd0\u884c\u4e86\u4e09\u6b21\u65b0\u4ee3\u7801\u548c\u65e7\u4ee3\u7801\uff0c\u5176\u8fd0\u884c\u201c\u4e2d\u503c\u201d\u5982\u4e0b\uff1a<\/p>\n

\u65e7\u4ee3\u7801\uff1a<\/p>\n

real    0m36.705s\r\nuser    0m20.176s\r\nsys     0m16.072s\r\n<\/pre>\n

\u65b0\u4ee3\u7801\uff1a<\/p>\n

real    0m29.032s\r\nuser    0m12.196s\r\nsys     0m12.820s\r\n<\/pre>\n

\u627f\u8f7d\u8fd9\u4e2a 80GB \u6587\u4ef6\u7684\u670d\u52a1\u5668\u662f\u6807\u51c6\u7684 Apache 2.4.25\uff0c\u6587\u4ef6\u5b58\u50a8\u5728 SSD \u4e0a\uff0c\u6211\u7684\u673a\u5668\u7684 CPU \u662f i7 3770K 3.50GHz \u3002<\/p>\n

\u6709\u4e9b\u4eba\u4e5f\u63d0\u5230 alloca()<\/span> \u4f5c\u4e3a\u8be5\u8865\u4e01\u4e4b\u4e00\u4e5f\u662f\u4e2a\u89e3\u51b3\u65b9\u6848\uff0c\u4f46\u662f alloca()<\/span> \u79fb\u690d\u6027\u4e0d\u591f\uff0c\u53ea\u80fd\u4f5c\u4e3a\u4e00\u4e2a\u5b64\u7acb\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u8fd9\u610f\u5473\u7740\u5982\u679c\u6211\u4eec\u8981\u4f7f\u7528\u5b83\u7684\u8bdd\uff0c\u9700\u8981\u5199\u4e00\u5806\u4e11\u964b\u7684 #ifdef<\/span>\u3002<\/p>\n


\n

via: https:\/\/daniel.haxx.se\/blog\/2017\/04\/22\/fewer-mallocs-in-curl\/<\/a><\/p>\n

\u4f5c\u8005\uff1aDANIEL STENBERG<\/a>[5]<\/span> \u8bd1\u8005\uff1ageekpi<\/a>[6]<\/span> \u6821\u5bf9\uff1awxy<\/a>[7]<\/span><\/p>\n

\u672c\u6587\u7531 LCTT<\/a>[8]<\/span> \u539f\u521b\u7f16\u8bd1\uff0cLinux\u4e2d\u56fd<\/a>[9]<\/span> \u8363\u8a89\u63a8\u51fa<\/p>\n

\n

\u539f\u6587\u6765\u81ea\uff1ahttps:\/\/linux.cn\/article-8870-1.html<\/a><\/p>\n

\u672c\u6587\u5730\u5740\uff1ahttp:\/\/lrxjmw.cn\/fewer-mallocs-in-curl.html<\/a>\u7f16\u8f91\uff1a\u9004\u589e\u5b9d\uff0c\u5ba1\u6838\u5458\uff1a\u5218\u9044<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

\u4eca\u5929\u6211\u5728 libcurl \u5185\u90e8\u53c8\u505a\u4e86\u4e00\u4e2a\u5c0f\u6539\u52a8[1]\uff0c\u4f7f\u5176\u505a\u66f4\u5c11\u7684 malloc\u3002\u8fd9\u4e00\u6b21\uff0c\u6cdb\u578b\u94fe\u8868\u51fd\u6570\u88ab\u8f6c\u6362\u6210 […]<\/p>\n","protected":false},"author":309,"featured_media":83601,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-83588","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/83588","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/309"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=83588"}],"version-history":[{"count":11,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/83588\/revisions"}],"predecessor-version":[{"id":83966,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/83588\/revisions\/83966"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/83601"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=83588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=83588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=83588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}