public inbox for ell@lists.linux.dev
 help / color / mirror / Atom feed
From: Denis Kenzior <denkenz@gmail.com>
To: ell@lists.linux.dev
Cc: Denis Kenzior <denkenz@gmail.com>
Subject: [PATCH 04/12] string: Use conservative allocation growth strategy
Date: Mon, 22 Jul 2024 14:04:21 -0500	[thread overview]
Message-ID: <20240722190443.43196-4-denkenz@gmail.com> (raw)
In-Reply-To: <20240722190443.43196-1-denkenz@gmail.com>

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 --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);
 }
 
-- 
2.45.2


  parent reply	other threads:[~2024-07-22 19:04 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-22 19:04 [PATCH 01/12] useful: Add utility to find the next power of two Denis Kenzior
2024-07-22 19:04 ` [PATCH 02/12] unit: Add unit test for roundup_pow_of_two Denis Kenzior
2024-07-22 19:04 ` [PATCH 03/12] util: Introduce l_util_pagesize Denis Kenzior
2024-07-22 19:04 ` Denis Kenzior [this message]
2024-07-22 19:04 ` [PATCH 05/12] unit: Refactor genl-msg tests Denis Kenzior
2024-07-22 19:04 ` [PATCH 06/12] netlink: Add l_netlink_message class Denis Kenzior
2024-07-22 19:04 ` [PATCH 07/12] netlink: support appending using scatter / gather Denis Kenzior
2024-07-22 19:04 ` [PATCH 08/12] netlink: Support adding nested attributes to messages Denis Kenzior
2024-07-22 19:04 ` [PATCH 09/12] netlink: Add additional helpers Denis Kenzior
2024-07-22 19:04 ` [PATCH 10/12] unit: Add netlink message builder / parser tests Denis Kenzior
2024-07-22 19:04 ` [PATCH 11/12] netlink: add netlink_message_from_nlmsg Denis Kenzior
2024-07-22 19:04 ` [PATCH 12/12] genl: Use l_netlink_message as implementation for l_genl_msg Denis Kenzior
2024-07-23 23:51 ` [PATCH 01/12] useful: Add utility to find the next power of two Denis Kenzior

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240722190443.43196-4-denkenz@gmail.com \
    --to=denkenz@gmail.com \
    --cc=ell@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox