Skip to content

Commit

Permalink
Add new decode implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Antares0982 committed Jan 1, 2025
1 parent c6b478d commit c084846
Show file tree
Hide file tree
Showing 13 changed files with 1,458 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/decode/decode_loop_dirty/1_f_f.inl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* NOTE:
* write_as = max(_read_state.max_char_type, COMPILE_READ_UCS_LEVEL)
* need_copy == <escape is met>
* need_check_max_char = max_char_type < COMPILE_UCS_LEVEL
*
* additional note:
* 1. need_copy == false => max_char_type <= COMPILE_UCS_LEVEL
* !need_copy && !need_check_max_char => max_char_type == COMPILE_UCS_LEVEL
* COMPILE_UCS_LEVEL < max_char_type == write_as => need_copy == true
* 2. COMPILE_READ_UCS_LEVEL = COMPILE_UCS_LEVEL ? COMPILE_UCS_LEVEL : 1
* 3. max_char_type == 4 || COMPILE_UCS_LEVEL == 0 => need_check_max_char == false
*/

// knowledge before loop: max_char_type == COMPILE_UCS_LEVEL && max_char_type <= 1
assert(_read_state.need_copy);
switch (_read_state.max_char_type) {
#if COMPILE_UCS_LEVEL == PYYJSON_STRING_TYPE_ASCII
case PYYJSON_STRING_TYPE_ASCII: {
// max char can only be greater than before
// so before this loop we have
// max_char_type == COMPILE_UCS_LEVEL <= 0
// therefore, need_check_max_char is false
// 1_t_f
goto loop_1_t_f;
}
#endif
case PYYJSON_STRING_TYPE_LATIN1: {
// max char can only be greater than before.
// so "new max_char_type" == PYYJSON_STRING_TYPE_LATIN1 >= "old max_char_type" == COMPILE_UCS_LEVEL
// 1_t_f
goto loop_1_t_f;
}
case PYYJSON_STRING_TYPE_UCS2: {
// 2_t_f
goto loop_2_t_f;
}
case PYYJSON_STRING_TYPE_UCS4: {
// 4_t_f
goto loop_4_t_f;
}
default: {
assert(false);
Py_UNREACHABLE();
}
}
48 changes: 48 additions & 0 deletions src/decode/decode_loop_dirty/1_f_t.inl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* NOTE:
* write_as = max(_read_state.max_char_type, COMPILE_READ_UCS_LEVEL)
* need_copy == <escape is met>
* need_check_max_char = max_char_type < COMPILE_UCS_LEVEL
*
* additional note:
* 1. need_copy == false => max_char_type <= COMPILE_UCS_LEVEL
* !need_copy && !need_check_max_char => max_char_type == COMPILE_UCS_LEVEL
* COMPILE_UCS_LEVEL < max_char_type == write_as => need_copy == true
* 2. COMPILE_READ_UCS_LEVEL = COMPILE_UCS_LEVEL ? COMPILE_UCS_LEVEL : 1
* 3. max_char_type == 4 || COMPILE_UCS_LEVEL == 0 => need_check_max_char == false
*/

// knowledge before loop: max_char_type == 0 && COMPILE_UCS_LEVEL == 1
if (_read_state.need_copy) {
// escaped.
switch (_read_state.max_char_type) {
case PYYJSON_STRING_TYPE_ASCII: {
// max_char_type not updated
// 1_t_t
goto loop_1_t_t;
}
case PYYJSON_STRING_TYPE_LATIN1: {
// 1_t_f
goto loop_1_t_f;
}
case PYYJSON_STRING_TYPE_UCS2: {
// 2_t_f
goto loop_2_t_f;
}
case PYYJSON_STRING_TYPE_UCS4: {
// 4_t_f
goto loop_4_t_f;
}
default: {
assert(false);
Py_UNREACHABLE();
}
}
} else {
// "check max char" is dirty without any escape, max_char_type must have been updated
// so we must have "old max_char_type" == 0 and
// 1 == "new max_char_type" <= COMPILE_UCS_LEVEL == 1,
// since need_copy is false
assert(_read_state.max_char_type == COMPILE_UCS_LEVEL && COMPILE_UCS_LEVEL == 1);
goto loop_1_f_f;
}
47 changes: 47 additions & 0 deletions src/decode/decode_loop_dirty/1_t_f.inl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* NOTE:
* write_as = max(_read_state.max_char_type, COMPILE_READ_UCS_LEVEL)
* need_copy == <escape is met>
* need_check_max_char = max_char_type < COMPILE_UCS_LEVEL
*
* additional note:
* 1. need_copy == false => max_char_type <= COMPILE_UCS_LEVEL
* !need_copy && !need_check_max_char => max_char_type == COMPILE_UCS_LEVEL
* COMPILE_UCS_LEVEL < max_char_type == write_as => need_copy == true
* 2. COMPILE_READ_UCS_LEVEL = COMPILE_UCS_LEVEL ? COMPILE_UCS_LEVEL : 1
* 3. max_char_type == 4 || COMPILE_UCS_LEVEL == 0 => need_check_max_char == false
*/

// knowledge before loop: 1 >= max_char_type >= COMPILE_UCS_LEVEL
assert(_read_state.need_copy);

switch (_read_state.max_char_type) {
#if !defined(NDEBUG) && COMPILE_UCS_LEVEL == PYYJSON_STRING_TYPE_ASCII
case PYYJSON_STRING_TYPE_ASCII: {
// max char can only be greater than before.
// this means max_char == 0
assert(false);
// 1_t_f (self), not really dirty.
goto loop_1_t_f;
}
#endif
#if !defined(NDEBUG)
case PYYJSON_STRING_TYPE_LATIN1: {
assert(false);
// 1_t_f (self), not really dirty.
goto loop_1_t_f;
}
#endif
case PYYJSON_STRING_TYPE_UCS2: {
// 2_t_f
goto loop_2_t_f;
}
case PYYJSON_STRING_TYPE_UCS4: {
// 4_t_f
goto loop_4_t_f;
}
default: {
assert(false);
Py_UNREACHABLE();
}
}
40 changes: 40 additions & 0 deletions src/decode/decode_loop_dirty/1_t_t.inl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* NOTE:
* write_as = max(_read_state.max_char_type, COMPILE_READ_UCS_LEVEL)
* need_copy == <escape is met>
* need_check_max_char = max_char_type < COMPILE_UCS_LEVEL
*
* additional note:
* 1. need_copy == false => max_char_type <= COMPILE_UCS_LEVEL
* !need_copy && !need_check_max_char => max_char_type == COMPILE_UCS_LEVEL
* COMPILE_UCS_LEVEL < max_char_type == write_as => need_copy == true
* 2. COMPILE_READ_UCS_LEVEL = COMPILE_UCS_LEVEL ? COMPILE_UCS_LEVEL : 1
* 3. max_char_type == 4 || COMPILE_UCS_LEVEL == 0 => need_check_max_char == false
*/

// knowledge before loop: max_char_type == 0 && COMPILE_UCS_LEVEL == 1
switch (_read_state.max_char_type) {
#if !defined(NDEBUG)
case PYYJSON_STRING_TYPE_ASCII: {
assert(false);
// 1_t_t (self), not really dirty.
goto loop_1_t_t;
}
#endif
case PYYJSON_STRING_TYPE_LATIN1: {
// 1_t_f
goto loop_1_t_f;
}
case PYYJSON_STRING_TYPE_UCS2: {
// 2_t_f
goto loop_2_t_f;
}
case PYYJSON_STRING_TYPE_UCS4: {
// 4_t_f
goto loop_4_t_f;
}
default: {
assert(false);
Py_UNREACHABLE();
}
}
30 changes: 30 additions & 0 deletions src/decode/decode_loop_dirty/2_f_f.inl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* NOTE:
* write_as = max(_read_state.max_char_type, COMPILE_READ_UCS_LEVEL)
* need_copy == <escape is met>
* need_check_max_char = max_char_type < COMPILE_UCS_LEVEL
*
* additional note:
* 1. need_copy == false => max_char_type <= COMPILE_UCS_LEVEL
* !need_copy && !need_check_max_char => max_char_type == COMPILE_UCS_LEVEL
* COMPILE_UCS_LEVEL < max_char_type == write_as => need_copy == true
* 2. COMPILE_READ_UCS_LEVEL = COMPILE_UCS_LEVEL ? COMPILE_UCS_LEVEL : 1
* 3. max_char_type == 4 || COMPILE_UCS_LEVEL == 0 => need_check_max_char == false
*/

// knowledge before loop: 2 == max_char_type == COMPILE_UCS_LEVEL
assert(_read_state.need_copy);
switch (_read_state.max_char_type) {
case PYYJSON_STRING_TYPE_UCS2: {
// 2_t_f
goto loop_2_t_f;
}
case PYYJSON_STRING_TYPE_UCS4: {
// 4_t_f
goto loop_4_t_f;
}
default: {
assert(false);
Py_UNREACHABLE();
}
}
52 changes: 52 additions & 0 deletions src/decode/decode_loop_dirty/2_f_t.inl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* NOTE:
* write_as = max(_read_state.max_char_type, COMPILE_READ_UCS_LEVEL)
* need_copy == <escape is met>
* need_check_max_char = max_char_type < COMPILE_UCS_LEVEL
*
* additional note:
* 1. need_copy == false => max_char_type <= COMPILE_UCS_LEVEL
* !need_copy && !need_check_max_char => max_char_type == COMPILE_UCS_LEVEL
* COMPILE_UCS_LEVEL < max_char_type == write_as => need_copy == true
* 2. COMPILE_READ_UCS_LEVEL = COMPILE_UCS_LEVEL ? COMPILE_UCS_LEVEL : 1
* 3. max_char_type == 4 || COMPILE_UCS_LEVEL == 0 => need_check_max_char == false
*/

// knowledge before loop: max_char_type < COMPILE_UCS_LEVEL == 2
static_assert(COMPILE_UCS_LEVEL == 2, "COMPILE_UCS_LEVEL == 2");
if (_read_state.need_copy) {
// escaped.
switch (_read_state.max_char_type) {
case PYYJSON_STRING_TYPE_ASCII: {
// 1_t_t
goto loop_2_t_t;
}
case PYYJSON_STRING_TYPE_LATIN1: {
// 1_t_t
goto loop_2_t_t;
}
case PYYJSON_STRING_TYPE_UCS2: {
// 2_t_f
goto loop_2_t_f;
}
case PYYJSON_STRING_TYPE_UCS4: {
// 4_t_f
goto loop_4_t_f;
}
default: {
assert(false);
Py_UNREACHABLE();
}
}
} else {
// check max char is dirty without any escape, max_char_type must have been updated
#if !defined(NDEBUG)
if (_read_state.max_char_type < COMPILE_UCS_LEVEL) {
assert(false);
// 2_f_t (self), not really dirty.
goto loop_2_f_t;
}
#endif
// 2_f_f
goto loop_2_f_f;
}
27 changes: 27 additions & 0 deletions src/decode/decode_loop_dirty/2_t_f.inl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* NOTE:
* write_as = max(_read_state.max_char_type, COMPILE_READ_UCS_LEVEL)
* need_copy == <escape is met>
* need_check_max_char = max_char_type < COMPILE_UCS_LEVEL
*
* additional note:
* 1. need_copy == false => max_char_type <= COMPILE_UCS_LEVEL
* !need_copy && !need_check_max_char => max_char_type == COMPILE_UCS_LEVEL
* COMPILE_UCS_LEVEL < max_char_type == write_as => need_copy == true
* 2. COMPILE_READ_UCS_LEVEL = COMPILE_UCS_LEVEL ? COMPILE_UCS_LEVEL : 1
* 3. max_char_type == 4 || COMPILE_UCS_LEVEL == 0 => need_check_max_char == false
*/

// knowledge before loop: max_char_type == 2 >= COMPILE_UCS_LEVEL
assert(_read_state.need_copy);

#if !defined(NDEBUG)
if (_read_state.max_char_type == PYYJSON_STRING_TYPE_UCS2) {
assert(false);
// 2_t_f (self), not really dirty
goto loop_2_t_f;
}
#endif
assert(_read_state.max_char_type == PYYJSON_STRING_TYPE_UCS4);
// 4_t_f
goto loop_4_t_f;
33 changes: 33 additions & 0 deletions src/decode/decode_loop_dirty/2_t_t.inl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* NOTE:
* write_as = max(_read_state.max_char_type, COMPILE_READ_UCS_LEVEL)
* need_copy == <escape is met>
* need_check_max_char = max_char_type < COMPILE_UCS_LEVEL
*
* additional note:
* 1. need_copy == false => max_char_type <= COMPILE_UCS_LEVEL
* !need_copy && !need_check_max_char => max_char_type == COMPILE_UCS_LEVEL
* COMPILE_UCS_LEVEL < max_char_type == write_as => need_copy == true
* 2. COMPILE_READ_UCS_LEVEL = COMPILE_UCS_LEVEL ? COMPILE_UCS_LEVEL : 1
* 3. max_char_type == 4 || COMPILE_UCS_LEVEL == 0 => need_check_max_char == false
*/

// knowledge before loop: max_char_type < COMPILE_UCS_LEVEL == 2

#if !defined(NDEBUG)
if (_read_state.max_char_type < PYYJSON_STRING_TYPE_UCS2) {
assert(false);
// 2_t_t (self), not really dirty
goto loop_2_t_t;
}
#endif
assert(_read_state.max_char_type >= PYYJSON_STRING_TYPE_UCS2);

if (_read_state.max_char_type == PYYJSON_STRING_TYPE_UCS2) {
// 2_t_f
goto loop_2_t_f;
} else {
assert(_read_state.max_char_type == PYYJSON_STRING_TYPE_UCS4);
// 4_t_f
goto loop_4_t_f;
}
18 changes: 18 additions & 0 deletions src/decode/decode_loop_dirty/4_f_f.inl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* NOTE:
* write_as = max(_read_state.max_char_type, COMPILE_READ_UCS_LEVEL)
* need_copy == <escape is met>
* need_check_max_char = max_char_type < COMPILE_UCS_LEVEL
*
* additional note:
* 1. need_copy == false => max_char_type <= COMPILE_UCS_LEVEL
* !need_copy && !need_check_max_char => max_char_type == COMPILE_UCS_LEVEL
* COMPILE_UCS_LEVEL < max_char_type == write_as => need_copy == true
* 2. COMPILE_READ_UCS_LEVEL = COMPILE_UCS_LEVEL ? COMPILE_UCS_LEVEL : 1
* 3. max_char_type == 4 || COMPILE_UCS_LEVEL == 0 => need_check_max_char == false
*/

// knowledge before loop: max_char_type == COMPILE_UCS_LEVEL == 4
assert(_read_state.need_copy);
assert(_read_state.max_char_type == PYYJSON_STRING_TYPE_UCS4);
goto loop_4_t_f;
36 changes: 36 additions & 0 deletions src/decode/decode_loop_dirty/4_f_t.inl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* NOTE:
* write_as = max(_read_state.max_char_type, COMPILE_READ_UCS_LEVEL)
* need_copy == <escape is met>
* need_check_max_char = max_char_type < COMPILE_UCS_LEVEL
*
* additional note:
* 1. need_copy == false => max_char_type <= COMPILE_UCS_LEVEL
* !need_copy && !need_check_max_char => max_char_type == COMPILE_UCS_LEVEL
* COMPILE_UCS_LEVEL < max_char_type == write_as => need_copy == true
* 2. COMPILE_READ_UCS_LEVEL = COMPILE_UCS_LEVEL ? COMPILE_UCS_LEVEL : 1
* 3. max_char_type == 4 || COMPILE_UCS_LEVEL == 0 => need_check_max_char == false
*/

// knowledge before loop: max_char_type < COMPILE_UCS_LEVEL == 4
if (_read_state.need_copy) {
// escaped.
if (_read_state.max_char_type == PYYJSON_STRING_TYPE_UCS4) {
goto loop_4_t_f;
} else {
assert(_read_state.max_char_type >= PYYJSON_STRING_TYPE_ASCII && _read_state.max_char_type <= PYYJSON_STRING_TYPE_UCS2);
goto loop_4_t_t;
}
} else {
// check max char is dirty without any escape, max_char_type must have been updated
#if !defined(NDEBUG)
if (_read_state.max_char_type < COMPILE_UCS_LEVEL) {
assert(false);
// loop_4_f_t (self), not really dirty.
goto loop_4_f_t;
}
#endif
assert(_read_state.max_char_type == COMPILE_UCS_LEVEL);
// 4_f_f
goto loop_4_f_f;
}
Loading

0 comments on commit c084846

Please sign in to comment.