skymp icon indicating copy to clipboard operation
skymp copied to clipboard

Speed up calls of scripting functions

Open Pospelove opened this issue 3 years ago • 1 comments

In SkyrimPlatformProxy.cpp we call scripting function by name and class name:

thread_local auto callNative = origin.GetProperty("callNative");
return callNative.Call(*callNativeArgs);

The existing code is pretty slow since it goes into JS engine internals under the hood. Since we are in C++ code we should use C++ API for calling scripting functions instead: CallNative.h.

Example of using CallNative.h is in CallNativeApi.cpp

Feel free to refactor anything but consider generating as low diff as possible.

Here is benchmark:

// Save this code as "Data/Platform/Plugins/test.js"
// Ping kkEngine or RetroWave or Pospelov in Discord if this code isn't working 
skyrimPlatform.on("update", () => {
    let date = Date.now();
    for (let i = 0; i < 10000; ++i) skyrimPlatform.Game.getSunPositionX();
    skyrimPlatform.printConsole("SP:", Date.now() - date);
});

it outputs to the game console (can be opened by ~).

Please provide "before" and "after" benchmark results. I think that performance will be better after doing this task, but it's ok if the actual results will be not so much better.

Pospelove avatar Nov 10 '21 02:11 Pospelove

@Wedmer Hey! Any progress on this?

Pospelove avatar Apr 10 '22 00:04 Pospelove

Initial Version01

Initial ⇾ Result

Comparison: Initial - min: 226 ms; max: 258 ms; average (middle of all current): ~238 ms Result of #1491 - min: 212 ms; max: 258 ms; average (middle of all current): ~218 ms Perhaps the maximum is the same because of the same time period when the game starts loading. (it can be ~250-260 ms)

Benchmark code:

// Save this code as "Data/Platform/Plugins/test.js"
// Ping kkEngine or RetroWave or Pospelov in Discord if this code isn't working

let min = 10000
let max = 0

skyrimPlatform.on("update", () => {
    let date = Date.now();
    for (let i = 0; i < 10000; ++i) skyrimPlatform.Game.getSunPositionX();

    let current = Date.now() - date;
    if (current < min) min = current;
    else if (current > max) max = current;

    skyrimPlatform.printConsole("SP - min: ", min, " max: ", max, " current: ", current);
});

Benchmark device configurations: OS - Windows 10 Pro Processor - Intel(R) Core(TM) i7-3770 CPU @ 3.40GHz 3.90 GHz RAM memory - 32GB Video card - NVIDIA GeForce GTX 1050TI

ZikkeyLS avatar Jun 22 '23 15:06 ZikkeyLS

Thank you for your detailed report and for your PR @ZikkeyLS, taking a look

Pospelove avatar Jun 22 '23 15:06 Pospelove