Browse Source

Use processors as a map, don't iterate them every line.

tags/v0.9.0
Chris Smith 5 years ago
parent
commit
b9a36acc64

+ 1
- 0
CHANGELOG View File

@@ -13,6 +13,7 @@ vNEXT (in development)
13 13
    * All events in a batch are buffered until the batch is finished
14 14
    * The events are then published together in a single `BatchReceived` event
15 15
  * Added support for CHGHOST messages
16
+ * (Internal) Improve performance when the MessageHandler is finding a processor for a message
16 17
  * (Internal) Introduced event mutators
17 18
    * Event mutators are now responsible for handling changing events in response to state
18 19
      e.g. ChannelFanOutMutator creates Channel* events for global quits/nick changes/etc

+ 3
- 2
src/main/kotlin/com/dmdirc/ktirc/io/MessageHandler.kt View File

@@ -18,13 +18,14 @@ internal interface MessageEmitter {
18 18
 }
19 19
 
20 20
 internal class MessageHandler(
21
-        private val processors: List<MessageProcessor>,
21
+        processors: List<MessageProcessor>,
22 22
         private val mutators: List<EventMutator>,
23 23
         private val handlers: List<EventHandler>) : MessageEmitter {
24 24
 
25 25
     private val log by logger()
26 26
 
27 27
     private val emitters = mutableListOf<(IrcEvent) -> Unit>()
28
+    private val processorMap = processors.flatMap { it.commands.map { c -> c to it } }.toMap()
28 29
 
29 30
     suspend fun processMessages(ircClient: IrcClient, messages: ReceiveChannel<IrcMessage>) {
30 31
         for (message in messages) {
@@ -54,7 +55,7 @@ internal class MessageHandler(
54 55
     }
55 56
 
56 57
     private fun IrcMessage.toEvents() = this.getProcessor()?.process(this) ?: emptyList()
57
-    private fun IrcMessage.getProcessor() = processors.firstOrNull { it.commands.contains(command) } ?: run {
58
+    private fun IrcMessage.getProcessor() = processorMap[command] ?: run {
58 59
         log.warning { "No processor found for $command" }
59 60
         null
60 61
     }

+ 1
- 1
src/main/kotlin/com/dmdirc/ktirc/model/CapabilitiesState.kt View File

@@ -94,7 +94,7 @@ sealed class Capability(val name: String) {
94 94
     object AwayStateMessages : Capability("away-notify") // TODO: Add processor
95 95
 
96 96
     /** Receive a notification when a user's host changes, instead of a quit/join. */
97
-    object HostChangeMessages : Capability("chghost") // TODO: Add processor
97
+    object HostChangeMessages : Capability("chghost")
98 98
 
99 99
 }
100 100
 

Loading…
Cancel
Save