refactor for efficiency

pull/10041/head
Brent Shaffer 3 years ago
parent 7ed2c262e2
commit 8b4837981c
  1. 29
      php/ext/google/protobuf/names.c
  2. 1
      php/ext/google/protobuf/names.h
  3. 6
      php/ext/google/protobuf/protobuf.c

@ -261,3 +261,32 @@ char *GetPhpClassname(const upb_FileDef *file, const char *fullname, bool previo
free(prefix);
return ret;
}
bool IsPreviouslyUnreservedClassName(const char* fullname) {
const char *classname = strrchr(fullname, '\\');
if (classname) {
classname += 1;
} else {
classname = fullname;
}
if (strncmp(classname, "PB", 2) != 0) {
return false;
}
classname += 2;
int length = strlen(classname);
char* lower = calloc(1, length + 1);
memcpy(lower, classname, length);
int i = 0;
while(lower[i]) {
lower[i] = nolocale_tolower(lower[i]);
i++;
}
lower[length] = 0;
int j;
for (j = 0; kPreviouslyUnreservedNames[j]; j++) {
if (strcmp(kPreviouslyUnreservedNames[j], lower) == 0) {
return true;
}
}
return false;
}

@ -36,5 +36,6 @@
// Translates a protobuf symbol name (eg. foo.bar.Baz) into a PHP class name
// (eg. \Foo\Bar\Baz).
char *GetPhpClassname(const upb_FileDef *file, const char *fullname, bool previous);
bool IsPreviouslyUnreservedClassName(const char* fullname);
#endif // PHP_PROTOBUF_NAMES_H_

@ -244,12 +244,12 @@ bool ObjCache_Get(const void *upb_obj, zval *val) {
void NameMap_AddMessage(const upb_MessageDef *m) {
char *k = GetPhpClassname(upb_MessageDef_File(m), upb_MessageDef_FullName(m), false);
zend_hash_str_add_ptr(&PROTOBUF_G(name_msg_cache), k, strlen(k), (void*)m);
char *k2 = GetPhpClassname(upb_MessageDef_File(m), upb_MessageDef_FullName(m), true);
if (strcmp(k, k2) != 0) {
if (IsPreviouslyUnreservedClassName(k)) {
char *k2 = GetPhpClassname(upb_MessageDef_File(m), upb_MessageDef_FullName(m), true);
zend_hash_str_add_ptr(&PROTOBUF_G(name_msg_cache), k2, strlen(k2), (void*)m);
free(k2);
}
free(k);
free(k2);
}
void NameMap_AddEnum(const upb_EnumDef *e) {

Loading…
Cancel
Save