Skip to content

Commit 9020ac7

Browse files
authored
optimize all_name_chars (#3442)
Remove redundant PyUnicode_Check call. Use a static table for checking chars.
1 parent 590665c commit 9020ac7

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

Objects/codeobject.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
#include "code.h"
55
#include "structmember.h"
66

7-
#define NAME_CHARS \
8-
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
9-
107
/* Holder for co_extra information */
118
typedef struct {
129
Py_ssize_t ce_size;
@@ -18,23 +15,26 @@ typedef struct {
1815
static int
1916
all_name_chars(PyObject *o)
2017
{
21-
static char ok_name_char[256];
22-
static const unsigned char *name_chars = (unsigned char *)NAME_CHARS;
18+
/* [a-zA-Z0-9_] */
19+
static const bool ok_name_char[128] = {
20+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
21+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
22+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
23+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
24+
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
25+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,
26+
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
27+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0
28+
};
2329
const unsigned char *s, *e;
2430

25-
if (!PyUnicode_Check(o) || PyUnicode_READY(o) == -1 ||
26-
!PyUnicode_IS_ASCII(o))
31+
if (PyUnicode_READY(o) == -1 || !PyUnicode_IS_ASCII(o))
2732
return 0;
2833

29-
if (ok_name_char[*name_chars] == 0) {
30-
const unsigned char *p;
31-
for (p = name_chars; *p; p++)
32-
ok_name_char[*p] = 1;
33-
}
3434
s = PyUnicode_1BYTE_DATA(o);
3535
e = s + PyUnicode_GET_LENGTH(o);
36-
while (s != e) {
37-
if (ok_name_char[*s++] == 0)
36+
for (; s != e; s++) {
37+
if (!ok_name_char[*s])
3838
return 0;
3939
}
4040
return 1;

0 commit comments

Comments
 (0)