Ver Fonte

Merge pull request #94118 from CrazyRoka/optimize-gdscript-notifications

Optimize `GDScriptInstance::notification` for better performance
Thaddeus Crews há 2 semanas atrás
pai
commit
7b5ee98474
1 ficheiros alterados com 13 adições e 11 exclusões
  1. 13 11
      modules/gdscript/gdscript.cpp

+ 13 - 11
modules/gdscript/gdscript.cpp

@@ -2080,20 +2080,22 @@ void GDScriptInstance::notification(int p_notification, bool p_reversed) {
 	//notification is not virtual, it gets called at ALL levels just like in C.
 	//notification is not virtual, it gets called at ALL levels just like in C.
 	Variant value = p_notification;
 	Variant value = p_notification;
 	const Variant *args[1] = { &value };
 	const Variant *args[1] = { &value };
+	const StringName &notification_str = GDScriptLanguage::get_singleton()->strings._notification;
 
 
-	List<GDScript *> pl;
-	GDScript *sptr = script.ptr();
-	while (sptr) {
-		if (p_reversed) {
-			pl.push_back(sptr);
-		} else {
-			pl.push_front(sptr);
-		}
-		sptr = sptr->_base;
+	LocalVector<GDScript *> script_stack;
+	uint32_t script_count = 0;
+	for (GDScript *sptr = script.ptr(); sptr; sptr = sptr->_base, ++script_count) {
+		script_stack.push_back(sptr);
 	}
 	}
-	for (GDScript *sc : pl) {
+
+	const int start = p_reversed ? 0 : script_count - 1;
+	const int end = p_reversed ? script_count : -1;
+	const int step = p_reversed ? 1 : -1;
+
+	for (int idx = start; idx != end; idx += step) {
+		GDScript *sc = script_stack[idx];
 		if (likely(sc->valid)) {
 		if (likely(sc->valid)) {
-			HashMap<StringName, GDScriptFunction *>::Iterator E = sc->member_functions.find(GDScriptLanguage::get_singleton()->strings._notification);
+			HashMap<StringName, GDScriptFunction *>::Iterator E = sc->member_functions.find(notification_str);
 			if (E) {
 			if (E) {
 				Callable::CallError err;
 				Callable::CallError err;
 				E->value->call(this, args, 1, err);
 				E->value->call(this, args, 1, err);