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

[ preformance ] implementation of unpack is inefficient #3280

Closed
stefan-hoeck opened this issue May 15, 2024 · 0 comments · Fixed by #3281
Closed

[ preformance ] implementation of unpack is inefficient #3280

stefan-hoeck opened this issue May 15, 2024 · 0 comments · Fixed by #3281

Comments

@stefan-hoeck
Copy link
Contributor

Steps to Reproduce

Typecheck the following example (this is a slightly adjusted example from a question on discord):

module Printf

import Data.String

%default total

data Format = FInt Format        -- %d
            | FString Format     -- %s
            | FOther Char Format
            | FEnd

format : List Char -> Format 
format Nil = FEnd
format ('%' :: 'd' :: xs) = FInt (format xs)
format ('%' :: 's' :: xs) = FString (format xs)
format (x :: xs) = FOther x (format xs)


0 InterpFormat : Format -> Type 
InterpFormat (FInt f)     = Int    -> InterpFormat f
InterpFormat (FString f)  = String -> InterpFormat f
InterpFormat (FOther _ f) = InterpFormat f
InterpFormat FEnd         = String

formatString : String -> Format 
formatString s = format (unpack s)

toFunction : (fmt : Format) -> String -> InterpFormat fmt
toFunction (FInt x) str     = \y => toFunction x (str ++ show y)
toFunction (FString x) str  = \y => toFunction x (str ++ y)
toFunction (FOther c x) str = toFunction x (str ++ singleton c)
toFunction FEnd str         = str

printf : (s : String) -> InterpFormat (formatString s)
printf s = toFunction (formatString s) ""

message : String
message = printf "My name is %s and I am %d years old"

Expected Behavior

Fails with a type error after a short time.

Observed Behavior

Consumes all my system's memory and dies after a couple of minutes.

The reason seems to be the implementation of unpack: For shorter messages, it produces a very large syntax tree (at least quadratic w.r.t. to the length of the string). It was pretty straight forward to come up with a different implementation of unpack that performs much better at compile time. I'll submit a PR in a moment.

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 a pull request may close this issue.

1 participant