Collapse sorted().first/last() and reversed().first/last() chains
Recipe ID
org.openrewrite.kotlin.performance.CollapseSortAndReverse$KtRecipeArtifact
io.moderne.recipe:recipes-kotlinReplaces O(n log n) sorts whose only purpose is to read one element with the equivalent O(n) min/max/minBy/maxBy, and elides unnecessary reversed() copies before first/last.
Usage
This recipe has no required configuration options. You’ll need the Moderne CLI configured before running the command below.
mod run . --recipe org.openrewrite.kotlin.performance.CollapseSortAndReverse$KtRecipeIf the recipe isn’t available locally, install it with:
mod config recipes jar install io.moderne.recipe:recipes-kotlin:0.2.0Definition
This recipe runs the following recipes in order.
- Use
min()instead ofsorted().first()org.openrewrite.kotlin.performance.UseMinForSortedFirst$KtRecipe - Use
max()instead ofsorted().last()org.openrewrite.kotlin.performance.UseMaxForSortedLast$KtRecipe - Use
max()instead ofsortedDescending().first()org.openrewrite.kotlin.performance.UseMaxForSortedDescendingFirst$KtRecipe - Use
min()instead ofsortedDescending().last()org.openrewrite.kotlin.performance.UseMinForSortedDescendingLast$KtRecipe - Use
minBy { selector }instead ofsortedBy { selector }.first()org.openrewrite.kotlin.performance.UseMinByForSortedByFirst$KtRecipe - Use
maxBy { selector }instead ofsortedBy { selector }.last()org.openrewrite.kotlin.performance.UseMaxByForSortedByLast$KtRecipe - Use
last()instead ofreversed().first()org.openrewrite.kotlin.performance.UseLastForReversedFirst$KtRecipe - Use
first()instead ofreversed().last()org.openrewrite.kotlin.performance.UseFirstForReversedLast$KtRecipe - Use
sortedByDescending { f }instead ofsortedBy { f }.reversed()org.openrewrite.kotlin.performance.UseSortedByDescendingForSortedByReversed$KtRecipe - Use
sortedDescending()instead ofsorted().reversed()org.openrewrite.kotlin.performance.UseSortedDescendingForSortedReversed$KtRecipe - Use
mapTo(mutableListOf(), f)instead ofmap(f).toMutableList()org.openrewrite.kotlin.performance.UseMapToForMapToMutableList$KtRecipe - Use
filterTo(mutableListOf(), p)instead offilter(p).toMutableList()org.openrewrite.kotlin.performance.UseFilterToForFilterToMutableList$KtRecipe - Use
filterNotTo(mutableListOf(), p)instead offilterNot(p).toMutableList()org.openrewrite.kotlin.performance.UseFilterNotToForFilterNotToMutableList$KtRecipe - Use
flatMapTo(mutableListOf(), f)instead offlatMap(f).toMutableList()org.openrewrite.kotlin.performance.UseFlatMapToForFlatMapToMutableList$KtRecipe
Data tables
Structured output this recipe can produce.
- Source files that had resultsSource files that were modified by the recipe run.
org.openrewrite.table.SourcesFileResults - Source files that had search resultsSearch results that were found during the recipe run.
org.openrewrite.table.SearchResults - Source files that errored on a recipeThe details of all errors produced by a recipe run.
org.openrewrite.table.SourcesFileErrors - Recipe performanceStatistics used in analyzing the performance of recipes.
org.openrewrite.table.RecipeRunStats