diff mbox series

[04/12] string: Use conservative allocation growth strategy

Message ID 20240722190443.43196-4-denkenz@gmail.com (mailing list archive)
State New
Headers show
Series [01/12] useful: Add utility to find the next power of two | expand

Checks

Context Check Description
tedd_an/pre-ci_am success Success

Commit Message

Denis Kenzior July 22, 2024, 7:04 p.m. UTC
Instead of always growing exponentially, only request enough pages to
hold the new string.  It is unlikely that ell based applications will be
processing large strings, so be more conservative with memory use.
---
 ell/string.c | 21 +++++++--------------
 1 file changed, 7 insertions(+), 14 deletions(-)
diff mbox series

Patch

diff --git a/ell/string.c b/ell/string.c
index a6d359d20cc9..e8e45beacb92 100644
--- a/ell/string.c
+++ b/ell/string.c
@@ -34,25 +34,18 @@  struct l_string {
 	char *str;
 };
 
-static inline size_t next_power(size_t len)
-{
-	size_t n = 1;
-
-	if (len > SIZE_MAX / 2)
-		return SIZE_MAX;
-
-	while (n < len)
-		n = n << 1;
-
-	return n;
-}
-
 static void grow_string(struct l_string *str, size_t extra)
 {
 	if (str->len + extra < str->max)
 		return;
 
-	str->max = next_power(str->len + extra + 1);
+	str->max = str->len + extra + 1;
+
+	if (str->max < l_util_pagesize())
+		str->max = roundup_pow_of_two(str->max);
+	else
+		str->max = align_len(str->max, l_util_pagesize());
+
 	str->str = l_realloc(str->str, str->max);
 }