-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use JS-specific collections inside core compose (#2318)
These are faster and more memory-efficient than Kotlin's implementation.
- Loading branch information
1 parent
e05aaa2
commit e0989f3
Showing
9 changed files
with
271 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
redwood-compose/src/commonMain/kotlin/app/cash/redwood/compose/PlatformList.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright (C) 2024 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package app.cash.redwood.compose | ||
|
||
internal expect class PlatformList<T> | ||
|
||
internal expect inline fun <T> platformListOf(): PlatformList<T> | ||
|
||
internal expect inline val <T> PlatformList<T>.size: Int | ||
|
||
internal expect inline operator fun <T> PlatformList<T>.get(index: Int): T | ||
|
||
internal expect inline fun <T> PlatformList<T>.add(index: Int, element: T) | ||
|
||
internal expect inline fun <T> PlatformList<T>.remove(index: Int, count: Int) | ||
|
||
internal expect fun <T> PlatformList<T>.move(from: Int, to: Int, count: Int) |
26 changes: 26 additions & 0 deletions
26
redwood-compose/src/commonMain/kotlin/app/cash/redwood/compose/PlatformSet.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright (C) 2024 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package app.cash.redwood.compose | ||
|
||
internal expect class PlatformSet<T> | ||
|
||
internal expect inline fun <T> platformSetOf(): PlatformSet<T> | ||
|
||
internal expect inline operator fun <T> PlatformSet<T>.plusAssign(element: T) | ||
|
||
internal expect inline fun <T> PlatformSet<T>.forEach(crossinline block: (T) -> Unit) | ||
|
||
internal expect inline fun <T> PlatformSet<T>.clear() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
redwood-compose/src/jsMain/kotlin/app/cash/redwood/compose/PlatformList.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright (C) 2024 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
@file:Suppress("NOTHING_TO_INLINE") | ||
|
||
package app.cash.redwood.compose | ||
|
||
@JsName("Array") | ||
internal external class JsArray<T> { | ||
val length: Int | ||
fun splice(start: Int, deleteCount: Int): JsArray<T> | ||
fun splice(start: Int, deleteCount: Int, element: T) | ||
} | ||
|
||
internal actual typealias PlatformList<T> = JsArray<T> | ||
|
||
internal actual inline fun <T> platformListOf(): PlatformList<T> { | ||
return js("[]").unsafeCast<PlatformList<T>>() | ||
} | ||
|
||
internal actual inline val <T> PlatformList<T>.size: Int get() = length | ||
|
||
internal actual inline operator fun <T> PlatformList<T>.get(index: Int): T { | ||
return asDynamic()[index] | ||
} | ||
|
||
internal actual inline fun <T> PlatformList<T>.add(index: Int, element: T) { | ||
splice(index, 0, element) | ||
} | ||
|
||
internal actual inline fun <T> PlatformList<T>.remove(index: Int, count: Int) { | ||
splice(index, count) | ||
} | ||
|
||
internal actual fun <T> PlatformList<T>.move(from: Int, to: Int, count: Int) { | ||
val dest = if (from > to) to else to - count | ||
val removed = splice(from, count) | ||
repeat(count) { offset -> | ||
splice(dest + offset, 0, removed[offset]) | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
redwood-compose/src/jsMain/kotlin/app/cash/redwood/compose/PlatformSet.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (C) 2024 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
@file:Suppress("NOTHING_TO_INLINE") | ||
|
||
package app.cash.redwood.compose | ||
|
||
@JsName("Set") | ||
internal external class JsSet<T> { | ||
fun add(element: T) | ||
fun clear() | ||
fun forEach(block: (T) -> Unit) | ||
} | ||
|
||
internal actual typealias PlatformSet<T> = JsSet<T> | ||
|
||
internal actual inline fun <T> platformSetOf(): PlatformSet<T> { | ||
return JsSet() | ||
} | ||
|
||
internal actual inline operator fun <T> PlatformSet<T>.plusAssign(element: T) { | ||
add(element) | ||
} | ||
|
||
internal actual inline fun <T> PlatformSet<T>.forEach(crossinline block: (T) -> Unit) { | ||
forEach { block(it) } | ||
} | ||
|
||
internal actual inline fun <T> PlatformSet<T>.clear() { | ||
clear() | ||
} |
45 changes: 45 additions & 0 deletions
45
redwood-compose/src/nonJsMain/kotlin/app/cash/redwood/compose/PlatformList.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (C) 2024 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
@file:Suppress("NOTHING_TO_INLINE") | ||
|
||
package app.cash.redwood.compose | ||
|
||
@Suppress("ACTUAL_TYPE_ALIAS_NOT_TO_CLASS") // On the JVM it aliases to java.util.ArrayList. | ||
internal actual typealias PlatformList<T> = ArrayList<T> | ||
|
||
internal actual inline fun <T> platformListOf(): PlatformList<T> { | ||
return ArrayList() | ||
} | ||
|
||
internal actual inline val <T> PlatformList<T>.size: Int get() = size | ||
|
||
internal actual inline operator fun <T> PlatformList<T>.get(index: Int): T { | ||
return get(index) | ||
} | ||
|
||
internal actual inline fun <T> PlatformList<T>.add(index: Int, element: T) { | ||
add(index, element) | ||
} | ||
|
||
internal actual inline fun <T> PlatformList<T>.remove(index: Int, count: Int) { | ||
// Force resolution to the generated extension. | ||
(this as MutableList<T>).remove(index, count) | ||
} | ||
|
||
internal actual inline fun <T> PlatformList<T>.move(from: Int, to: Int, count: Int) { | ||
// Force resolution to the generated extension. | ||
(this as MutableList<T>).move(from, to, count) | ||
} |
39 changes: 39 additions & 0 deletions
39
redwood-compose/src/nonJsMain/kotlin/app/cash/redwood/compose/PlatformSet.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright (C) 2024 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
@file:Suppress("NOTHING_TO_INLINE") | ||
|
||
package app.cash.redwood.compose | ||
|
||
import androidx.collection.MutableScatterSet | ||
import androidx.collection.mutableScatterSetOf | ||
|
||
internal actual typealias PlatformSet<T> = MutableScatterSet<T> | ||
|
||
internal actual inline fun <T> platformSetOf(): PlatformSet<T> { | ||
return mutableScatterSetOf() | ||
} | ||
|
||
internal actual inline operator fun <T> PlatformSet<T>.plusAssign(element: T) { | ||
plusAssign(element) | ||
} | ||
|
||
internal actual inline fun <T> PlatformSet<T>.forEach(block: (T) -> Unit) { | ||
forEach(block) | ||
} | ||
|
||
internal actual inline fun <T> PlatformSet<T>.clear() { | ||
clear() | ||
} |