Skip to content

[cpyrt] Take template arg classes from the type when ranking overloads - #138

Draft
guitargeek wants to merge 1 commit into
compiler-research:mainfrom
guitargeek:rank-template-args-by-type
Draft

guitargeek wants to merge 1 commit into
compiler-research:mainfrom
guitargeek:rank-template-args-by-type

Conversation

@guitargeek

Copy link
Copy Markdown
Collaborator

To rank overloads, CPPMethod::GetPriority() looks up the class of each argument by its name. GetScope() cannot resolve template names like "std::vector" directly, so each such lookup declared a new trampoline in the interpreter, even for a name it had resolved before.

This happens once for every new method with such arguments, e.g. for every specialization of a function template taking containers. The method already knows the argument type, so take the class from the type instead, and force its definition like GetScope() does. Names of std::initializer_list still go through GetScope(), which deliberately resolves them as std::vector: the ranking relies on that (see PR #133 for the initializer_list case).

Add an interop wrapper for Cpp::GetUnderlyingType().

For example:

import time
import cppjit

cppjit.cppdef(r"""
#include <vector>
#include <utility>

template <typename T>
struct Box {
    int probe(const std::vector<T>& v) { return 1; }
    int probe(const std::pair<T, T>& p) { return 2; }
    int probe(int x) { return 3; }
};
""")

types = ["int", "long", "short", "char", "float", "double",
         "unsigned int", "unsigned long"]
extras = [f"std::pair<{a}, {b}>" for a in types for b in ("int", "double")][:12]

vec = cppjit.gbl.std.vector
start = time.perf_counter()
for t in types + extras:
    box = cppjit.gbl.Box[t]()
    box.probe(vec[t]())  # first call per specialization: overload ranking
print(f"first ranked call for {len(types) + len(extras)} specializations: {1000 * (time.perf_counter() - start):.0f} ms")

This takes about 1230 ms instead of 1460 ms (median of 3 trials, Linux x86_64, Python 3.14, CppInterOp/LLVM 22.1.8).

@guitargeek
guitargeek force-pushed the rank-template-args-by-type branch from 79a258e to cc388b6 Compare October 5, 2026 20:47
@aaronj0

aaronj0 commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator

We'll have to look at this in the context of the new overload resolution: https://github.com/compiler-research/cppjit/tree/overload-resolution cc @Vipul-Cariappa

To rank overloads, CPPMethod::GetPriority() looks up the class of each
argument by its name. GetScope() cannot resolve template names like
"std::vector<double>" directly, so each such lookup declared a new
trampoline in the interpreter, even for a name it had resolved before.

This happens once for every new method with such arguments, e.g. for
every specialization of a function template taking containers. The
method already knows the argument type, so take the class from the type
instead, and force its definition like GetScope() does. Names of
std::initializer_list<T> still go through GetScope(), which deliberately
resolves them as std::vector<T>: the ranking relies on that (see PR compiler-research#133
for the initializer_list case).

Add an interop wrapper for Cpp::GetUnderlyingType().

For example:

    import time
    import cppjit

    cppjit.cppdef(r"""
    #include <vector>
    #include <utility>

    template <typename T>
    struct Box {
        int probe(const std::vector<T>& v) { return 1; }
        int probe(const std::pair<T, T>& p) { return 2; }
        int probe(int x) { return 3; }
    };
    """)

    types = ["int", "long", "short", "char", "float", "double",
             "unsigned int", "unsigned long"]
    extras = [f"std::pair<{a}, {b}>" for a in types for b in ("int", "double")][:12]

    vec = cppjit.gbl.std.vector
    start = time.perf_counter()
    for t in types + extras:
        box = cppjit.gbl.Box[t]()
        box.probe(vec[t]())  # first call per specialization: overload ranking
    print(f"first ranked call for {len(types) + len(extras)} specializations: {1000 * (time.perf_counter() - start):.0f} ms")

This takes about 1230 ms instead of 1460 ms (median of 3 trials, Linux
x86_64, Python 3.14, CppInterOp/LLVM 22.1.8).
@guitargeek
guitargeek force-pushed the rank-template-args-by-type branch from cc388b6 to 0838dd7 Compare October 13, 2026 19:15
@aaronj0
aaronj0 marked this pull request as draft October 19, 2026 12:44

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants