Improve generic enum generation

I would like to quote a file or a gist, but my company firewall is denying any such action.

Basically I have a very big enum (like ~200 constructors). I derive Generic for it and instances for Enum, Bounded and BoundedEnum (by generic… counterparts of typeclass functions). I run spago build… and wait.

Compiling the module takes about 3-4 minutes, taking 11GB of RAM at peak, and resulting .js file has enormous 15MB. Parcel takes another few minutes to package it after that. When I comment most of the constructors leaving 30-40, it compiles in 3 secs.

Am I doing something wrong, or is it a real issue? For now I think I will just create an array of all constructors and implement all functions by hand.

4 Likes

I’m pretty confident that this is an actual issue, and I remember reading about it somewhere. I’ll post a link if I find anything.

2 Likes

I found https://github.com/purescript/purescript/issues/3475 and https://github.com/purescript/purescript/issues/3948.

4 Likes

Thanks. Yeah, that seems like a big issue. Even Eq and Ord typeclasses alone weigh a bit - Ord has a quadratic number of ifs to generate.

I managed to make it performant by making an array of “all objects”, and expressing everything in terms of index/lookup. Not elegant, but compiles in a blink. Not sure why the former issue got closed - not having a good proposal does not mean the issue is nonexistent, it is quite real.

1 Like

It was closed because it’s not practically useful to track every thing we might potentially want to do in the compiler repo; we’d quickly be overwhelmed if we did that. We’ve found that it works well to close things if they don’t appear to have a lot of interest and/or are missing an outline of how we would go about even addressing them, because while they are in that state they’re unlikely to be worked on anyway and it’s hard to work out if it’s something we even want to do.

1 Like

I think it’s a matter of someone finding time to work on it.

An interesting thought I had - for this particular issue, people usually stumble across it when using Purescript in production on large commercial apps (just like I did). If only the impacted companies could be persuaded to allocate resources towards solving it instead of just working around it :slight_smile:

3 Likes

I understand that this issue would improve a lot if the generated Generic instances were more tree-like, not list-like. How about teaching the compiler to generate it like that, e.g. cut the definitions in half and make a sum of both halves, instead of appending elements to Sum one by one? What are the advantages/disadvantages?

Yeah. In my case I’m doing a proof-of-concept of rewriting our frontend desktop app into web, and I’m happy to have enough resources to do so. So I think I already spent my budget :smiley: having to make tweaks to the language to make it suit our use case would be a little too much.

Hah apologies if it seemed like I was targeting you. I was in the same boat, working on a commercial product and I was unable to procure the resources as well, so this was more of a general rant :slight_smile:

1 Like