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
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
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
# 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_TRUE = 259,
PHP_JSON_T_FALSE = 260,
@ -67,29 +61,28 @@ extern int php_json_yydebug;
#define PHP_JSON_T_EOI 265
#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;
struct {
zval key;
zend_string *key;
zval val;
} pair;
};
typedef union YYSTYPE YYSTYPE;
} YYSTYPE;
# define YYSTYPE_IS_TRIVIAL 1
# define yystype YYSTYPE /* obsolescent; will be withdrawn */
# define YYSTYPE_IS_DECLARED 1
#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 {
zval value;
struct {
zval key;
zend_string *key;
zval val;
} pair;
}
@ -67,13 +67,13 @@ int json_yydebug = 1;
%type <pair> pair
%destructor { zval_dtor(&$$); } <value>
%destructor { zval_dtor(&$$.key); zval_dtor(&$$.val); } <pair>
%destructor { zend_string_release($$.key); zval_dtor(&$$.val); } <pair>
%code {
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_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_append(zval *array, zval *zvalue);
@ -89,7 +89,7 @@ void php_json_parser_array_append(zval *array, zval *zvalue);
%% /* Rules */
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); }
;
@ -108,18 +108,18 @@ members:
;
member:
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; }
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); ZVAL_COPY_VALUE(&$$, &$1); }
| member errlex { PHP_JSON_USE_2($$, $1, $2); }
;
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); }
;
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:
@ -134,7 +134,7 @@ elements:
element:
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); }
;
@ -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);
size_t key_len = Z_STRLEN_P(zkey);
if (parser->scanner.options & PHP_JSON_OBJECT_AS_ARRAY) {
add_assoc_zval_ex(object, key, key_len, zvalue);
if (Z_TYPE_P(object) == IS_ARRAY/*parser->scanner.options & PHP_JSON_OBJECT_AS_ARRAY*/) {
zend_symtable_update(Z_ARRVAL_P(object), key, zvalue);
} else {
if (key_len == 0) {
key = "_empty_";
key_len = sizeof("_empty_") - 1;
zval zkey;
/* ZEND_ASSERT(Z_TYPE_P(object) == IS_OBJECT); */
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)) {
Z_DELREF_P(zvalue);
}
}
zval_dtor(zkey);
zend_string_release(key);
}
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)
{
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)