def wrap_with_mergestate(self, timsort, func, _cache=None):
        """
        Wrap *func* into another compiled function inserting a runtime-created
        mergestate as the first function argument.
        """
        if _cache is None:
            _cache = {}
        key = timsort, func
        if key in _cache:
            return _cache[key]

        merge_init = timsort.merge_init

        @timsort.compile
        def wrapper(keys, values, *args):
            ms = merge_init(keys)
            res = func(ms, keys, values, *args)
            return res

        _cache[key] = wrapper
        return wrapper

        Wrap *func* into another compiled function inserting a runtime-created
        mergestate as the first function argument.
        
numba/tests/test_sort.py