Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data.List.upTo/applyUpTo can be made faster #2437

Open
Taneb opened this issue Jul 11, 2024 · 4 comments
Open

Data.List.upTo/applyUpTo can be made faster #2437

Taneb opened this issue Jul 11, 2024 · 4 comments

Comments

@Taneb
Copy link
Member

Taneb commented Jul 11, 2024

In a quick experiment, the following definition runs circles around the current implementation of upTo

goUpTo : List ℕ
goUpTo _ 0 = []
goUpTo n (suc i) = n ∷ goUpTo (suc n) i

upTo′ : List ℕ
upTo′ = goUpTo 0

I suspect this is because applyUpTo builds huge closures.

@jamesmckinna
Copy link
Contributor

(Deleted my original comment, but now reinstating it)
Can the implementation be further improved/speeded-up by considering a tail-call optimisation?

@Taneb
Copy link
Member Author

Taneb commented Jul 11, 2024

Can the implementation be further improved/speeded-up by considering a tail-call optimisation?

I don't think so. The recursion here is already guarded behind a constructor. I don't know what a tail recursive implementation would look like.

@MatthewDaggitt
Copy link
Contributor

Yes, I never had performance in mind when I add any of my definitions. Apologies! There's probably plenty more of these lurking in the library...

@jamesmckinna
Copy link
Contributor

jamesmckinna commented Jan 20, 2025

So...: instead have

upTo : List ℕ
upTo = go 0
  module UpTo where
    go : List ℕ
    go _ zero    = []
    go n (suc i) = n ∷ go (suc n) i

downFrom : List ℕ
downFrom zero    = []
downFrom (suc n) = n ∷ downFrom n

to avoid building the closures in applyUpTo/applyDownFrom? or is it easier simply to compute downFrom, and then compute upTo = reverse ∘ downFrom?

allFin seems trickier, because the closures, even if only specialised to powers of suc, do seem necessary? (although the reverse trick would again avoid such unpleasantness? UPDATED: er, no?)

Not clear what the knock-on effects on Data.List.Properties might be, but ...

@agda agda deleted a comment from linyue0103 Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants