<p dir="auto">I was hoping to find a routine in base that would give me the first <code class="notranslate">k</code> elements of <code class="notranslate">sortperm(v)</code>.</p>
<p dir="auto">This would be much like <code class="notranslate">select(v, 1:k)</code>, but instead of returning the actual elements, it would return the index where those elements can be found.</p>
<p dir="auto">Does such a function exist?</p>
<p dir="auto">One of the first things we need to do is make the runtime thread safe. This work is on the <code class="notranslate">threads</code> branch, and this tracker predates the new GC. I thought it is worth capturing a tracker that <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/StefanKarpinski/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/StefanKarpinski">@StefanKarpinski</a> prepared earlier in an issue to ease the thread safety work.</p>
<p dir="auto">This list is organized as <code class="notranslate">Variable; Approach</code></p>
<h1 dir="auto">builtins.c</h1>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> extern size_t jl_page_size;       constant</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> extern int jl_in_inference;       lock</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> extern int jl_boot_file_loaded;   constant</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> int in_jl_ = 0;                   thread-local</li>
</ul>
<h1 dir="auto">ccall.cpp</h1>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> static std::mapstd::stringstd::string sonameMap;        lock</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> static bool got_sonames = false;        lock, write-once</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> static std::mapstd::stringuv_lib_t* libMap; lock</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> static std::mapstd::stringGlobalVariable* libMapGV;     lock</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> static std::mapstd::stringGlobalVariable* symMapGV;     lock</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> <del>static char *temp_arg_area;                 thread-local (will be deleted very soon)</del></li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> <del>static const uint32_t arg_area_sz = 4196;   constant (will be deleted very soon)</del></li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> <del>static uint32_t arg_area_loc;     thread-local (will be deleted very soon)</del></li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> <del>static void *temp_arg_blocks[N_TEMP_ARG_BLOCKS];  thread-local (will be deleted very soon)</del></li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> <del>static uint32_t arg_block_n = 0;                  thread-local (will be deleted  very soon)</del></li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> <del>static Function *save_arg_area_loc_func;          constant (will be deleted very soon)</del></li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> <del>static Function *restore_arg_area_loc_func;       constant (will be deleted very soon)</del></li>
</ul>
<h1 dir="auto">cgutils.cpp</h1>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> static std::map&lt;const std::stringGlobalVariable*&gt; stringConstants;        lock</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> static std::map&lt;void*jl_value_llvm&gt; jl_value_to_llvm;                     lock</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> static std::map&lt;Value <em>void</em>&gt; llvm_to_jl_value;                           lock</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> static std::vector&lt;Constant*&gt; jl_sysimg_gvars;                            lock</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> static std::map&lt;intjl_value_t*&gt; typeIdToType;                             lock</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> jl_array_t *typeToTypeId;       lock</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> static int cur_type_id = 1;     lock</li>
</ul>
<h1 dir="auto">codegen.cpp</h1>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> void *__stack_chk_guard = NULL;   thread-local (jwn: why is this on the list? it's a constant and not thread local)</li>
</ul>
<h1 dir="auto">debuginfo.cpp</h1>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> extern "C" volatile int jl_in_stackwalk;</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> JuliaJITEventListener *jl_jit_events;</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> static obfiletype objfilemap;</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> extern char *jl_sysimage_name;    constant</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> static logdata_t coverageData;</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> static logdata_t mallocData;</li>
</ul>
<h1 dir="auto">dump.c</h1>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> static jl_array_t *tree_literal_values=NULL;      thread-local</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> static jl_value_t *jl_idtable_type=NULL;          constant</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> static jl_array_t *datatype_list=NULL;            thread 0 only</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> jl_value_t ***sysimg_gvars = NULL;                thread 0 only</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> extern int globalUnique;   thread 0 only</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> static size_t delayed_fptrs_n = 0;  thread 0 only</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> static size_t delayed_fptrs_max = 0;       thread 0 only</li>
</ul>
<h1 dir="auto">gc.c</h1>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> static volatile size_t allocd_bytes = 0;  thread-local</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> static volatile int64_t total_allocd_bytes = 0;   thread-local</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> static int64_t last_gc_total_bytes = 0;    thread-local</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> static size_t freed_bytes = 0;     barrier</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> static uint64_t total_gc_time=0;   barrier</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> int jl_in_gc=0; * referenced from switchto task.c        barrier</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> static htable_t obj_counts;   barrier</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> static size_t total_freed_bytes=0;          barrier</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> static arraylist_t to_finalize;             barrier</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> static jl_value_t **mark_stack = NULL;      barrier</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> static size_t mark_stack_size = 0;          barrier</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> static size_t mark_sp = 0;    barrier</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> extern jl_module_t *jl_old_base_module;     constant</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> extern jl_array_t *typeToTypeId;            barrier</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> extern jl_array_t *jl_module_init_order;    barrier</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> static int is_gc_enabled = 1;               atomic</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> static double process_t0;  constant</li>
</ul>
<h1 dir="auto">init.c</h1>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> char *jl_stack_lo;        thread-local</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> char *jl_stack_hi;        thread-local</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> volatile sig_atomic_t jl_signal_pending = 0;      thread-local</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> volatile sig_atomic_t jl_defer_signal = 0;        thread-local</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> uv_loop_t *jl_io_loop;                I/O thread ?</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> static void *signal_stack;            thread-local (see <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="54277793" data-permission-text="Title is private" data-url="https://github.com/JuliaLang/julia/issues/9763" data-hovercard-type="issue" data-hovercard-url="/JuliaLang/julia/issues/9763/hovercard?comment_id=69864396&amp;comment_type=issue_comment" href="https://github.com/JuliaLang/julia/issues/9763#issuecomment-69864396">#9763 (comment)</a>)</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> static mach_port_t segv_port = 0;     constant</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> extern void * __stack_chk_guard;      thread-local (duplicate of above)</li>
</ul>
<h1 dir="auto">jltypes.c</h1>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> int inside_typedef = 0;   thread-local</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> static int match_intersection_mode = 0;   thread-local</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> static int has_ntuple_intersect_tuple = 0;        thread-local</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> static int t_uid_ctr = 1;             lock</li>
</ul>
<h1 dir="auto">llvm-simdloop.cpp</h1>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> static unsigned simd_loop_mdkind = 0;     constant</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> static MDNode* simd_loop_md = NULL;       constant</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> char LowerSIMDLoop::ID = 0; lock</li>
</ul>
<h1 dir="auto">module.c</h1>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> jl_module_t *jl_main_module=NULL; constant</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> jl_module_t *jl_core_module=NULL; constant</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> jl_module_t *jl_base_module=NULL; constant</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> jl_module_t *jl_current_module=NULL;              thread-local</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> jl_array_t *jl_module_init_order = NULL;          lock (this code is bady broken anyways: <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="54539866" data-permission-text="Title is private" data-url="https://github.com/JuliaLang/julia/issues/9799" data-hovercard-type="issue" data-hovercard-url="/JuliaLang/julia/issues/9799/hovercard" href="https://github.com/JuliaLang/julia/issues/9799">#9799</a>)</li>
</ul>
<h1 dir="auto">profile.c</h1>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> static volatile ptrint_t* bt_data_prof = NULL;</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> static volatile size_t bt_size_max = 0;</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> static volatile size_t bt_size_cur = 0;</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> static volatile u_int64_t nsecprof = 0;</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> static volatile int running = 0;</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> volatile HANDLE hBtThread = 0;</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> static pthread_t profiler_thread;</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> static mach_port_t main_thread;</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> clock_serv_t clk;</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> static int profile_started = 0;</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> static mach_port_t profile_port = 0;</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> volatile static int forceDwarf = -2;</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> volatile mach_port_t mach_profiler_thread = 0;</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> static unw_context_t profiler_uc;</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> mach_timespec_t timerprof;</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> struct itimerval timerprof;</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> static timer_t timerprof;</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> static struct itimerspec itsprof;</li>
</ul>
<h1 dir="auto">sys.c</h1>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> JL_STREAM *JL_STDIN=0;    constant</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> JL_STREAM *JL_STDOUT=0;   constant</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> JL_STREAM *JL_STDERR=0;   constant</li>
</ul>
<h1 dir="auto">task.c</h1>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> volatile int jl_in_stackwalk = 0; thread-local</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> static size_t _frame_offset; constant</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> DLLEXPORT jl_task_t * volatile jl_current_task;   thread-local</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> jl_task_t *jl_root_task;       constant</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> jl_value_t * volatile jl_task_arg_in_transit;     thread-local</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> jl_value_t *jl_exception_in_transit;              thread-local</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> __JL_THREAD jl_gcframe_t *jl_pgcstack = NULL;     thread-local</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> jl_jmp_buf * volatile jl_jmp_target;  thread-local</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> extern int jl_in_gc;  barrier</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> static jl_function_t *task__hook_func=NULL;   constant</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> ptrint_t bt_data[MAX_BT_SIZE+1];                  thread-local</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> size_t bt_size = 0;                               thread-local</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> int needsSymRefreshModuleList;                    lock</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> jl_function_t *jl_unprotect_stack_func;           constant</li>
</ul>
<h1 dir="auto">toplevel.c</h1>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> int jl_lineno = 0;        thread-local</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> jl_module_t *jl_old_base_module = NULL;   constant</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> jl_module_t *jl_internal_main_module = NULL;              constant</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> extern int jl_in_inference;          lock</li>
</ul>