From 21b90e9d47bbce7ffb1f3e1f1e27a56fc7abf056 Mon Sep 17 00:00:00 2001 From: Mothblocks <35135081+Mothblocks@users.noreply.github.com> Date: Mon, 17 May 2021 01:13:34 -0700 Subject: [PATCH] Micro-optimize TGUI_CREATE_MESSAGE (#59142) --- code/__DEFINES/tgui.dm | 9 +++--- code/modules/unit_tests/_unit_tests.dm | 1 + .../modules/unit_tests/tgui_create_message.dm | 28 +++++++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 code/modules/unit_tests/tgui_create_message.dm diff --git a/code/__DEFINES/tgui.dm b/code/__DEFINES/tgui.dm index f594b735b6b..e20ad071db8 100644 --- a/code/__DEFINES/tgui.dm +++ b/code/__DEFINES/tgui.dm @@ -28,8 +28,9 @@ #define TGUI_WINDOW_INDEX(window_id) text2num(copytext(window_id, 13)) /// Creates a message packet for sending via output() +// This is {"type":type,"payload":payload}, but pre-encoded. This is much faster +// than doing it the normal way. +// To ensure this is correct, this is unit tested in tgui_create_message. #define TGUI_CREATE_MESSAGE(type, payload) ( \ - url_encode(json_encode(list( \ - "type" = type, \ - "payload" = payload, \ - )))) + "%7b%22type%22%3a%22[type]%22%2c%22payload%22%3a[url_encode(json_encode(payload))]%7d" \ +) diff --git a/code/modules/unit_tests/_unit_tests.dm b/code/modules/unit_tests/_unit_tests.dm index 120147b5c00..03cbe13cc82 100644 --- a/code/modules/unit_tests/_unit_tests.dm +++ b/code/modules/unit_tests/_unit_tests.dm @@ -88,6 +88,7 @@ #include "subsystem_init.dm" #include "surgeries.dm" #include "teleporters.dm" +#include "tgui_create_message.dm" #include "timer_sanity.dm" #include "unit_test.dm" #include "wizard.dm" diff --git a/code/modules/unit_tests/tgui_create_message.dm b/code/modules/unit_tests/tgui_create_message.dm new file mode 100644 index 00000000000..4d5a4bc0a02 --- /dev/null +++ b/code/modules/unit_tests/tgui_create_message.dm @@ -0,0 +1,28 @@ +/// Test that `TGUI_CREATE_MESSAGE` is correctly implemented +/datum/unit_test/tgui_create_message + +/datum/unit_test/tgui_create_message/Run() + var/type = "something/here" + var/list/payload = list( + "name" = "Terry McTider", + "heads_caved" = 100, + "accomplishments" = list( + "nothing", + "literally nothing", + list( + "something" = "just kidding", + ), + ), + ) + + var/message = TGUI_CREATE_MESSAGE(type, payload) + + // Ensure consistent output to compare by performing a round-trip. + var/output = json_encode(json_decode(url_decode(message))) + + var/expected = json_encode(list( + "type" = type, + "payload" = payload, + )) + + TEST_ASSERT_EQUAL(expected, output, "TGUI_CREATE_MESSAGE didn't round trip properly")