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
    * All events in a batch are buffered until the batch is finished
13
    * All events in a batch are buffered until the batch is finished
14
    * The events are then published together in a single `BatchReceived` event
14
    * The events are then published together in a single `BatchReceived` event
15
  * Added support for CHGHOST messages
15
  * Added support for CHGHOST messages
16
+ * (Internal) Improve performance when the MessageHandler is finding a processor for a message
16
  * (Internal) Introduced event mutators
17
  * (Internal) Introduced event mutators
17
    * Event mutators are now responsible for handling changing events in response to state
18
    * Event mutators are now responsible for handling changing events in response to state
18
      e.g. ChannelFanOutMutator creates Channel* events for global quits/nick changes/etc
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
 }
18
 }
19
 
19
 
20
 internal class MessageHandler(
20
 internal class MessageHandler(
21
-        private val processors: List<MessageProcessor>,
21
+        processors: List<MessageProcessor>,
22
         private val mutators: List<EventMutator>,
22
         private val mutators: List<EventMutator>,
23
         private val handlers: List<EventHandler>) : MessageEmitter {
23
         private val handlers: List<EventHandler>) : MessageEmitter {
24
 
24
 
25
     private val log by logger()
25
     private val log by logger()
26
 
26
 
27
     private val emitters = mutableListOf<(IrcEvent) -> Unit>()
27
     private val emitters = mutableListOf<(IrcEvent) -> Unit>()
28
+    private val processorMap = processors.flatMap { it.commands.map { c -> c to it } }.toMap()
28
 
29
 
29
     suspend fun processMessages(ircClient: IrcClient, messages: ReceiveChannel<IrcMessage>) {
30
     suspend fun processMessages(ircClient: IrcClient, messages: ReceiveChannel<IrcMessage>) {
30
         for (message in messages) {
31
         for (message in messages) {
54
     }
55
     }
55
 
56
 
56
     private fun IrcMessage.toEvents() = this.getProcessor()?.process(this) ?: emptyList()
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
         log.warning { "No processor found for $command" }
59
         log.warning { "No processor found for $command" }
59
         null
60
         null
60
     }
61
     }

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

94
     object AwayStateMessages : Capability("away-notify") // TODO: Add processor
94
     object AwayStateMessages : Capability("away-notify") // TODO: Add processor
95
 
95
 
96
     /** Receive a notification when a user's host changes, instead of a quit/join. */
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