From: Philippe Antoine Date: Thu, 16 Jun 2022 13:14:27 +0000 (+0200) Subject: mqtt: remove quadratic time complexity X-Git-Tag: suricata-6.0.7~9 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=40d1bb3422d7837e245d2588eb49ceb3ae70183e;p=thirdparty%2Fsuricata.git mqtt: remove quadratic time complexity When having many transactions in a single parsing call... Fix has overhead of having one more field in the mqtt state. Completes commit a8079dc9787d77cf705aa47000b499a325be0716 Ticket: #5399 (cherry picked from commit e160917bcfb67b23535b4ce082f506a9e66b7778) --- diff --git a/rust/src/mqtt/mqtt.rs b/rust/src/mqtt/mqtt.rs index 4e16250e8a..9517a67294 100644 --- a/rust/src/mqtt/mqtt.rs +++ b/rust/src/mqtt/mqtt.rs @@ -120,6 +120,7 @@ pub struct MQTTState { skip_request: usize, skip_response: usize, max_msg_len: usize, + tx_index_completed: usize, } impl MQTTState { @@ -132,6 +133,7 @@ impl MQTTState { skip_request: 0, skip_response: 0, max_msg_len: unsafe { MAX_MSG_LEN as usize }, + tx_index_completed: 0, } } @@ -148,6 +150,7 @@ impl MQTTState { } } if found { + self.tx_index_completed = 0; self.transactions.remove(index); } } @@ -184,13 +187,16 @@ impl MQTTState { tx.toserver = true; } if self.transactions.len() > unsafe { MQTT_MAX_TX } { - for tx_old in &mut self.transactions { + let mut index = self.tx_index_completed; + for tx_old in self.transactions.iter_mut().skip(self.tx_index_completed) { + index = index + 1; if !tx_old.complete { tx_old.complete = true; MQTTState::set_event(tx_old, MQTTEvent::TooManyTransactions); break; } } + self.tx_index_completed = index; } return tx; }