Skip to content

[cpyrt] Take element type of std::initializer_list args from the class - #133

Open
guitargeek wants to merge 1 commit into
compiler-research:mainfrom
guitargeek:initlist-element-type
Open

guitargeek wants to merge 1 commit into
compiler-research:mainfrom
guitargeek:initlist-element-type

Conversation

@guitargeek

Copy link
Copy Markdown
Collaborator

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:

  • GetPriority() resolved the argument name through GetScope(), which deliberately maps std::initializer_list to std::vector, 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 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:

    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.

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
guitargeek force-pushed the initlist-element-type branch from 1c79ca7 to 4ede630 Compare October 2, 2026 22:50

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.

1 participant