Skip to content

Commit 82caeb2

Browse files
authored
Document segment pool configuration (#494)
Related issue: KT-81537
1 parent b4e59a4 commit 82caeb2

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,61 @@ for details on how to configure a Gradle project to utilize JPMS.
9292
`kotlinx-io` is not tested on Android on a regular basis,
9393
but the library is compatible with Android 5.0+ (API level 21+).
9494

95+
### Segment pooling
96+
97+
As an optimization, on some platforms `Buffer`'s segments are pooled,
98+
meaning that everytime a buffer needs a new segment,
99+
an attempt will be made to take a pre-allocated segment from a pool,
100+
and if the pool is empty, only then a new segment will be created.
101+
Everytime a segment is no longer needed, it will be placed back into a pool (unless the pool is already full).
102+
103+
Currently, the pooling is only supported on JVM (and Android).
104+
105+
The segment pool has a two-level structure, with a smaller first-level pool aimed to serve requests as quickly as possible,
106+
and a larger second-level pool. While the first-level pool sizing could not be configured at the moment,
107+
the size of the second-level pool could be adjusted using the `kotlinx.io.pool.size.bytes` system property.
108+
The property accepts numeric values only.
109+
On Android, the second-level pool size is `0` by default,
110+
for all other JVM environments it is `4194304` (4 megabytes) by default.
111+
112+
While the size could be changed using the system property, it is necessary to note that its value will be read during
113+
the pool initialization, and later changes of the property will not affect the pool size.
114+
In other words, the property has to be updated before any calls to the `kotlinx-io` API.
115+
116+
On JVM, the easiest way to achieve that is to supply the property value via command line JVM flag
117+
(`-Pkotlinx.io.pool.size.bytes=XXX`).
118+
119+
On Android, it is a bit trickier. One of the ways to set up the property before running any `kotlinx-io`-related code
120+
is by overriding `Application.onCreate` and setting the property there:
121+
```kotlin
122+
package org.example
123+
124+
import android.app.Application
125+
126+
class MySegmentHungryApp : Application() {
127+
override fun onCreate() {
128+
System.setProperty("kotlinx.io.pool.size.bytes", "2097152" /* or whatever value suites your case */)
129+
super.onCreate()
130+
}
131+
}
132+
```
133+
134+
Then, the application class needs to be explicitly registered in the manifest:
135+
136+
```xml
137+
<?xml version="1.0" encoding="utf-8"?>
138+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
139+
xmlns:tools="http://schemas.android.com/tools">
140+
141+
<application
142+
android:name="org.example.MySegmentHungryApp"
143+
...
144+
>
145+
146+
</application>
147+
</manifest>
148+
```
149+
95150
## Contributing
96151

97152
Read the [Contributing Guidelines](CONTRIBUTING.md).

0 commit comments

Comments
 (0)