All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Carlo Marcelo Arenas Belón" <carenas@gmail.com>
To: selinux@vger.kernel.org
Cc: "Carlo Marcelo Arenas Belón" <carenas@gmail.com>
Subject: [PATCH 3/3] libselinux: use a static match_data if single threaded
Date: Mon, 23 Jan 2023 01:40:47 +0000	[thread overview]
Message-ID: <20230123014047.84911-4-carenas@gmail.com> (raw)
In-Reply-To: <20230123014047.84911-1-carenas@gmail.com>

A previous patch reimplemented a single threaded fallback while keeping
the memory usage low as was done originally by the workaround, but the
resulting code is too slow.

To improve performance, while keeping memory in check, use instead a
static match_data that gets reused for all queries and let its contents
(including the heapframes) leak.

Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
---
 libselinux/src/regex.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/libselinux/src/regex.c b/libselinux/src/regex.c
index 54f24026..a6b44023 100644
--- a/libselinux/src/regex.c
+++ b/libselinux/src/regex.c
@@ -220,7 +220,8 @@ void regex_data_free(struct regex_data *regex)
 int regex_match(struct regex_data *regex, char const *subject, int partial)
 {
 	int rc;
-	bool slow;
+	bool single_threaded;
+	static pcre2_match_data *static_match_data;
 	pcre2_match_data *match_data = NULL;
 
 	if (match_data_key_initialized > 0) {
@@ -238,10 +239,13 @@ int regex_match(struct regex_data *regex, char const *subject, int partial)
 			match_data = __selinux_getspecific(match_data_key);
 	}
 
-	slow = (match_data_key_initialized <= 0 || match_data == NULL);
-	if (slow) {
-		match_data = pcre2_match_data_create_from_pattern(regex->regex,
-									NULL);
+	single_threaded = (match_data_key_initialized <= 0 || !match_data);
+	if (single_threaded) {
+		if (!static_match_data && !match_data_initialized)
+			static_match_data = pcre2_match_data_create(1, NULL);
+
+		match_data_initialized = 1;
+		match_data = static_match_data;
 		if (!match_data)
 			return REGEX_ERROR;
 	}
@@ -250,9 +254,6 @@ int regex_match(struct regex_data *regex, char const *subject, int partial)
 	    regex->regex, (PCRE2_SPTR)subject, PCRE2_ZERO_TERMINATED, 0,
 	    partial ? PCRE2_PARTIAL_SOFT : 0, match_data, NULL);
 
-	if (slow)
-		pcre2_match_data_free(match_data);
-
 	if (rc >= 0)
 		return REGEX_MATCH;
 	switch (rc) {
-- 
2.39.0


  parent reply	other threads:[~2023-01-23  1:42 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-21 18:03 [RFC PATCH] libselinux: improve performance with pcre matches Carlo Marcelo Arenas Belón
2023-01-23  1:40 ` [PATCH 0/3] improve performance of " Carlo Marcelo Arenas Belón
2023-01-23  1:40   ` [PATCH 1/3] scripts: respect an initial LD_LIBRARY_PATH with env_use_destdir Carlo Marcelo Arenas Belón
2023-01-23  1:40   ` [PATCH 2/3] libselinux: improve performance with pcre matches Carlo Marcelo Arenas Belón
2023-03-22  4:25     ` Eric Biggers
2023-07-21 17:13       ` Stephen Smalley
2023-01-23  1:40   ` Carlo Marcelo Arenas Belón [this message]
2023-07-21 16:16   ` [PATCH 0/3] improve performance of " Stephen Smalley

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=20230123014047.84911-4-carenas@gmail.com \
    --to=carenas@gmail.com \
    --cc=selinux@vger.kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.