You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The implementations of Show for the various containers are basically showList (toList c). The default showList__ in GHC doesn't use foldr, so it has to create a list and then destroy it. Read uses readList, which has no opportunity to not build an intermediate list.
For Set and Seq, we really can't do better than this, because they use arbitrary Show/Read instances, so we're at their mercy. However, IntSet, IntMap, and Map use the stock instances for Int, (Int, a), and (k, a) respectively, and thus their showList and readList are as the Prelude says.
Show is easier to demonstrate in the limited form of an issue. I'll demonstrate Show IntSet first; the others are similar.
instanceShowIntSetwhereshowsPrec p s = showParens (p >10) $\st ->"fromList ["++foldr showOne (']':st) s where
showOne a sl =shows a $case sl of']':_ -> sl
_ ->',':sl
Read is more complex, but it basically involves parsing fromList and [, then reading the elements directly into a container separated by commas with the same mechanisms as fromList, followed by ]. The question, though, is how many people actually use the Read instances for these.
Yes, I expect these can be made faster, but I don't expect anyone is relying on Show and Read performance in practice.
But improving the Show instances should be quite harmless, so I am happy to review a PR for it.
instanceShowIntSetwhereshowsPrec p s = showParens (p >10) $\st ->"fromList ["++foldr showOne (']':st) s where
showOne a sl =shows a $case sl of']':_ -> sl
_ ->',':sl
This one isn't quite right, it is too strict. For the output to be generated lazily sl must not be inspected.
For Read, I am less convinced that having more complicated code is worth the trouble.
Oh, you're right. In the same vein, maybe it would be faster to remove a , at the end, rather than check and add it every time.
showsPrec2 p s =showParen (p >10) $showString"fromList [".
dropComma .
(\r ->IntSet.foldr (\x ->showChar','.shows x) r s) .showChar']'where
dropComma (',':r) = r
dropComma r = r
Activity
meooow25 commentedon Oct 21, 2026
Yes, I expect these can be made faster, but I don't expect anyone is relying on
ShowandReadperformance in practice.But improving the
Showinstances should be quite harmless, so I am happy to review a PR for it.This one isn't quite right, it is too strict. For the output to be generated lazily
slmust not be inspected.For
Read, I am less convinced that having more complicated code is worth the trouble.Zemyla commentedon Oct 21, 2026
showsis lazy in its second argument, so this is only inspecting the head ofslwhen it would be inspected anyway.meooow25 commentedon Oct 23, 2026
Oh, you're right. In the same vein, maybe it would be faster to remove a
,at the end, rather than check and add it every time.