Skip to content

Optimize mapMonotonic by using an Fmap class instead of Functor#140

Merged
konsumlamm merged 1 commit intolspitzner:masterfrom
konsumlamm:fmap
Feb 28, 2025
Merged

Optimize mapMonotonic by using an Fmap class instead of Functor#140
konsumlamm merged 1 commit intolspitzner:masterfrom
konsumlamm:fmap

Conversation

@konsumlamm
Copy link
Copy Markdown
Collaborator

Resolves #55.

This is the same optimization as in #59. Due to the recursive nature of our instances, the Functor instance built a lot of unnecessary <$s, which are avoided by the new Fmap class. This is the only such remaining instance that I found.

This change improves the performance of mapMonotonic significantly (see benchmarks below).

Benchmark results

Benchmarking mapMonotonic (+ 1) on a MinQueue Int with n random elements.
Before:

All
  mapMonotonic
    1000:    OK
      10.8 μs ± 286 ns
    10000:   OK
      129  μs ± 7.5 μs
    100000:  OK
      3.79 ms ± 230 μs
    1000000: OK
      98.9 ms ± 6.8 ms

After:

All
  mapMonotonic
    1000:    OK
      6.69 μs ± 398 ns, 37% less than baseline
    10000:   OK
      76.6 μs ± 2.4 μs, 40% less than baseline
    100000:  OK
      3.13 ms ± 212 μs, 17% less than baseline
    1000000: OK
      85.0 ms ± 6.0 ms, 14% less than baseline

@treeowl
Copy link
Copy Markdown
Collaborator

treeowl commented Feb 28, 2025

Good catch!

@konsumlamm konsumlamm merged commit ef1a6f3 into lspitzner:master Feb 28, 2025
@konsumlamm konsumlamm deleted the fmap branch February 28, 2025 18:43
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.

Make sure we're not building unnecessarily large dictionaries

2 participants