Skip to content

Performance Issue on a Nested JSON example #15

Description

@soasme

PeppaPEG version: 1.2.0.

Given program, half of the execution time spent on NeedLoosen. We should optimize it.

$ cat examples/json.c
#include <stdio.h>
#include <stdlib.h>
#include "../peppapeg.h"
#include "json.h"

# define NESTING_DEPTH          1000

int main(int argc, char* argv[]) {
    char* input = malloc(sizeof(char) * (NESTING_DEPTH*2 + 1));
    int i;

    for (i = 0; i < NESTING_DEPTH; i++) {
        input[i] = '[';
        input[NESTING_DEPTH+i] = ']';
    }
    input[NESTING_DEPTH*2] = '\0';

    P4_Grammar* grammar = P4_CreateJSONGrammar();
    P4_Source* source = P4_CreateSource(input, P4_JSONEntry);

    printf("%u\n", P4_Parse(grammar, source));

    free(input);
    P4_DeleteSource(source);
    P4_DeleteGrammar(grammar);

    return 0;
}

Run it with valgrind:

$ gcc -g  peppapeg.c examples/json.c && valgrind --tool=callgrind ./a.out
$ callgrind_annotate callgrind.out.63642

We have below profiling result:

--------------------------------------------------------------------------------
Ir
--------------------------------------------------------------------------------
1,265,470,759 (100.0%)  PROGRAM TOTALS

--------------------------------------------------------------------------------
Ir                    file:function
--------------------------------------------------------------------------------
706,187,168 (55.80%)  peppapeg.c:P4_NeedLoosen [/app/a.out]
320,620,450 (25.34%)  peppapeg.c:P4_IsTight [/app/a.out]
170,320,160 (13.46%)  peppapeg.c:P4_IsScoped [/app/a.out]
 25,020,000 ( 1.98%)  peppapeg.c:P4_NeedSquash [/app/a.out]
 10,000,000 ( 0.79%)  peppapeg.c:P4_IsSquashed [/app/a.out]
  4,617,160 ( 0.36%)  ???:_int_free [/usr/lib64/libc-2.28.so]
  3,382,226 ( 0.27%)  ???:malloc [/usr/lib64/ld-2.28.so]
  2,727,160 ( 0.22%)  peppapeg.c:P4_Match'2 [/app/a.out]
  1,925,056 ( 0.15%)  ???:__strlen_avx2 [/usr/lib64/libc-2.28.so]
  1,848,485 ( 0.15%)  ???:free [/usr/lib64/ld-2.28.so]
  1,841,351 ( 0.15%)  peppapeg.c:P4_MatchLiteral [/app/a.out]
  1,705,988 ( 0.13%)  peppapeg.c:P4_MatchChoice'2 [/app/a.out]
  1,540,594 ( 0.12%)  peppapeg.c:P4_IsRule [/app/a.out]
  1,518,598 ( 0.12%)  peppapeg.c:P4_Expression_dispatch'2 [/app/a.out]
$ time ./a.out
real	0m0.157s
user	0m0.154s
sys	0m0.002s

A full profiling result can be seen here: https://gist.github.com/soasme/38471063511aa14302e5ccad173767de

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions