Optimized json_parser

1. use zend_string in key
2. use faster APIs
3. use ZVAL_COPY_VALUE instead of assignment (save u2 copy)
This commit is contained in:
Xinchen Hui 2015-05-06 11:37:30 +08:00
parent 9f0dbdf6c8
commit 76fb02123f
3 changed files with 796 additions and 746 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,8 +1,10 @@
/* A Bison parser, made by GNU Bison 3.0.4. */
/* Bison interface for Yacc-like parsers in C /* A Bison parser, made by GNU Bison 2.4.1. */
Copyright (C) 1984, 1989-1990, 2000-2015 Free Software Foundation, Inc. /* Skeleton interface for Bison's Yacc-like parsers in C
Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004, 2005, 2006
Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -30,21 +32,13 @@
This special exception was added by the Free Software Foundation in This special exception was added by the Free Software Foundation in
version 2.2 of Bison. */ version 2.2 of Bison. */
#ifndef YY_PHP_JSON_YY_HOME_JAKUB_PROG_PHP_MASTER_EXT_JSON_JSON_PARSER_TAB_H_INCLUDED
# define YY_PHP_JSON_YY_HOME_JAKUB_PROG_PHP_MASTER_EXT_JSON_JSON_PARSER_TAB_H_INCLUDED
/* Debug traces. */
#ifndef YYDEBUG
# define YYDEBUG 0
#endif
#if YYDEBUG
extern int php_json_yydebug;
#endif
/* Token type. */ /* Tokens. */
#ifndef YYTOKENTYPE #ifndef YYTOKENTYPE
# define YYTOKENTYPE # define YYTOKENTYPE
enum yytokentype /* Put the tokens into the symbol table, so that GDB and other debuggers
{ know about them. */
enum yytokentype {
PHP_JSON_T_NUL = 258, PHP_JSON_T_NUL = 258,
PHP_JSON_T_TRUE = 259, PHP_JSON_T_TRUE = 259,
PHP_JSON_T_FALSE = 260, PHP_JSON_T_FALSE = 260,
@ -67,29 +61,28 @@ extern int php_json_yydebug;
#define PHP_JSON_T_EOI 265 #define PHP_JSON_T_EOI 265
#define PHP_JSON_T_ERROR 266 #define PHP_JSON_T_ERROR 266
/* Value type. */
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
union YYSTYPE
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
typedef union YYSTYPE
{ {
zval value; zval value;
struct { struct {
zval key; zend_string *key;
zval val; zval val;
} pair; } pair;
};
typedef union YYSTYPE YYSTYPE; } YYSTYPE;
# define YYSTYPE_IS_TRIVIAL 1 # define YYSTYPE_IS_TRIVIAL 1
# define yystype YYSTYPE /* obsolescent; will be withdrawn */
# define YYSTYPE_IS_DECLARED 1 # define YYSTYPE_IS_DECLARED 1
#endif #endif
int php_json_yyparse (php_json_parser *parser);
#endif /* !YY_PHP_JSON_YY_HOME_JAKUB_PROG_PHP_MASTER_EXT_JSON_JSON_PARSER_TAB_H_INCLUDED */

View file

@ -46,7 +46,7 @@ int json_yydebug = 1;
%union { %union {
zval value; zval value;
struct { struct {
zval key; zend_string *key;
zval val; zval val;
} pair; } pair;
} }
@ -67,13 +67,13 @@ int json_yydebug = 1;
%type <pair> pair %type <pair> pair
%destructor { zval_dtor(&$$); } <value> %destructor { zval_dtor(&$$); } <value>
%destructor { zval_dtor(&$$.key); zval_dtor(&$$.val); } <pair> %destructor { zend_string_release($$.key); zval_dtor(&$$.val); } <pair>
%code { %code {
int php_json_yylex(union YYSTYPE *value, php_json_parser *parser); int php_json_yylex(union YYSTYPE *value, php_json_parser *parser);
void php_json_yyerror(php_json_parser *parser, char const *msg); void php_json_yyerror(php_json_parser *parser, char const *msg);
void php_json_parser_object_init(php_json_parser *parser, zval *object); void php_json_parser_object_init(php_json_parser *parser, zval *object);
void php_json_parser_object_update(php_json_parser *parser, zval *object, zval *zkey, zval *zvalue); void php_json_parser_object_update(php_json_parser *parser, zval *object, zend_string *key, zval *zvalue);
void php_json_parser_array_init(zval *object); void php_json_parser_array_init(zval *object);
void php_json_parser_array_append(zval *array, zval *zvalue); void php_json_parser_array_append(zval *array, zval *zvalue);
@ -89,7 +89,7 @@ void php_json_parser_array_append(zval *array, zval *zvalue);
%% /* Rules */ %% /* Rules */
start: start:
value PHP_JSON_T_EOI { $$ = $1; ZVAL_COPY_VALUE(parser->return_value, &$1); PHP_JSON_USE($2); YYACCEPT; } value PHP_JSON_T_EOI { ZVAL_COPY_VALUE(&$$, &$1); ZVAL_COPY_VALUE(parser->return_value, &$1); PHP_JSON_USE($2); YYACCEPT; }
| value errlex { PHP_JSON_USE_2($$, $1, $2); } | value errlex { PHP_JSON_USE_2($$, $1, $2); }
; ;
@ -108,18 +108,18 @@ members:
; ;
member: member:
pair { php_json_parser_object_init(parser, &$$); php_json_parser_object_update(parser, &$$, &$1.key, &$1.val); } pair { php_json_parser_object_init(parser, &$$); php_json_parser_object_update(parser, &$$, $1.key, &$1.val); }
| member ',' pair { php_json_parser_object_update(parser, &$1, &$3.key, &$3.val); $$ = $1; } | member ',' pair { php_json_parser_object_update(parser, &$1, $3.key, &$3.val); ZVAL_COPY_VALUE(&$$, &$1); }
| member errlex { PHP_JSON_USE_2($$, $1, $2); } | member errlex { PHP_JSON_USE_2($$, $1, $2); }
; ;
pair: pair:
key ':' value { $$.key = $1; $$.val = $3; } key ':' value { $$.key = Z_STR($1); ZVAL_COPY_VALUE(&$$.val, &$3); }
| key errlex { PHP_JSON_USE_2($$, $1, $2); } | key errlex { PHP_JSON_USE_2($$, $1, $2); }
; ;
array: array:
'[' { PHP_JSON_DEPTH_INC; } elements array_end { PHP_JSON_DEPTH_DEC; $$ = $3; } '[' { PHP_JSON_DEPTH_INC; } elements array_end { PHP_JSON_DEPTH_DEC; ZVAL_COPY_VALUE(&$$, &$3); }
; ;
array_end: array_end:
@ -134,7 +134,7 @@ elements:
element: element:
value { php_json_parser_array_init(&$$); php_json_parser_array_append(&$$, &$1); } value { php_json_parser_array_init(&$$); php_json_parser_array_append(&$$, &$1); }
| element ',' value { php_json_parser_array_append(&$1, &$3); $$ = $1; } | element ',' value { php_json_parser_array_append(&$1, &$3); ZVAL_COPY_VALUE(&$$, &$1); }
| element errlex { PHP_JSON_USE_2($$, $1, $2); } | element errlex { PHP_JSON_USE_2($$, $1, $2); }
; ;
@ -185,25 +185,25 @@ void php_json_parser_object_init(php_json_parser *parser, zval *object)
} }
} }
void php_json_parser_object_update(php_json_parser *parser, zval *object, zval *zkey, zval *zvalue) void php_json_parser_object_update(php_json_parser *parser, zval *object, zend_string *key, zval *zvalue)
{ {
char *key = Z_STRVAL_P(zkey); if (Z_TYPE_P(object) == IS_ARRAY/*parser->scanner.options & PHP_JSON_OBJECT_AS_ARRAY*/) {
size_t key_len = Z_STRLEN_P(zkey); zend_symtable_update(Z_ARRVAL_P(object), key, zvalue);
if (parser->scanner.options & PHP_JSON_OBJECT_AS_ARRAY) {
add_assoc_zval_ex(object, key, key_len, zvalue);
} else { } else {
if (key_len == 0) { zval zkey;
key = "_empty_"; /* ZEND_ASSERT(Z_TYPE_P(object) == IS_OBJECT); */
key_len = sizeof("_empty_") - 1; if (key->len == 0) {
zend_string_release(key);
key = zend_string_init("_empty_", sizeof("_empty_") - 1, 0);
} }
add_property_zval_ex(object, key, key_len, zvalue); ZVAL_NEW_STR(&zkey, key);
zend_std_write_property(object, &zkey, zvalue, NULL);
if (Z_REFCOUNTED_P(zvalue)) { if (Z_REFCOUNTED_P(zvalue)) {
Z_DELREF_P(zvalue); Z_DELREF_P(zvalue);
} }
} }
zval_dtor(zkey); zend_string_release(key);
} }
void php_json_parser_array_init(zval *array) void php_json_parser_array_init(zval *array)
@ -213,7 +213,7 @@ void php_json_parser_array_init(zval *array)
void php_json_parser_array_append(zval *array, zval *zvalue) void php_json_parser_array_append(zval *array, zval *zvalue)
{ {
add_next_index_zval(array, zvalue); zend_hash_next_index_insert(Z_ARRVAL_P(array), zvalue);
} }
int php_json_yylex(union YYSTYPE *value, php_json_parser *parser) int php_json_yylex(union YYSTYPE *value, php_json_parser *parser)