Why is getting a function address so slow?

The code generation with the LLVM C API is relative fast.
But i noticed that a call to LLVMGetFunctionAddress takes a disporpotional time.
Why is that? Is there a way to reduce the time?

INFO   - typescript.jit       - read time: 0.000296
INFO   - typescrtip.jit       - parse time: 0.000257
INFO   - typescript.jit       - compile time: 0.002226
INFO   - typescript.module    - create mapping: 0.000008
INFO   - typescript.module    - get addr: 0.011029
INFO   - typescript.module    - main: 0.000050
let start = SystemTime::now();
let ns_ptr = Arc::into_raw(self.namespace.clone());
LLVMAddGlobalMapping(self.ee, self.namespace_ptr, ns_ptr as *mut _);

for ex in self.extern_functions.values() {
    LLVMAddGlobalMapping(self.ee, ex.func, ex.pointer);
let dur = start.elapsed().unwrap();
log::info!(target: "typescript.module", "create mapping: {}.{:06}", dur.as_secs(), dur.subsec_micros());

let start = SystemTime::now();
// !!! Here we are going real slow !!!
let addr = LLVMGetFunctionAddress(self.ee, b"__main__\0".as_ptr() as *const _);
let dur = start.elapsed().unwrap();
log::info!(target: "typescript.module", "get addr: {}.{:06}", dur.as_secs(), dur.subsec_micros());

let start = SystemTime::now();
let f: extern "C" fn() = std::mem::transmute(addr);

let dur = start.elapsed().unwrap();
log::info!(target: "typescript.module", "main: {}.{:06}", dur.as_secs(), dur.subsec_micros());

JIT compilation is deferred until first needed, in your case until you query the function address.
That’s why you measure such high cost on first Get, while subsequent calls will be cheap as expected.

1 Like

Ahhh, the more you know.
Thank you for the answer.