Skip to content

Commit 4a5c41a

Browse files
committed
chore: replace invocations of Array.toList with Array.asList to slightly reduce some copy overhead
An unnecessary `toList` conversion was removed instead of replaced.
1 parent 64bb20e commit 4a5c41a

File tree

14 files changed

+33
-29
lines changed

14 files changed

+33
-29
lines changed

exposed-core/src/main/kotlin/org/jetbrains/exposed/v1/core/OpBuilder.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,13 +658,15 @@ fun <T> anyFrom(subQuery: AbstractQuery<*>): Op<T> = AllAnyFromSubQueryOp(true,
658658
* **Note** If [delegateType] is left `null`, the base column type associated with storing elements of type [T] will be
659659
* resolved according to the internal mapping of the element's type in [resolveColumnType].
660660
*
661+
* @param array Wrapped in the resulting [Op] to improve performance. Do not mutate after passing to this function, otherwise pass the list converted using [Array.toList].
662+
*
661663
* @throws IllegalStateException If no column type mapping is found and a [delegateType] is not provided.
662664
*/
663665
inline fun <reified T : Any> anyFrom(array: Array<T>, delegateType: ColumnType<T>? = null): Op<T> {
664666
// emptyArray() without type info generates ARRAY[]
665667
@OptIn(InternalApi::class)
666668
val columnType = delegateType ?: resolveColumnType(T::class, if (array.isEmpty()) TextColumnType() else null)
667-
return AllAnyFromArrayOp(true, array.toList(), columnType)
669+
return AllAnyFromArrayOp(true, array.asList(), columnType)
668670
}
669671

670672
/**
@@ -697,13 +699,15 @@ fun <T> allFrom(subQuery: AbstractQuery<*>): Op<T> = AllAnyFromSubQueryOp(false,
697699
* **Note** If [delegateType] is left `null`, the base column type associated with storing elements of type [T] will be
698700
* resolved according to the internal mapping of the element's type in [resolveColumnType].
699701
*
702+
* @param array Wrapped in the resulting [Op] to improve performance. Do not mutate after passing to this function, otherwise pass the list converted using [Array.toList].
703+
*
700704
* @throws IllegalStateException If no column type mapping is found and a [delegateType] is not provided.
701705
*/
702706
inline fun <reified T : Any> allFrom(array: Array<T>, delegateType: ColumnType<T>? = null): Op<T> {
703707
// emptyArray() without type info generates ARRAY[]
704708
@OptIn(InternalApi::class)
705709
val columnType = delegateType ?: resolveColumnType(T::class, if (array.isEmpty()) TextColumnType() else null)
706-
return AllAnyFromArrayOp(false, array.toList(), columnType)
710+
return AllAnyFromArrayOp(false, array.asList(), columnType)
707711
}
708712

709713
/**

exposed-core/src/main/kotlin/org/jetbrains/exposed/v1/core/SchemaUtilityApi.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ abstract class SchemaUtilityApi {
553553
@InternalApi
554554
object TableUtils : SchemaUtilityApi() {
555555
/** Checks whether any of the [tables] have a sequence of foreign key constraints that cycle back to them. */
556-
internal fun checkCycle(vararg tables: Table) = tables.toList().hasCycle()
556+
internal fun checkCycle(vararg tables: Table) = tables.asList().hasCycle()
557557

558558
/** Returns a list of [tables] sorted according to the targets of their foreign key constraints, if any exist. */
559559
fun sortTablesByReferences(tables: Iterable<Table>): List<Table> = tables.sortByReferences()

exposed-core/src/main/kotlin/org/jetbrains/exposed/v1/core/Table.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1520,7 +1520,7 @@ open class Table(name: String = "") : ColumnSet(), DdlAware {
15201520
) {
15211521
_indices.add(
15221522
Index(
1523-
columns.toList(),
1523+
columns.asList(),
15241524
isUnique,
15251525
customIndexName,
15261526
indexType,

exposed-core/src/main/kotlin/org/jetbrains/exposed/v1/core/statements/BatchUpsertStatement.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ open class BatchUpsertStatement(
5454
override fun prepareSQL(transaction: Transaction, prepared: Boolean): String {
5555
val dialect = transaction.db.dialect
5656
val functionProvider = UpsertBuilder.getFunctionProvider(dialect)
57-
val keyColumns = if (functionProvider is MysqlFunctionProvider) keys.toList() else getKeyColumns(keys = keys)
57+
val keyColumns = if (functionProvider is MysqlFunctionProvider) keys.asList() else getKeyColumns(keys = keys)
5858
val insertValues = arguments!!.first()
5959
val insertValuesSql = insertValues.toSqlString(prepared)
6060
val updateExcludeColumns = (onUpdateExclude ?: emptyList()) + if (dialect is OracleDialect) keyColumns else emptyList()

exposed-core/src/main/kotlin/org/jetbrains/exposed/v1/core/statements/UpsertStatement.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ open class UpsertStatement<Key : Any>(
4646
override fun prepareSQL(transaction: Transaction, prepared: Boolean): String {
4747
val dialect = transaction.db.dialect
4848
val functionProvider = UpsertBuilder.getFunctionProvider(dialect)
49-
val keyColumns = if (functionProvider is MysqlFunctionProvider) keys.toList() else getKeyColumns(keys = keys)
49+
val keyColumns = if (functionProvider is MysqlFunctionProvider) keys.asList() else getKeyColumns(keys = keys)
5050
val insertValues = arguments!!.first()
5151
val insertValuesSql = insertValues.toSqlString(prepared)
5252
val updateExcludeColumns = (onUpdateExclude ?: emptyList()) + if (dialect is OracleDialect) keyColumns else emptyList()
@@ -105,8 +105,9 @@ sealed interface UpsertBuilder {
105105
/** Returns the columns to be used in the conflict condition of an upsert statement. */
106106
internal fun UpsertBuilder.getKeyColumns(vararg keys: Column<*>): List<Column<*>> {
107107
this as InsertStatement<*>
108-
return keys.toList().ifEmpty {
109-
table.primaryKey?.columns?.toList() ?: table.indices.firstOrNull { it.unique }?.columns
108+
return keys.asList().ifEmpty {
109+
// TODO If it's needed to avoid changes from the underlying `columns`, revert to `toList`.
110+
table.primaryKey?.columns?.asList() ?: table.indices.firstOrNull { it.unique }?.columns
110111
} ?: emptyList()
111112
}
112113

exposed-core/src/main/kotlin/org/jetbrains/exposed/v1/core/vendors/SQLiteDialect.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ internal object SQLiteFunctionProvider : FunctionProvider() {
3434

3535
override fun concat(separator: String, queryBuilder: QueryBuilder, vararg expr: Expression<*>) = queryBuilder {
3636
if (separator == "") {
37-
expr.toList().appendTo(this, separator = " || ") { +it }
37+
expr.asList().appendTo(this, separator = " || ") { +it }
3838
} else {
39-
expr.toList().appendTo(this, separator = " || '$separator' || ") { +it }
39+
expr.asList().appendTo(this, separator = " || '$separator' || ") { +it }
4040
}
4141
}
4242

exposed-dao/src/main/kotlin/org/jetbrains/exposed/v1/dao/References.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ open class Referrers<ParentID : Any, in Parent : Entity<ParentID>, ChildID : Any
191191
infix fun orderBy(expression: Expression<*>) = orderBy(listOf(expression to SortOrder.ASC))
192192

193193
/** Modifies this reference to sort entities based on multiple columns as specified in [order]. **/
194-
fun orderBy(vararg order: Pair<Expression<*>, SortOrder>) = orderBy(order.toList())
194+
fun orderBy(vararg order: Pair<Expression<*>, SortOrder>) = orderBy(order.asList())
195195
}
196196

197197
/**
@@ -377,7 +377,7 @@ private fun <ID : Any> List<Entity<ID>>.preloadRelations(
377377
}
378378

379379
if (directRelations.isNotEmpty() && relations.size != directRelations.size) {
380-
val remainingRelations = relations.toList() - directRelations
380+
val remainingRelations = relations.asList() - directRelations
381381
directRelations.map { relationProperty ->
382382
val relationsToLoad = this.flatMap {
383383
when (val relation = (relationProperty as KProperty1<Entity<*>, *>).get(it)) {

exposed-jdbc/src/main/kotlin/org/jetbrains/exposed/v1/jdbc/IterableEx.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class EmptySizedIterable<out T> : SizedIterable<T>, Iterator<T> {
6666

6767
/** Represents a [SizedIterable] that defers to the specified [delegate] collection. */
6868
class SizedCollection<out T>(val delegate: Collection<T>) : SizedIterable<T> {
69-
constructor(vararg values: T) : this(values.toList())
69+
constructor(vararg values: T) : this(values.asList())
7070

7171
override fun limit(count: Int): SizedIterable<T> = SizedCollection(delegate.take(count))
7272
override fun offset(start: Long): SizedIterable<T> = if (start >= Int.MAX_VALUE) {

exposed-jdbc/src/main/kotlin/org/jetbrains/exposed/v1/jdbc/SchemaUtils.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ object SchemaUtils : SchemaUtilityApi() {
2121
/** Checks whether any of the [tables] have a sequence of foreign key constraints that cycle back to them. */
2222
fun checkCycle(vararg tables: Table): Boolean {
2323
@OptIn(InternalApi::class)
24-
return tables.toList().hasCycle()
24+
return tables.asList().hasCycle()
2525
}
2626

2727
/** Returns the SQL statements that create all [tables] that do not already exist. */
2828
fun createStatements(vararg tables: Table): List<String> {
2929
if (tables.isEmpty()) return emptyList()
3030

31-
val toCreate = sortTablesByReferences(tables.toList()).filterNot { it.exists() }
31+
val toCreate = sortTablesByReferences(tables.asList()).filterNot { it.exists() }
3232
val alters = arrayListOf<String>()
3333

3434
@OptIn(InternalApi::class)
@@ -441,7 +441,7 @@ object SchemaUtils : SchemaUtilityApi() {
441441
fun drop(vararg tables: Table, inBatch: Boolean = false) {
442442
if (tables.isEmpty()) return
443443
with(TransactionManager.current()) {
444-
var tablesForDeletion = sortTablesByReferences(tables.toList()).reversed().filter { it in tables }
444+
var tablesForDeletion = sortTablesByReferences(tables.asList()).reversed().filter { it in tables }
445445
if (!currentDialect.supportsIfNotExists) {
446446
tablesForDeletion = tablesForDeletion.filter { it.exists() }
447447
}

exposed-r2dbc-tests/src/main/kotlin/org/jetbrains/exposed/v1/r2dbc/tests/shared/Assert.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ fun <T> assertEqualCollections(actual: Collection<T>, expected: Collection<T>) {
2323
}
2424

2525
fun <T> assertEqualCollections(actual: Collection<T>, vararg expected: T) {
26-
assertEqualCollectionsImpl(actual, expected.toList())
26+
assertEqualCollectionsImpl(actual, expected.asList())
2727
}
2828

2929
suspend fun <T> assertEqualCollections(actual: Flow<T>, vararg expected: T) {
30-
assertEqualCollectionsImpl(actual.toList(), expected.toList())
30+
assertEqualCollectionsImpl(actual.toList(), expected.asList())
3131
}
3232

3333
fun <T> assertEqualCollections(actual: Iterable<T>, vararg expected: T) {
34-
assertEqualCollectionsImpl(actual.toList(), expected.toList())
34+
assertEqualCollectionsImpl(actual.toList(), expected.asList())
3535
}
3636

3737
fun <T> assertEqualCollections(actual: Iterable<T>, expected: Collection<T>) {
@@ -57,7 +57,7 @@ fun <T> assertEqualLists(actual: List<T>, expected: List<T>) {
5757
}
5858

5959
fun <T> assertEqualLists(actual: List<T>, vararg expected: T) {
60-
assertEqualLists(actual, expected.toList())
60+
assertEqualLists(actual, expected.asList())
6161
}
6262

6363
suspend fun <T> assertEqualLists(expected: Flow<T>, actual: List<T>) {

0 commit comments

Comments
 (0)