Standard Library: Now open for improvements and suggestions!

Hmm. Very mixed feelings about (1). On the one hand, I definitely get where you’re coming from; in principle, I suspect you’re right. OTOH, I have a nasty feeling that the Scala ecosystem as a whole has the Seq == List equivalence baked in in a billion places. (In this instance, I’m even more worried about applications than libraries.)

If it does happen, it would need (IMO) to be accompanied by some sort of hard-assed scalafix utility that looks for inappropriate casts. (If that’s even possible – I haven’t done enough with scalafix to know if that would be a straightforward rule to write.) Otherwise, I worry about people hitting mysterious runtime exceptions.

Also, I worry a bit about serialization problems. I don’t know how common it is for people to serialize raw Seq, but it wouldn’t surprise me if it’s fairly common, and it might well have incorrect assumptions baked in.

1 Like

I am mostly concerned people are using Seq just because it is short and easy to type. And then they might be even indexing into it, forgetting that it is O(n) and trashes all the caches to top it off.

It is all about incentives. Someone (maybe even Martin Odersky) said that asInstanceOf is so long and hard to type because your are not supposed to do it often. We should make the correct data structures be easy to type and use.

One more thing, I have recently seen we have introduced IArray in Scala 3, which is further confusing, as it seems to be very similar to ArraySeq.

I feel all this needs to simplified / unified.

3 Likes

I regularly try Vector to replace List in some code that has a significant time use in my profiler, and Vector never clearly wins. Its often slower or has no measurable benefit. I don’t believe the guaranteed[1] cache locality of Vectors outweighs the generally more expensive operations.

This clearly does not mean that List is always better, just that changing the default might be quite invasive on programs that do use Seq.


  1. List may also have cache locality, if the elements are allocated closely together, which is likely with the way ListBuilder works. Also, I believe the JVM GCs can and will consider access patterns when moving memory around to improve cache locality on hot paths. ↩︎

2 Likes

Early on we tried to change List in the Scala compiler to Vector. It got ~ 10% slower. YMMV, but that also shows it’s not a clear-cut win.

1 Like

How “early on” was that? Modern Vector (since Scala 2.13.2 in 2020) is a rather different beast than older Vectors. In particular, small vectors are more compactly represented.

HotSpot is a moving target as well, of course. We’ve just moved to 17+, and users who value performance should be (and likely are) on 21 or 25.

2 Likes

I guess it was around 2016? Not sure. Agreed that thuings might have changed since then.

  • Martin
1 Like

Vector is not the only option. If you only do filter, map, flatMap, like what you would expect on Seq, ArraySeq / IArray / Array should be a clear winners.

Also I assume for comprehensions unnecessarily wrap Array and IArray, so I hope this is fixed someday.

normally no, map, flatMap and withFilter on arrays are extension methods that return Array/IArray - if you do see an example that doesnt follow this then it might be an implicit conversion for a function that wasnt special cased

2 Likes

Another suggestion: sumBy(f)

I have checked my codebases and almost never I use .sum alone. It is always in combination with .map(f).sum. I know foldLeft exists, but it is not as readable.

This is especially useful on Set[T], because you can introduce a bug if you just call map, you must use .iterator.map(f).sum to avoid deduplication.

6 Likes

we merged in a change to make -> fully inlined from 3.10.0!

11 Likes

more merged PRs for 3.10:

9 Likes

I have just learned that Enum.values does a defensive .clone() of Array on every access. It is not very intuitive and could have bad performance implications.

Proposal: Introduce an alternative method that returns IArray. Also add @deprecated on Enum.values.

5 Likes

That’s called foldMap in cats, and it works on every F[A] where F[_] is a Foldable and A is a Monoid. So maybe it’s time to have at least some basic type classes like that in the standard library.

3 Likes

Could sumBy(f) be smart about handling (grouping small) floating point values when traversing structures that make it possible? Or does that make things too crazy?

I think it would be great to add andThen for functions of any arity. It can be done directly in interfaces or as an extension method using experimental TupledFunction:

extension [F, Args <: Tuple, R](f: F)(using tf: TupledFunction[F, Args => R])
    def andThenN[R2, FOut](g: R => R2)(using tfOut: TupledFunction[FOut, Args => R2]): FOut =
      tfOut.untupled(args => g(tf.tupled(f)(args)))
1 Like

Scala already has a non-empty list type in its standard library: ::[A] . However, operations like map return List[B] , losing the non-emptiness guarantee at the type level.

There seems to be no fundamental reason for this. ::[A] could override map to return ::[B] , and similarly preserve non-emptiness in other operations where mathematically guaranteed.

This would effectively give us much of what Cats NonEmptyList provides, without introducing another collection type or an external dependency.

7 Likes

You have a point there, but I think there is one aspect we might want to consider, and it’s how enum works. Ideally List would just be a simple enum.

enum List[+A]:
  case Nil
  case ::(head: A, tail: List[A])

It’s not, but that’s for historical reasons: enum didn’t exist when List was written. And the thing with enum is that the individual cases behave a little different from the old case classes. When you just write something like List.::(0, List.Nil), the type will be List[Int] and not List.::[Int], unless you explicitly ascribe that type. So it would at least be inconvenient to do it that way.

Of course you’re going to say that List isn’t an enum today, but IMO that’s a historical wart that we’d ideally fix some day, and using :: this way makes that more difficult than it already is. Of course the counter-argument here would be that we’d be giving up something in the present for a nicer future that might never come. So I’m on the fence for this myself.

You’d also have to re-export List.:: and List.Nil but maybe not a huge issue.

But yeah 1 :: 2 :: Nil already today returns List[Int] and not ::[Int].

The reason is likely the type inference issue where precise types are not always what you want e.g. when instantiating an invariant type - so List[Int] is typically what you want.

Also from a user’s perspective, ::[Int] is a much more cryptic type than List[Int] !

5 Likes

Nice idea, but why would this feature be limited to just Lists? It feels like a clever non-orthogonal hack. Maybe it’s worth researching a solution that will apply to non-empty collections in general.

And this is at the time when we know that List should generally be avoided as a data structure on modern CPUs, unless most of your collections are up to 2 elements long or for branching recursive algorithms.

4 Likes