improve memory usage and allocation - webdump - HTML to plain-text converter for webpages
(HTM) git clone git://git.codemadness.org/webdump
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit 5cde25b5150bd0375e9b5800bf3855765830c588
(DIR) parent 72b23084b7c64c298c6b90ae6ad9f53f497cec57
(HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Sat, 6 Jul 2024 13:05:54 +0200
improve memory usage and allocation
Do not realloc when it is not needed (even when it is the same size).
Decrease the greedy allocation increment size for nested nodes also.
Tested for example using valgrind and "add Beej's Guide to Network Programming" HTML page:
https://git.codemadness.org/webdump_tests/commit/837749abc02f28e1584e5f2cf2b274ae1c69d8e6.html
The buffering for link references (-l option) used way too much memory.
Diffstat:
M webdump.c | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)
---
(DIR) diff --git a/webdump.c b/webdump.c
@@ -1420,15 +1420,17 @@ addlinkref(const char *url, const char *_type, enum TagId tagid, int ishidden)
if (!ishidden) {
linknr = ++nvisrefs;
- if (nvisrefs >= ncapvisrefs)
+ if (nvisrefs >= ncapvisrefs) {
ncapvisrefs += 256; /* greedy alloc */
- visrefs = erealloc(visrefs, sizeof(*visrefs) * ncapvisrefs);
+ visrefs = erealloc(visrefs, sizeof(*visrefs) * ncapvisrefs);
+ }
visrefs[linknr - 1] = link; /* add pointer to list */
} else {
linknr = ++nhiddenrefs;
- if (nhiddenrefs >= ncaphiddenrefs)
+ if (nhiddenrefs >= ncaphiddenrefs) {
ncaphiddenrefs += 256; /* greedy alloc */
- hiddenrefs = erealloc(hiddenrefs, sizeof(*hiddenrefs) * ncaphiddenrefs);
+ hiddenrefs = erealloc(hiddenrefs, sizeof(*hiddenrefs) * ncaphiddenrefs);
+ }
hiddenrefs[linknr - 1] = link; /* add pointer to list */
}
@@ -1534,7 +1536,7 @@ printlinkrefs(void)
}
/* size to grow node capacity (greedy) */
-#define NODE_CAP_INC 256
+#define NODE_CAP_INC 16
/* increase node depth, allocate space for nodes if needed */
static void