dimanche 26 avril 2020

Is there preformance penalty if I use lambda instead of an If block?

I'm using Dear ImGui to draw my UIs, in immediate mode, in an OpenGL program.

To do a tab, you tipically do:

if (ImGui::BeginTabItem("Some Tab")) {
    // Stuff
    ImGui::EndTabItem();
}

I come from Kotlin, where you typically use constructs like this:

something("bla bla bla") { x ->
  // whatever
} // this is a Kotlin lambda btw

So I wrote a simple wrapper, to wrap the call instead of writing the if and the end call, in order to avoid accidentally omitting the end call:

inline void tab(const char* label, std::function<void()> fn)
{
    if (ImGui::BeginTabItem(label)) {
        fn();
        ImGui::EndTabItem();
    }
}

Which replaces the first snippet by somehting like:

ui::tab("Some other tab", []{
    // More UI...
});

The question is, will the compiler emit similar code? Or there will be a big performance impact?

I'm afraid that if the compiler just places a new callable struct each time that the UI is drawn it will be a problem.

Also, I'm capturing this pointer, to use inside.

if (ImGui::BeginTabItem("Some Tab")) {
    // Stuff
    ImGui::EndTabItem();
}

// -- VS -- //

ui::tab("Some other tab", [this]{
    // More UI...
});

Aucun commentaire:

Enregistrer un commentaire