All projects

MethodProxies

An instrumentation library to run your own code before, after or instead of any method, safely and fast.

Pharo, Instrumentation, JIT

The problem

Profilers, tracers and debuggers all need to intercept method calls. In a reflective language like Pharo, that is surprisingly easy to get wrong: the instrumentation code calls instrumented methods and recurses forever, exceptions and non-local returns skip the “after” hook, and naive approaches defeat the JIT compiler.

What I built

MethodProxies lets you run custom code before, after, instead of, or during the unwind of any method, without changing its source. You subclass a handler and decide what happens; the library takes care of the hard parts:

  • Meta-safety: instrumented code can call instrumented methods without infinite recursion.
  • Unwind-safety: exceptions and non-local returns still trigger the right hooks.
  • Per-process meta state, so it is safe with threads.
  • Install and remove at run time.

Results

The library integrates with the JIT and polymorphic inline caches through trap methods. It adds an average overhead of 1.54× and is on average 6.47× faster than an equivalent implementation that is not JIT-friendly. It is the instrumentation backend of Illimani and of the rest of my PhD work, and was published in the Journal of Computer Languages (2026).