Skip to content

Commit

Permalink
Added cache to ensure that we don't cause OOM
Browse files Browse the repository at this point in the history
  • Loading branch information
thsaravana committed Nov 6, 2022
1 parent b6e25e8 commit 828e90a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.intellij.openapi.vfs.VirtualFile
import com.intellij.util.concurrency.AppExecutorUtil
import org.jetbrains.concurrency.CancellablePromise
import java.awt.Image
import java.lang.ref.SoftReference
import java.util.concurrent.Callable
import javax.imageio.ImageIO
import javax.swing.DefaultListModel
Expand Down Expand Up @@ -55,7 +56,7 @@ class MainServiceImpl(private val project: Project) : MainService, PersistentSta
FileEditorManagerListener {

private var storage = MainService.Storage()
private val snapshotsMap: MutableMap<VirtualFile, Image> = mutableMapOf()
private val cache = SoftReference(SnapshotCache())
private var width: Int = 0

override var panel: PaparazziWindowPanel? = null
Expand All @@ -79,13 +80,11 @@ class MainServiceImpl(private val project: Project) : MainService, PersistentSta
}
}

override fun image(item: Item): Image? {
return snapshotsMap[item.file]
}
override fun image(item: Item): Image? = cacheImage(item)

private fun cacheImage(item: Item): Image? {
val file = item.file
return snapshotsMap[file] ?: try {
return cache.get()?.get(file) ?: try {
file.inputStream.use {
val bufferedImage = ImageIO.read(it)
val finalImage = if (width == 0) {
Expand All @@ -98,7 +97,7 @@ class MainServiceImpl(private val project: Project) : MainService, PersistentSta
if (newHeight == 0) newHeight = 20
bufferedImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH)
}
snapshotsMap[file] = finalImage
cache.get()?.set(file, finalImage)
finalImage
}
} catch (e: Exception) {
Expand All @@ -122,7 +121,6 @@ class MainServiceImpl(private val project: Project) : MainService, PersistentSta
}

override fun reload() {
snapshotsMap.clear() // FIXME enable LRU cache
val toList = model.elements().toList()
model.clear()
toList.forEach { item ->
Expand All @@ -140,8 +138,6 @@ class MainServiceImpl(private val project: Project) : MainService, PersistentSta
val nonBlocking: NonBlockingReadAction<List<Item>> = ReadAction.nonBlocking(Callable {
try {
model.clear()
snapshotsMap.clear() // FIXME enable LRU cache

file.toItems(project, onlyShowFailures).map { item ->
ProgressManager.checkCanceled()
cacheImage(item)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.getyourguide.paparazzi.service

import com.intellij.openapi.vfs.VirtualFile
import java.awt.Image

// TODO Move the cacheLimit to settings, so that the user can configure this
internal class SnapshotCache(private val cacheLimit: Int = 20) {
private val cache: MutableList<Snapshot> = mutableListOf()

operator fun set(file: VirtualFile, image: Image) {
if (cache.find { it.file == file } == null) {
if (cache.size >= cacheLimit) {
cache.removeFirstOrNull()
}
cache.add(Snapshot(file, image))
}
}

operator fun get(file: VirtualFile): Image? = cache.find { it.file == file }?.image

private data class Snapshot(val file: VirtualFile, val image: Image)
}

0 comments on commit 828e90a

Please sign in to comment.