Skip to content

Replace MSVC lazy initialization with DllMain entry point#233

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/remove-lazy-initialization
Draft

Replace MSVC lazy initialization with DllMain entry point#233
Copilot wants to merge 3 commits intomainfrom
copilot/remove-lazy-initialization

Conversation

Copy link
Contributor

Copilot AI commented Mar 14, 2026

On MSVC, all dispatch function pointers were lazily initialized via NULL checks on every call. GCC/Clang use __attribute__((constructor)) for load-time init, but MSVC had no equivalent—just runtime overhead on every invocation.

Changes

  • Unified DECLARE_INTERNAL_* and DECLARE_ALL_KEYVALUE_METHODS macros — removed the MSVC-specific variants with NULL checks and forward declarations, keeping only the direct-call versions (~130 lines of duplicated macro code deleted)
  • Added XSS_POST_RESOLVE macro — on MSVC, each DISPATCH/DISPATCH_KV_FUNC registers its resolve function into a static array during C++ static init; no-op on other compilers
  • Added DllMain on DLL_PROCESS_ATTACH — iterates registered resolve functions and calls them all at load time
// MSVC: resolve functions register themselves during static init
#define XSS_POST_RESOLVE(resolve_func_name) \
    static int CAT(xss_reg_, resolve_func_name) \
            = (xss_init_funcs[xss_init_count < XSS_MAX_INIT_FUNCS \
                                      ? xss_init_count++ \
                                      : xss_init_count] \
                       = &resolve_func_name, 0);

// DllMain calls them all on load
extern "C" BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
{
    if (fdwReason == DLL_PROCESS_ATTACH) {
        for (int i = 0; i < xss_init_count; i++)
            xss_init_funcs[i]();
    }
    return TRUE;
}

CRT static initialization order within a single TU guarantees all registrations complete before DllMain is called by _DllMainCRTStartup.


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Copilot AI and others added 2 commits March 14, 2026 15:55
Remove MSVC-specific lazy initialization (NULL checks) from
DECLARE_INTERNAL_* and DECLARE_ALL_KEYVALUE_METHODS macros, unifying
them with the non-MSVC versions. Add XSS_POST_RESOLVE macro to
register resolve functions during static initialization on MSVC.
Add DllMain that calls all registered resolve functions on
DLL_PROCESS_ATTACH.

Co-authored-by: r-devulap <44766858+r-devulap@users.noreply.github.com>
Co-authored-by: r-devulap <44766858+r-devulap@users.noreply.github.com>
Copilot AI changed the title [WIP] Remove lazy initialization of pointers in x86simdsort Replace MSVC lazy initialization with DllMain entry point Mar 14, 2026
Copilot AI requested a review from r-devulap March 14, 2026 16:00
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