Skip to content

50x performance drop when calling overloaded function with mismatched type #514

@MomoDeve

Description

@MomoDeve
  • Compiler Used: MSVC (Visual Studio 2019 (v142))
  • Operating System: Windows 10 x64
  • Architecture: 64bit / Win32

Calling overloaded function in chaiscript with one mismatched type (for example 0 instead of 0.0f) causes 50x-100x performance drop. Minimal code to reproduce problem:

#include <chaiscript/chaiscript.hpp>
#include <iostream>
#include <string>
#include <chrono>

using namespace chaiscript;
using namespace std::chrono;

void func1(std::string a, float x, float y) { }
void func2(float a, float x, float y) {}

int main()
{
    ChaiScript chai;
    chai.add(fun(func1), "func");
    chai.add(fun(func2), "func");

    // ~2500 microseconds
    auto t1 = system_clock::now();
    chai.eval("func(0.0f, 0.0f, 0)"); 
    auto t2 = system_clock::now();
    std::cout << duration_cast<microseconds>(t2 - t1).count() << " microseconds\n";

    // ~25 microseconds
    auto t3 = system_clock::now();
    chai.eval("func(0.0f, 0.0f, 0.0f)");
    auto t4 = system_clock::now();
    std::cout << duration_cast<microseconds>(t4 - t3).count() << " microseconds\n";
}

I profiled my code and noticed that this is mainly caused by bad_boxed_cast exception which is thrown when trying to find the suitable function to invoke. The check for such function is done in the for loop in proxy_functions.hpp, lines 967-985.
I am using chaiscript engine in a game engine, so nearly 2ms per each function call is too slow in my case. In my opinion, the best way to solve this issue is just get rid of exception throwing during control flow.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions