Skip to content

Commit

Permalink
Free discarded pool objects by default. #476
Browse files Browse the repository at this point in the history
  • Loading branch information
czyzby committed Jan 30, 2024
1 parent 5b6fdab commit ddb1f59
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ _See also: [the official libGDX changelog](https://github.com/libgdx/libgdx/blob

#### 1.12.1-SNAPSHOT

- **[CHANGE]** (`ktx-assets`) The `pool` factory method now attempts to reset `Poolable` objects with the default `discard` lambda
to match the default libGDX pool behavior. Pass a custom function to override it.

#### 1.12.1-rc1

- **[UPDATE]** Updated to libGDX 1.12.1.
Expand Down
12 changes: 9 additions & 3 deletions assets/src/main/kotlin/ktx/assets/pools.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ktx.assets

import com.badlogic.gdx.utils.Disposable
import com.badlogic.gdx.utils.Pool
import com.badlogic.gdx.utils.Pool.Poolable

/**
* Allows to use a [Pool] instance as a functional object. When invoked with no parameters, [Pool] will provide an
Expand All @@ -14,7 +15,7 @@ operator fun <Type> Pool<Type>.invoke(): Type = this.obtain()
/**
* Allows to use a [Pool] instance as a functional object. When invoked with a parameter, [Pool] will treat the passed
* parameter as an object freed to the pool.
* @param free will be returned to the pool. Might be reset if it implements the Poolable interface.
* @param free will be returned to the pool. Might be reset if it implements the [Poolable] interface.
* @see Pool.free
*/
operator fun <Type> Pool<Type>.invoke(free: Type) = this.free(free)
Expand All @@ -24,14 +25,19 @@ operator fun <Type> Pool<Type>.invoke(free: Type) = this.free(free)
* @param max max amount stored in the pool. When exceeded, freed objects are no longer accepted.
* @param discard invoked each time an object is rejected or removed from the pool. This might happen if an object is
* freed with [Pool.free] or [Pool.freeAll] if the pool is full, or when [Pool.clear] is called. Optional, defaults
* to no operation. If the objects are [Disposable], this lambda might be used to dispose of them.
* to resetting the objects implementing the [Poolable] interface to replicate the default behavior. If the objects are
* [Disposable], this lambda should be used to dispose of them.
* @param provider creates instances of the requested objects.
* @return a new [Pool] instance, creating the object with the passed provider.
*/
inline fun <Type> pool(
initialCapacity: Int = 16,
max: Int = Int.MAX_VALUE,
crossinline discard: (Type) -> Unit = {},
crossinline discard: (Type) -> Unit = {
if (it is Poolable) {
it.reset()
}
},
crossinline provider: () -> Type,
): Pool<Type> =
object : Pool<Type>(initialCapacity, max) {
Expand Down
26 changes: 26 additions & 0 deletions assets/src/test/kotlin/ktx/assets/PoolsTest.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package ktx.assets

import com.badlogic.gdx.utils.Pool
import com.badlogic.gdx.utils.Pool.Poolable
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertSame
import org.junit.Assert.assertTrue
import org.junit.Test

/**
Expand Down Expand Up @@ -76,6 +78,30 @@ class PoolsTest {
assertEquals(listOf("Value6", "Value7", "Value8", "Value9", "Value10"), discarded)
}

@Test
fun `should create new pools that reset discarded objects by default`() {
// Given:
val pool = pool(max = 1) { SamplePoolable() }
val freed = pool()
val discarded = pool()
pool.free(freed)

// When:
pool.free(discarded)

// Then:
assertTrue(discarded.isReset)
}

/**
* A simple data object implementing the [Poolable] interface.
*/
private class SamplePoolable(var isReset: Boolean = false) : Poolable {
override fun reset() {
this.isReset = true
}
}

/**
* Provides new [Any] instances.
*/
Expand Down

0 comments on commit ddb1f59

Please sign in to comment.