Repository navigation
[cpyrt] Take element type of std::initializer_list args from the class - #133
Open
guitargeek wants to merge 1 commit into
Open
guitargeek wants to merge 1 commit into
guitargeek wants to merge 1 commit into
Conversation
Almost every constructor of a standard container has a
std::initializer_list<T> argument, and each of them resolved a type
from its spelling, which declares new code:
- GetPriority() resolved the argument name through GetScope(), which
deliberately maps std::initializer_list<T> to std::vector<T>, a
mapping the ranking of overloads relies on. This declared a new
trampoline variable on every call.
- CreateConverter() and the InitializerListConverter resolved the
element type T from a substring of the type name.
Take T from the template arguments of the std::initializer_list class
instead, and instantiate std::vector<T> from it directly for the
ranking. Names like std::vector<std::initializer_list<int>>, where the
list is not the argument class itself, still go through GetScope().
Add interop wrappers for Cpp::GetClassTemplateInstantiationArgs() and
Cpp::GetUnderlyingType().
For example:
import time
import cppjit
cppjit.cppdef("#include <set>\n#include <list>\n#include <vector>\n#include <utility>\n")
types = ["int", "long", "short", "char", "float", "double",
"unsigned int", "unsigned long"]
pairs = [f"std::pair<{a}, {b}>" for a, b in
[("int", "int"), ("long", "double"), ("double", "double"),
("short", "short"), ("float", "float"), ("long", "long"),
("unsigned int", "unsigned long"), ("char", "char")]]
classes = ([cppjit.gbl.std.set[t] for t in types] +
[cppjit.gbl.std.list[t] for t in types] +
[cppjit.gbl.std.vector[p] for p in pairs])
start = time.perf_counter()
for cls in classes:
cls() # first construction of each class
print(f"first construction of {len(classes)} container classes: {1000 * (time.perf_counter() - start):.0f} ms")
This takes about 645 ms instead of 690 ms (median of 3 trials, Linux
x86_64, Python 3.14, CppInterOp/LLVM 22.1.8). The savings grow with the
number of distinct container instantiations created for the first time.
guitargeek
force-pushed
the
initlist-element-type
branch
from
October 2, 2026 22:50
1c79ca7 to
4ede630
Compare
guitargeek
added a commit
to guitargeek/cppjit
that referenced
this pull request
Oct 5, 2026
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
added a commit
to guitargeek/cppjit
that referenced
this pull request
Oct 13, 2026
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).
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Almost every constructor of a standard container has a std::initializer_list argument, and each of them resolved a type from its spelling, which declares new code:
Take T from the template arguments of the std::initializer_list class instead, and instantiate std::vector from it directly for the ranking. Names like std::vector<std::initializer_list>, where the list is not the argument class itself, still go through GetScope().
Add interop wrappers for Cpp::GetClassTemplateInstantiationArgs() and Cpp::GetUnderlyingType().
For example:
This takes about 645 ms instead of 690 ms (median of 3 trials, Linux x86_64, Python 3.14, CppInterOp/LLVM 22.1.8). The savings grow with the number of distinct container instantiations created for the first time.