Scala Enumeration ValueSet.isEmpty slow

I use Scala Enumeration ValueSets with a fairly high throughput - creating, testing, combining and intersecting about 10M sets / second / core. I did not expect this to be a big problem, because I read somewhere that they are supported by BitSets, but surprisingly ValueSet.isEmpty appeared as a hot spot in the profiling session with YourKit.

To check, I decided to try and redefine what I need using Java BitSet, trying to keep some Scala Enumerations usage types safe. (code review has moved to https://codereview.stackexchange.com/questions/74795/scala-bitset-implemented-with-java-bitset-for-use-in-scala-enumerations-to-repl ). The good news is that changing my ValueSets to these BitSets really reduced 25% of my runtime, so I don’t know what ValueSet really does under the hood, but it can be improved ...

EDIT: A SourceSet valueSet scan indicates that isEmpty is definitely O (N) implemented using the generic SetLike.isEmpty. Given that ValueSet is implemented using BitSet, is this an error?

EDIT: It was a back trace from the profiler. This seems like a crazy way to implement isEmpty on a bitet.

Backtrace of hot spot in YourKit

+6
source share
1 answer

For the record, I’m all for looking under the hood, but this design requires too much of any mortal encoder.

Immortals, of course, have infinite time at their disposal.

Enumeration.ValueSet supported by BitSet , but is not itself. Something in favor of composition.

[Have you heard of the heir to the whole fortune that haunted all this in his love of music? He preferred the composition by inheritance. Did I just do this or hear it in Java One?]

Undoubtedly, ValueSet should delegate more to BitSet methods, including isEmpty.

I was going to suggest trying values.iterator.isEmpty , but these are just hasNext tests that pass all checks of all possible values.

https://github.com/scala/scala/blob/v2.11.4/src/library/scala/collection/BitSetLike.scala#L109

If I read it correctly.

The best option is e.values.toBitMask forall (_ == 0) , which is the moral equivalent of BitSet.isEmpty .

+1
source

Source: https://habr.com/ru/post/980107/


All Articles