Skip to content

Commit a1085fe

Browse files
committed
performance improvement of Cell.layoutable()
1 parent 0946cf3 commit a1085fe

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

editor-runtime/src/commonMain/kotlin/org/modelix/editor/EditorComponent.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ open class EditorComponent(
1111
val state: EditorState = EditorState()
1212
private var selection: Selection? = null
1313
private val cellIndex: IncrementalIndex<CellReference, Cell> = IncrementalIndex()
14+
private val layoutablesIndex: IncrementalIndex<Cell, LayoutableCell> = IncrementalIndex()
1415
private var selectionUpdater: (() -> Selection?)? = null
1516
protected var codeCompletionMenu: CodeCompletionMenu? = null
1617
private var rootCell: Cell = rootCellCreator(state).also {
1718
it.editorComponent = this
1819
cellIndex.update(it.referencesIndexList)
20+
layoutablesIndex.update(it.layout.layoutablesIndexList)
1921
}
2022

2123
fun selectAfterUpdate(newSelection: () -> Selection?) {
@@ -24,6 +26,8 @@ open class EditorComponent(
2426

2527
fun resolveCell(reference: CellReference): List<Cell> = cellIndex.lookup(reference)
2628

29+
fun resolveLayoutable(cell: Cell): LayoutableCell? = layoutablesIndex.lookup(cell).firstOrNull()
30+
2731
private fun updateRootCell() {
2832
val oldRootCell = rootCell
2933
val newRootCell = rootCellCreator(state)
@@ -32,6 +36,7 @@ open class EditorComponent(
3236
newRootCell.editorComponent = this
3337
rootCell = newRootCell
3438
cellIndex.update(rootCell.referencesIndexList)
39+
layoutablesIndex.update(rootCell.layout.layoutablesIndexList)
3540
}
3641
}
3742

editor-runtime/src/commonMain/kotlin/org/modelix/editor/TextLayouter.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package org.modelix.editor
22

33
import kotlinx.html.*
4+
import org.modelix.incremental.IncrementalList
45

56
class TextLine(words_: Iterable<Layoutable>) : IProducesHtml {
67
var initialText: LayoutedText? = null
78
var finalText: LayoutedText? = null
89
val words: List<Layoutable> = words_.toList()
10+
val layoutablesIndexList: IncrementalList<Pair<Cell, LayoutableCell>> =
11+
IncrementalList.of(words.filterIsInstance<LayoutableCell>().map { it.cell to it })
912

1013
init {
1114
words.filter { it.initialLine == null }.forEach { it.initialLine = this }
@@ -51,6 +54,8 @@ class LayoutedText(
5154
var indent: Int = 0
5255
) : IProducesHtml {
5356
var owner: LayoutedText? = null
57+
val layoutablesIndexList: IncrementalList<Pair<Cell, LayoutableCell>> =
58+
IncrementalList.concat(lines.map { it.layoutablesIndexList })
5459

5560
init {
5661
lines.forEach { if (it.initialText == null) it.initialText = this }
@@ -306,8 +311,8 @@ class LayoutableCell(val cell: Cell) : Layoutable() {
306311
}
307312

308313
fun Cell.layoutable(): LayoutableCell? {
309-
// TODO performance
310-
return rootCell().layout.lines.asSequence().flatMap { it.words }.filterIsInstance<LayoutableCell>().find { it.cell == this }
314+
//return rootCell().layout.lines.asSequence().flatMap { it.words }.filterIsInstance<LayoutableCell>().find { it.cell == this }
315+
return editorComponent?.resolveLayoutable(this)
311316
}
312317

313318
class LayoutableIndent(val indentSize: Int): Layoutable() {

0 commit comments

Comments
 (0)