Skip to content

Some Optimisations#38

Merged
jordanmontt merged 6 commits intomasterfrom
optimisations
Dec 19, 2025
Merged

Some Optimisations#38
jordanmontt merged 6 commits intomasterfrom
optimisations

Conversation

@guillep
Copy link
Contributor

@guillep guillep commented Dec 18, 2025

This PR adds the following optimizations: partial instrumentation and avoiding array boxing.

Partial instrumentation

Instrument after/before only if the handler defines hooks for them. I did this trying to stay backwards compatible as much as possible.

This is implemented by adding specialized trap methods with more or less instrumentation, and selecting the trap method depending on the methods redefined by the handler. The downside of the approach is that the trap method is defined at proxy installation time, and if the handler changes in between adding new hooks (very unlikely), the trap will not take that into account.

Avoid array boxing

Method arguments as passed as normal individual arguments instead of boxing them into an array.
I extended the handler API to manage methods from 0 to 5 arguments in this way.
Methods with more than 5 arguments are still using the array boxing convention.

Benchmarks

I did four benches,

  • raw instumentation (an empty handler -- with optimized hooks if possible)
  • method count

on:

  • fibonachi (0 args, highly recursive) 20 benchFib.
    "Baseline -- no instrumentation"
    "88,067 iterations in 5 seconds 3 milliseconds. 17602.838 per second"

  • max: (1 arg) 1 max: 17
    "Baseline -- no instrumentation"
    "982,709,414 iterations in 5 seconds 4 milliseconds. 196384774.980 per second"

Base code:

handler :=  MpCountingHandler new.
p := MpMethodProxy 
	onMethod: Integer >> #benchFib 
	handler: handler.
p install.
p enableInstrumentation.
bench := [20 benchFib] bench.
p uninstall.

Fibonacci, empty instumentation

"Not optimized"
bench. "7,643 iterations in 5 seconds 2 milliseconds. 1527.989 per second"

"Optimized"
bench. "9,069 iterations in 5 seconds 2 milliseconds. 1813.075 per second"

9069 / 7643 = ~1.19x improvement

Fibonacci, counting

"Not optimized"
bench. "7,439 iterations in 5 seconds 2 milliseconds. 1487.205 per second"
handler count. "162847149"

"Optimized"
bench. "11,489 iterations in 5 seconds 2 milliseconds. 2296.881 per second"
handler count "251505699"

11489 / 7439 = 1.54x speedup.

max: empty instrumentation

"Not optimized"
bench. "187,399,224 iterations in 5 seconds 3 milliseconds. 37457370.378 per second"

"Optimized"
bench "208,645,555 iterations in 5 seconds 2 milliseconds. 41712426.030 per second"

208645555 / 187399224 = 1.11x speedup

max:, counting

"Not optimized"
bench "167,488,782 iterations in 5 seconds 1 millisecond. 33491058.188 per second".
handler count. "167488863"

"Optimized"
bench. "266,138,612 iterations in 5 seconds 2 milliseconds. 53206439.824 per second"
handler count "266138616"

266138612 / 167488782 = 1.58x speedup.

@guillep guillep mentioned this pull request Dec 18, 2025
@jordanmontt jordanmontt merged commit d67637a into master Dec 19, 2025
18 checks passed
@jordanmontt jordanmontt deleted the optimisations branch December 19, 2025 13:29
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