unisonweb / unison

A friendly programming language from the future
https://unison-lang.org
Other
5.73k stars 266 forks source link

pretty printer is really slow with long Text #3402

Closed ceedubs closed 2 months ago

ceedubs commented 1 year ago

The pretty-printer is really slow for long Text values. I could see the pretty-printer being slow when printing nested data structures in which it's attempting to align neatly, but it's slow even with just a flat Text value.

As you increase the length of the string, the slow down seems to be greater than linear. Consider the following watch expression:

> let
  n = 1000
  Stream.range 0 n |> Stream.fold! (acc -> i -> acc ++ " " ++ (Nat.toText i)) ""

On my computer here are the results:

n time to render
1000 near instant
5000 a few seconds
10000 ~25 seconds

Normally you probably aren't printing huge strings in watch expressions, so it's probably not that big of a deal. One context in which it can be a pain is when a program throws a Failure that has a data structure with a long string inside the Any value. This can cause your ucm session to hang for minutes at a time.

pchiusano commented 1 year ago

Also, I was curious if there's maybe some problem with Text.++ but if you don't print out the result at the end, it's instantaneous for all three.

ceedubs commented 2 months ago

I have no idea why, but this is way better now. For n = 10k I get 500ms instead of 25 seconds. For n = 100k it is on the order of a second or two.