Skip to content

Commit bb88a43

Browse files
authored
Remove fast* collection utils (#1620)
1 parent 4a069fe commit bb88a43

16 files changed

Lines changed: 131 additions & 232 deletions

compiler/src/main/kotlin/dev/zacsweers/metro/compiler/MetroAnnotations.kt

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -257,25 +257,25 @@ private fun IrAnnotationContainer.metroAnnotations(
257257
var qualifier: IrAnnotation? = null
258258
val mapKeys = mutableSetOf<IrAnnotation>()
259259

260-
annotations.fastForEach { annotation ->
261-
val annotationClass = annotation.type.classOrNull?.owner ?: return@fastForEach
262-
val classId = annotationClass.classId ?: return@fastForEach
260+
for (annotation in annotations) {
261+
val annotationClass = annotation.type.classOrNull?.owner ?: continue
262+
val classId = annotationClass.classId ?: continue
263263

264264
when (this) {
265265
is IrValueParameter -> {
266266
// Only BindsInstance and Assisted go here
267267
when (classId) {
268268
in ids.providesAnnotations if (Kind.Provides in kinds) -> {
269269
isBindsInstance = true
270-
return@fastForEach
270+
continue
271271
}
272272
in ids.assistedAnnotations if (Kind.Assisted in kinds) -> {
273273
assisted = expectNullAndSet("assisted", assisted, annotation.asIrAnnotation())
274-
return@fastForEach
274+
continue
275275
}
276276
in ids.optionalBindingAnnotations if (Kind.OptionalBinding in kinds) -> {
277277
isOptionalBinding = true
278-
return@fastForEach
278+
continue
279279
}
280280
}
281281
}
@@ -286,39 +286,39 @@ private fun IrAnnotationContainer.metroAnnotations(
286286
when (classId) {
287287
in ids.bindsAnnotations if (Kind.Binds in kinds) -> {
288288
isBinds = true
289-
return@fastForEach
289+
continue
290290
}
291291
in ids.providesAnnotations if (Kind.Provides in kinds) -> {
292292
isProvides = true
293-
return@fastForEach
293+
continue
294294
}
295295
in ids.intoSetAnnotations if (Kind.IntoSet in kinds) -> {
296296
isIntoSet = true
297-
return@fastForEach
297+
continue
298298
}
299299
in ids.elementsIntoSetAnnotations if (Kind.ElementsIntoSet in kinds) -> {
300300
isElementsIntoSet = true
301-
return@fastForEach
301+
continue
302302
}
303303
in ids.intoMapAnnotations if (Kind.IntoMap in kinds) -> {
304304
isIntoMap = true
305-
return@fastForEach
305+
continue
306306
}
307307
in ids.multibindsAnnotations -> {
308308
multibinds = expectNullAndSet("multibindings", multibinds, annotation.asIrAnnotation())
309-
return@fastForEach
309+
continue
310310
}
311311
Symbols.ClassIds.Composable if (Kind.Composable in kinds) -> {
312312
isComposable = true
313-
return@fastForEach
313+
continue
314314
}
315315
DaggerSymbols.ClassIds.DAGGER_BINDS_OPTIONAL_OF if (Kind.BindsOptionalOf in kinds) -> {
316316
isBindsOptionalOf = true
317-
return@fastForEach
317+
continue
318318
}
319319
in ids.optionalBindingAnnotations if (Kind.OptionalBinding in kinds) -> {
320320
isOptionalBinding = true
321-
return@fastForEach
321+
continue
322322
}
323323
}
324324
}
@@ -328,15 +328,15 @@ private fun IrAnnotationContainer.metroAnnotations(
328328
when (classId) {
329329
in ids.assistedFactoryAnnotations if (Kind.AssistedFactory in kinds) -> {
330330
isAssistedFactory = true
331-
return@fastForEach
331+
continue
332332
}
333333
in ids.dependencyGraphAnnotations if (Kind.DependencyGraph in kinds) -> {
334334
isDependencyGraph = true
335-
return@fastForEach
335+
continue
336336
}
337337
in ids.dependencyGraphFactoryAnnotations if (Kind.DependencyGraphFactory in kinds) -> {
338338
isDependencyGraphFactory = true
339-
return@fastForEach
339+
continue
340340
}
341341
}
342342
}
@@ -346,25 +346,25 @@ private fun IrAnnotationContainer.metroAnnotations(
346346

347347
if (classId in ids.injectAnnotations) {
348348
isInject = true
349-
return@fastForEach
349+
continue
350350
}
351351

352352
if (Kind.AssistedInject in kinds && classId in ids.assistedInjectAnnotations) {
353353
isAssistedInject = true
354-
return@fastForEach
354+
continue
355355
}
356356

357357
if (Kind.Scope in kinds && annotationClass.isAnnotatedWithAny(ids.scopeAnnotations)) {
358358
scope = expectNullAndSet("scope", scope, annotation.asIrAnnotation())
359-
return@fastForEach
359+
continue
360360
} else if (
361361
Kind.Qualifier in kinds && annotationClass.isAnnotatedWithAny(ids.qualifierAnnotations)
362362
) {
363363
qualifier = expectNullAndSet("qualifier", qualifier, annotation.asIrAnnotation())
364-
return@fastForEach
364+
continue
365365
} else if (Kind.MapKey in kinds && annotationClass.isAnnotatedWithAny(ids.mapKeyAnnotations)) {
366366
mapKeys += annotation.asIrAnnotation()
367-
return@fastForEach
367+
continue
368368
}
369369
}
370370

compiler/src/main/kotlin/dev/zacsweers/metro/compiler/collectionUtil.kt

Lines changed: 7 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package dev.zacsweers.metro.compiler
1717

18-
import kotlin.contracts.contract
19-
2018
internal fun <T> Iterable<T>.filterToSet(predicate: (T) -> Boolean): Set<T> {
2119
return filterTo(mutableSetOf(), predicate)
2220
}
@@ -69,98 +67,18 @@ internal fun <T, R> Iterable<T>.mapToSetWithDupes(transform: (T) -> R): Pair<Set
6967
}
7068

7169
internal fun <T> List<T>.filterToSet(predicate: (T) -> Boolean): Set<T> {
72-
return fastFilterTo(mutableSetOf(), predicate)
70+
return filterTo(mutableSetOf(), predicate)
7371
}
7472

7573
internal fun <T, R> List<T>.mapToSet(transform: (T) -> R): Set<R> {
76-
return fastMapTo(mutableSetOf(), transform)
77-
}
78-
79-
internal inline fun <T> List<T>.fastForEach(action: (T) -> Unit) {
80-
contract { callsInPlace(action) }
81-
for (index in indices) {
82-
val item = get(index)
83-
action(item)
84-
}
85-
}
86-
87-
internal inline fun <T> List<T>.fastForEachIndexed(action: (Int, T) -> Unit) {
88-
contract { callsInPlace(action) }
89-
for (index in indices) {
90-
val item = get(index)
91-
action(index, item)
92-
}
93-
}
94-
95-
internal inline fun <T, R, C : MutableCollection<in R>> List<T>.fastMapTo(
96-
destination: C,
97-
transform: (T) -> R,
98-
): C {
99-
contract { callsInPlace(transform) }
100-
fastForEach { item -> destination.add(transform(item)) }
101-
return destination
102-
}
103-
104-
internal inline fun <T> List<T>.fastFilter(predicate: (T) -> Boolean): List<T> {
105-
contract { callsInPlace(predicate) }
106-
return fastFilterTo(ArrayList(size), predicate)
107-
}
108-
109-
internal inline fun <T, C : MutableCollection<in T>> List<T>.fastFilterTo(
110-
destination: C,
111-
predicate: (T) -> Boolean,
112-
): C {
113-
contract { callsInPlace(predicate) }
114-
fastFilteredForEach(predicate) { destination.add(it) }
115-
return destination
116-
}
117-
118-
internal inline fun <T> List<T>.fastFilterNot(predicate: (T) -> Boolean): List<T> {
119-
contract { callsInPlace(predicate) }
120-
return fastFilterNotTo(ArrayList(size), predicate)
121-
}
122-
123-
internal inline fun <T, C : MutableCollection<in T>> List<T>.fastFilterNotTo(
124-
destination: C,
125-
predicate: (T) -> Boolean,
126-
): C {
127-
contract { callsInPlace(predicate) }
128-
fastForEach { item -> if (!predicate(item)) destination.add(item) }
129-
return destination
130-
}
131-
132-
internal inline fun <T> List<T>.fastFilteredForEach(
133-
predicate: (T) -> Boolean,
134-
action: (T) -> Unit,
135-
) {
136-
contract {
137-
callsInPlace(predicate)
138-
callsInPlace(action)
139-
}
140-
fastForEach { item -> if (predicate(item)) action(item) }
141-
}
142-
143-
internal inline fun <T, R> List<T>.fastFilteredMap(
144-
predicate: (T) -> Boolean,
145-
transform: (T) -> R,
146-
): List<R> {
147-
contract {
148-
callsInPlace(predicate)
149-
callsInPlace(transform)
150-
}
151-
val target = ArrayList<R>(size)
152-
fastForEach { if (predicate(it)) target += transform(it) }
153-
return target
154-
}
155-
156-
internal inline fun <T> List<T>.fastAny(predicate: (T) -> Boolean): Boolean {
157-
contract { callsInPlace(predicate) }
158-
fastForEach { if (predicate(it)) return true }
159-
return false
74+
return mapTo(mutableSetOf(), transform)
16075
}
16176

16277
internal fun <T> List<T>.allElementsAreEqual(): Boolean {
16378
if (size < 2) return true
164-
val firstElement = first()
165-
return !fastAny { it != firstElement }
79+
val firstElement = get(0)
80+
for (i in 1 until size) {
81+
if (get(i) != firstElement) return false
82+
}
83+
return true
16684
}

compiler/src/main/kotlin/dev/zacsweers/metro/compiler/fir/checkers/AggregationChecker.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ package dev.zacsweers.metro.compiler.fir.checkers
44

55
import dev.drewhamilton.poko.Poko
66
import dev.zacsweers.metro.compiler.MetroOptions
7-
import dev.zacsweers.metro.compiler.fastFilterNot
8-
import dev.zacsweers.metro.compiler.fastForEach
97
import dev.zacsweers.metro.compiler.fir.FirTypeKey
108
import dev.zacsweers.metro.compiler.fir.MetroDiagnostics
119
import dev.zacsweers.metro.compiler.fir.MetroFirAnnotation
@@ -71,11 +69,11 @@ internal object AggregationChecker : FirClassChecker(MppCheckerKind.Common) {
7169

7270
val classQualifier = declaration.annotations.qualifierAnnotation(session)
7371

74-
declaration.annotations.fastForEach { annotation ->
75-
if (!annotation.isResolved) return@fastForEach
76-
val classId = annotation.toAnnotationClassId(session) ?: return@fastForEach
72+
for (annotation in declaration.annotations) {
73+
if (!annotation.isResolved) continue
74+
val classId = annotation.toAnnotationClassId(session) ?: continue
7775
if (classId in classIds.allContributesAnnotations) {
78-
val scope = annotation.resolvedScopeClassId() ?: return@fastForEach
76+
val scope = annotation.resolvedScopeClassId() ?: continue
7977
val replaces = emptySet<ClassId>() // TODO implement
8078
val checkIntoSet by memoize {
8179
checkBindingContribution(
@@ -128,6 +126,7 @@ internal object AggregationChecker : FirClassChecker(MppCheckerKind.Common) {
128126
return
129127
}
130128
}
129+
131130
in classIds.contributesBindingAnnotations -> {
132131
val valid =
133132
checkBindingContribution(
@@ -153,16 +152,19 @@ internal object AggregationChecker : FirClassChecker(MppCheckerKind.Common) {
153152
return
154153
}
155154
}
155+
156156
in classIds.contributesIntoSetAnnotations -> {
157157
if (!checkIntoSet) {
158158
return
159159
}
160160
}
161+
161162
in classIds.contributesIntoMapAnnotations -> {
162163
if (!checkIntoMap) {
163164
return
164165
}
165166
}
167+
166168
in classIds.customContributesIntoSetAnnotations -> {
167169
val isMapBinding = declaration.annotations.mapKeyAnnotation(session) != null
168170
val valid = if (isMapBinding) checkIntoMap else checkIntoSet
@@ -205,7 +207,7 @@ internal object AggregationChecker : FirClassChecker(MppCheckerKind.Common) {
205207
return false
206208
}
207209

208-
val supertypesExcludingAny = declaration.superTypeRefs.fastFilterNot { it.coneType.isAny }
210+
val supertypesExcludingAny = declaration.superTypeRefs.filterNot { it.coneType.isAny }
209211
val hasSupertypes = supertypesExcludingAny.isNotEmpty()
210212

211213
val explicitBindingType = annotation.resolvedBindingArgument(session)

compiler/src/main/kotlin/dev/zacsweers/metro/compiler/fir/checkers/AssistedInjectChecker.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
package dev.zacsweers.metro.compiler.fir.checkers
44

55
import dev.zacsweers.metro.compiler.ClassIds
6-
import dev.zacsweers.metro.compiler.fastFilter
7-
import dev.zacsweers.metro.compiler.fastForEachIndexed
86
import dev.zacsweers.metro.compiler.fir.FirTypeKey
97
import dev.zacsweers.metro.compiler.fir.MetroDiagnostics.ASSISTED_INJECTION_ERROR
108
import dev.zacsweers.metro.compiler.fir.annotationsIn
@@ -120,7 +118,7 @@ internal object AssistedInjectChecker : FirClassChecker(MppCheckerKind.Common) {
120118

121119
val functionParams = function.valueParameterSymbols
122120
val constructorAssistedParams =
123-
injectConstructor.constructor?.valueParameterSymbols.orEmpty().fastFilter {
121+
injectConstructor.constructor?.valueParameterSymbols.orEmpty().filter {
124122
it.isAnnotatedWithAny(session, classIds.assistedAnnotations)
125123
}
126124

@@ -140,7 +138,7 @@ internal object AssistedInjectChecker : FirClassChecker(MppCheckerKind.Common) {
140138
val factorySubstitutionMap = mutableMapOf<FirTypeParameterSymbol, ConeKotlinType>()
141139

142140
// Map factory type parameters to the same concrete types
143-
declaration.typeParameters.fastForEachIndexed { index, factoryTypeParam ->
141+
declaration.typeParameters.forEachIndexed { index, factoryTypeParam ->
144142
val targetTypeParam = targetType.typeParameters.getOrNull(index)
145143
if (targetTypeParam != null) {
146144
// Use the concrete type from the return type if available

0 commit comments

Comments
 (0)