Bolt-Scripts / MHR-InGame-ModMenu-API

A user-friendly IMGUI inspired API for drawing in-game settings menus for REF mods in MHRise
13 stars 4 forks source link

[Optimization] Cache methods and singletons #3

Closed GreenComfyTea closed 2 years ago

GreenComfyTea commented 2 years ago

1) In ModOptionsMenu/ModMenuApi.lua you are calling a couple of methods of GuiManager by passing method name as a string inside PromptMsg and PromptYN functions:

gui_mgr:call("setOpenInfo(System.String, snow.gui.GuiCommonInfoBase.Type, snow.gui.SnowGuiCommonUtility.Segment, System.Boolean, System.Boolean)", promptMessage, 0x1, 0x32, false, false)

In this case it performs a hashmap lookup. You can cache the method.

local setOpenInfoMethod = sdk.find_type_definition("snow.gui.GuiManager"):get_method("setOpenInfo(System.String, snow.gui.GuiCommonInfoBase.Type, snow.gui.SnowGuiCommonUtility.Segment, System.Boolean, System.Boolean)")
...
setOpenInfoMethod:call(gui_mgr, promptMessage, 0x1, 0x32, false, false))

2) In the same functions you call methods with custom indexers, but I don't know if it does a hashmap lookup or not. Probably not.

gui_mgr:updateInfoWindow()

3) In the same places for getting GuiManager you call sdk.get_managed_singleton. Since this is a singleton you can cache it the first time you need it, or even maybe during initialization, and the just use it.

Bolt-Scripts commented 2 years ago

All valid points, though yeah I'm not sure about what the custom indexers do behind the scenes. But still, it's kind of just a hassle and increases complexity to cache these things so I didn't for the sake of simplicity. For the most part none of this runs on update or anything, they're one offs whenever they're called so it's negligible anyway. Especially since it's just options menu code and not gameplay code I don't feel the need to make sure it's all optimal vs just being easier to read.

GreenComfyTea commented 2 years ago

True. I saw you did cache stuff in the example script so decided to suggest this even thou its one-time calls, just for the code style uniformity reasons.

Bolt-Scripts commented 2 years ago

It's kinda all over the place, programming in lua sucks.