Sfoglia il codice sorgente

Unswitch the IP checksum loop for 30% improvement in performance.

whitequark 7 anni fa
parent
commit
275eb90785
2 ha cambiato i file con 10 aggiunte e 13 eliminazioni
  1. 2 2
      README.md
  2. 8 11
      src/wire/ip.rs

+ 2 - 2
README.md

@@ -316,9 +316,9 @@ on a Dell XPS 13 9360 laptop) is as follows:
 
 ```
 $ cargo run -q --release --example benchmark tap0 reader
-throughput: 2.219 Gbps
+throughput: 2.556 Gbps
 $ cargo run -q --release --example benchmark tap0 writer
-throughput: 4.519 Gbps
+throughput: 5.301 Gbps
 ```
 
 ## Bare-metal usage examples

+ 8 - 11
src/wire/ip.rs

@@ -610,18 +610,15 @@ pub mod checksum {
     }
 
     /// Compute an RFC 1071 compliant checksum (without the final complement).
-    pub fn data(data: &[u8]) -> u16 {
+    pub fn data(mut data: &[u8]) -> u16 {
+        // See RFC 1071 section 4.1 for the original implementation.
         let mut accum: u32 = 0;
-        let mut i = 0;
-        while i < data.len() {
-            let word;
-            if i + 2 <= data.len() {
-                word = NetworkEndian::read_u16(&data[i..i + 2]) as u32
-            } else {
-                word = (data[i] as u32) << 8
-            }
-            accum += word;
-            i += 2;
+        while data.len() >= 2 {
+            accum += NetworkEndian::read_u16(data) as u32;
+            data = &data[2..];
+        }
+        if let Some(&value) = data.first() {
+            accum += (value as u32) << 8;
         }
         propagate_carries(accum)
     }