Skip to content

Commit

Permalink
Merge pull request #26 from EmmanuelMess/fix-cpuusage
Browse files Browse the repository at this point in the history
Limit threads in pool to ceil(CORES / 2f).toInt()
  • Loading branch information
EmmanuelMess authored Dec 20, 2018
2 parents 13ede3d + 2230d33 commit 1be7c1e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.amaze.filepreloaderlibrary
import android.app.Activity
import com.amaze.filepreloaderlibrary.datastructures.DataContainer
import com.amaze.filepreloaderlibrary.datastructures.FetcherFunction
import com.amaze.filepreloaderlibrary.utils.LIB_CONTEXT
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.lang.ref.WeakReference
Expand Down Expand Up @@ -49,7 +50,7 @@ object FilePreloader {
* Get all the loaded data, this will load the data in the current thread if it's not loaded.
*/
fun getAllDataLoaded(activity: Activity, getList: (List<DataContainer>?) -> Unit) {
GlobalScope.launch {
GlobalScope.launch(LIB_CONTEXT) {
val preloaded = PreloadedManager.getAllLoaded()

activity.runOnUiThread {
Expand All @@ -63,7 +64,7 @@ object FilePreloader {
* Clear everything, all metadata loaded will be discarded.
*/
fun cleanUp() {
GlobalScope.launch {
GlobalScope.launch(LIB_CONTEXT) {
weakList.forEach {
it.get()?.clear()
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/main/java/com/amaze/filepreloaderlibrary/Loader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ internal class Loader<D: DataContainer>(private val clazz: Class<D>) {
* on each file (represented by its path) inside the folder.
*/
internal fun loadFolder(unit: ProcessUnit<D>) {
GlobalScope.launch {
GlobalScope.launch(LIB_CONTEXT) {
val file = KFile(unit.path)
val fileList = file.list() ?: arrayOf()

Expand Down
4 changes: 2 additions & 2 deletions lib/src/main/java/com/amaze/filepreloaderlibrary/Processor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal class Processor<D: DataContainer>(private val clazz: Class<D>) {
}

private fun work(producer: ReceiveChannel<PreloadableUnit<D>?>, onStart: suspend () -> Unit, onEnd: suspend () -> Unit = {}) {
val job = GlobalScope.launch {
val job = GlobalScope.launch(LIB_CONTEXT) {
onStart()

for (elem in producer) {
Expand All @@ -81,7 +81,7 @@ internal class Processor<D: DataContainer>(private val clazz: Class<D>) {
}

internal fun clear() {
GlobalScope.launch {
GlobalScope.launch(LIB_CONTEXT) {
ranCoroutines.forEach {
it.cancelAndJoin()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.amaze.filepreloaderlibrary.datastructures.DataContainer
import com.amaze.filepreloaderlibrary.datastructures.FetcherFunction
import com.amaze.filepreloaderlibrary.utils.DIVIDER
import com.amaze.filepreloaderlibrary.utils.KFile
import com.amaze.filepreloaderlibrary.utils.LIB_CONTEXT
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

Expand Down Expand Up @@ -35,7 +36,7 @@ class SpecializedPreloader<out D: DataContainer>(private val clazz: Class<D>,
* Get the loaded data. [getList] will run on UI thread.
*/
fun load(activity: Activity, path: String, getList: (List<D>) -> Unit) {
GlobalScope.launch {
GlobalScope.launch(LIB_CONTEXT) {
val t: Pair<Boolean, List<DataContainer>>? = loader.getLoaded(path)

if (t != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.amaze.filepreloaderlibrary.utils

import android.os.AsyncTask
import com.amaze.filepreloaderlibrary.datastructures.PreloadedFolder
import kotlinx.coroutines.ObsoleteCoroutinesApi
import kotlinx.coroutines.newFixedThreadPoolContext
import kotlin.math.ceil

/**
* Contains a hash map linking paths to [PreloadedFolder]s.
Expand All @@ -19,4 +23,13 @@ const val PRIORITY_NOW = 0
/**
* Probably will need to be loaded in the FUTURE
*/
const val PRIORITY_FUTURE = 1
const val PRIORITY_FUTURE = 1

private val CORES_AVAILABLE = Runtime.getRuntime().availableProcessors()

/**
* Normally coroutines use up all the CPUs available, wasting resources, like energy.
* This limits the amount of threads to at most half +1 of the cores of the processor.
*/
@ObsoleteCoroutinesApi
val LIB_CONTEXT = newFixedThreadPoolContext(ceil(CORES_AVAILABLE/2.toFloat()).toInt(), "FilePreloaderLibrary")

0 comments on commit 1be7c1e

Please sign in to comment.