My Intellij is suggesting to add a .`asSquence` be...
# announcements
h
My Intellij is suggesting to add a .
asSquence
before calling
groupBy
on this function that I wrote, but I can't find any evidence to prove if this is a good idea or not. I wonder if anyone here can suggest if this is a good idea. So basically here is what the code look like
Copy code
//BEFORE adding.asSequence()

                        override fun handle(event: List<String>): Either<Errors.RequestError, Pair<List<Errors.RequestError>, Int>> =
            emailParser.parseEmails(event)
                    .map {
                        Pair(
                                first = it.first,
                                second = it.second
                                        .groupBy { it.emailAdrress }
                                        .map {
                                            Pair(
                                                    first = MessageDestination(it.key),
                                                    second = it.value.map {
                                                        MessageSender(
                                                                message = it.environment
                                                        )
                                                    }
                                            )
                                        }.toMap()
                        )
                    }.flatMap { pair ->
                        service.handleRequests(pair.second)
                                .map {
                                    Pair(pair.first, pair.second.size)
                                }
                    }




 //AFTER adding .asSequence()
   override fun handle(event: List<String>): Either<Errors.RequestError, Pair<List<Errors.RequestError>, Int>> =
            emailParser.parseEmails(event)
                    .map {
                        Pair(
                                first = it.first,
                                second = it.second
                                        .asSequence()
                                        .groupBy { it.emailAdrress }
                                        .map {
                                            Pair(
                                                    first = MessageDestination(it.key),
                                                    second = it.value.map {
                                                        MessageSender(
                                                                message = it.environment
                                                        )
                                                    }
                                            )
                                        }
                                        .toList().toMap()
                        )
                    }.flatMap { pair ->
                        service.handleRequests(pair.second)
                                .map {
                                    Pair(pair.first, pair.second.size)
                                }
                    }
m
Each step would create a new list if you don't use a sequence instead
(correct me if i'm wrong, i just recently learned about them, too)
s
this should probably be in a snippet rather than a multiline block
also use
.associate { ... }
rather than mapping and then calling
.toMap()
on the resulting list
also, what do you mean you “can’t find evidence to prove if this is a good idea or not”
like are you looking for explanations on how kotlin sequences work, or did you benchmark the two approaches maybe?
h
thanks @Shawn I have seen a blog about it here: https://blog.kotlin-academy.com/effective-kotlin-use-sequence-for-bigger-collections-with-more-than-one-processing-step-649a15bb4bf but my main problem is I can't tell as I dont have any metrics to see if its faster or not
@Shawn shall I create large collection of list of Strings and test it?
s
There’s a lot of nuance to JVM benchmarking that I’m personally not familiar with
you might wanna look up a guide or something before trying your hand at a comparison
d
For benchmarking you need to get a high number of samples of your target. On the JVM, it would be wise to run the code a lot of times to warm it up and allow the jitter to do its work before you Benchmark. The resolution of nanoTime() is generally not better than 40 nano seconds.