Patchwork [4/6] utlities: kernelscan: make some regex optimisations

login
register
mail settings
Submitter Colin King
Date Nov. 2, 2012, 4:58 p.m.
Message ID <1351875517-19128-5-git-send-email-colin.king@canonical.com>
Download mbox | patch
Permalink /patch/196599/
State Accepted
Headers show

Comments

Colin King - Nov. 2, 2012, 4:58 p.m.
From: Colin Ian King <colin.king@canonical.com>

We only should compile a pattern if it is a regular expression. Also,
make the klog_find a little faster.

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 src/utilities/kernelscan.c |   20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
Keng-Yu Lin - Nov. 7, 2012, 2:19 a.m.
On Sat, Nov 3, 2012 at 12:58 AM, Colin King <colin.king@canonical.com> wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> We only should compile a pattern if it is a regular expression. Also,
> make the klog_find a little faster.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  src/utilities/kernelscan.c |   20 +++++++++++---------
>  1 file changed, 11 insertions(+), 9 deletions(-)
>
> diff --git a/src/utilities/kernelscan.c b/src/utilities/kernelscan.c
> index 9133ce9..2e0d606 100644
> --- a/src/utilities/kernelscan.c
> +++ b/src/utilities/kernelscan.c
> @@ -293,14 +293,17 @@ static klog_pattern *klog_load(const char *table)
>                         exit(EXIT_FAILURE);
>                 }
>
> -               if ((patterns[i].re = pcre_compile(patterns[i].pattern, 0, &error, &erroffset, NULL)) == NULL) {
> -                       fprintf(stderr, "Regex %s failed to compile: %s.\n", patterns[i].pattern, error);
> -                       patterns[i].re = NULL;
> -               } else {
> -                       patterns[i].extra = pcre_study(patterns[i].re, 0, &error);
> -                       if (error != NULL) {
> -                               fprintf(stderr, "Regex %s failed to optimize: %s.\n", patterns[i].pattern, error);
> +               /* Pre-compile regular expressions to make things run a bit faster */
> +               if (patterns[i].cm == COMPARE_REGEX) {
> +                       if ((patterns[i].re = pcre_compile(patterns[i].pattern, 0, &error, &erroffset, NULL)) == NULL) {
> +                               fprintf(stderr, "Regex %s failed to compile: %s.\n", patterns[i].pattern, error);
>                                 patterns[i].re = NULL;
> +                       } else {
> +                               patterns[i].extra = pcre_study(patterns[i].re, 0, &error);
> +                               if (error != NULL) {
> +                                       fprintf(stderr, "Regex %s failed to optimize: %s.\n", patterns[i].pattern, error);
> +                                       patterns[i].re = NULL;
> +                               }
>                         }
>                 }
>         }
> @@ -323,8 +326,7 @@ static bool klog_find(char *str, klog_pattern *patterns)
>                         if (strstr(str, patterns[i].pattern)) {
>                                 return true;
>                         }
> -               }
> -               if (patterns[i].cm == COMPARE_REGEX) {
> +               } else if (patterns[i].cm == COMPARE_REGEX) {
>                         int vector[1];
>                         if (pcre_exec(patterns[i].re, patterns[i].extra, str, strlen(str), 0, 0, vector, 1) == 0) {
>                                 return true;
> --
> 1.7.10.4
>
Acked-by: Keng-Yu Lin <kengyu@canonical.com>
Alex Hung - Nov. 21, 2012, 5:51 a.m.
On 11/03/2012 12:58 AM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> We only should compile a pattern if it is a regular expression. Also,
> make the klog_find a little faster.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>   src/utilities/kernelscan.c |   20 +++++++++++---------
>   1 file changed, 11 insertions(+), 9 deletions(-)
>
> diff --git a/src/utilities/kernelscan.c b/src/utilities/kernelscan.c
> index 9133ce9..2e0d606 100644
> --- a/src/utilities/kernelscan.c
> +++ b/src/utilities/kernelscan.c
> @@ -293,14 +293,17 @@ static klog_pattern *klog_load(const char *table)
>   			exit(EXIT_FAILURE);
>   		}
>
> -		if ((patterns[i].re = pcre_compile(patterns[i].pattern, 0, &error, &erroffset, NULL)) == NULL) {
> -			fprintf(stderr, "Regex %s failed to compile: %s.\n", patterns[i].pattern, error);
> -			patterns[i].re = NULL;
> -		} else {
> -			patterns[i].extra = pcre_study(patterns[i].re, 0, &error);
> -			if (error != NULL) {
> -				fprintf(stderr, "Regex %s failed to optimize: %s.\n", patterns[i].pattern, error);
> +		/* Pre-compile regular expressions to make things run a bit faster */
> +		if (patterns[i].cm == COMPARE_REGEX) {
> +			if ((patterns[i].re = pcre_compile(patterns[i].pattern, 0, &error, &erroffset, NULL)) == NULL) {
> +				fprintf(stderr, "Regex %s failed to compile: %s.\n", patterns[i].pattern, error);
>   				patterns[i].re = NULL;
> +			} else {
> +				patterns[i].extra = pcre_study(patterns[i].re, 0, &error);
> +				if (error != NULL) {
> +					fprintf(stderr, "Regex %s failed to optimize: %s.\n", patterns[i].pattern, error);
> +					patterns[i].re = NULL;
> +				}
>   			}
>   		}
>   	}
> @@ -323,8 +326,7 @@ static bool klog_find(char *str, klog_pattern *patterns)
>   			if (strstr(str, patterns[i].pattern)) {
>   				return true;
>   			}
> -		}
> -		if (patterns[i].cm == COMPARE_REGEX) {
> +		} else if (patterns[i].cm == COMPARE_REGEX) {
>   			int vector[1];
>   			if (pcre_exec(patterns[i].re, patterns[i].extra, str, strlen(str), 0, 0, vector, 1) == 0) {
>   				return true;
>
Acked-by: Alex Hung <alex.hung@canonical.com>

Patch

diff --git a/src/utilities/kernelscan.c b/src/utilities/kernelscan.c
index 9133ce9..2e0d606 100644
--- a/src/utilities/kernelscan.c
+++ b/src/utilities/kernelscan.c
@@ -293,14 +293,17 @@  static klog_pattern *klog_load(const char *table)
 			exit(EXIT_FAILURE);
 		}
 
-		if ((patterns[i].re = pcre_compile(patterns[i].pattern, 0, &error, &erroffset, NULL)) == NULL) {
-			fprintf(stderr, "Regex %s failed to compile: %s.\n", patterns[i].pattern, error);
-			patterns[i].re = NULL;
-		} else {
-			patterns[i].extra = pcre_study(patterns[i].re, 0, &error);
-			if (error != NULL) {
-				fprintf(stderr, "Regex %s failed to optimize: %s.\n", patterns[i].pattern, error);
+		/* Pre-compile regular expressions to make things run a bit faster */
+		if (patterns[i].cm == COMPARE_REGEX) {
+			if ((patterns[i].re = pcre_compile(patterns[i].pattern, 0, &error, &erroffset, NULL)) == NULL) {
+				fprintf(stderr, "Regex %s failed to compile: %s.\n", patterns[i].pattern, error);
 				patterns[i].re = NULL;
+			} else {
+				patterns[i].extra = pcre_study(patterns[i].re, 0, &error);
+				if (error != NULL) {
+					fprintf(stderr, "Regex %s failed to optimize: %s.\n", patterns[i].pattern, error);
+					patterns[i].re = NULL;
+				}
 			}
 		}
 	}
@@ -323,8 +326,7 @@  static bool klog_find(char *str, klog_pattern *patterns)
 			if (strstr(str, patterns[i].pattern)) {
 				return true;
 			}
-		}
-		if (patterns[i].cm == COMPARE_REGEX) {
+		} else if (patterns[i].cm == COMPARE_REGEX) {
 			int vector[1];
 			if (pcre_exec(patterns[i].re, patterns[i].extra, str, strlen(str), 0, 0, vector, 1) == 0) {
 				return true;