diff mbox

Avoid unnecessary data copying inside dvb_dmx_swfilter_204() function

Message ID 4C2615FB.2000805@kolumbus.fi (mailing list archive)
State Superseded
Headers show

Commit Message

Marko Ristola June 26, 2010, 3 p.m. UTC
None
diff mbox

Patch

diff --git a/drivers/media/dvb/dvb-core/dvb_demux.c
b/drivers/media/dvb/dvb-core/dvb_demux.c
index 977ddba..627d103 100644
--- a/drivers/media/dvb/dvb-core/dvb_demux.c
+++ b/drivers/media/dvb/dvb-core/dvb_demux.c
@@ -520,49 +520,60 @@  bailout:
 
 EXPORT_SYMBOL(dvb_dmx_swfilter);
 
+static inline int find204Sync(const u8 *buf, int pos, size_t count)
+{
+    while(likely(pos < count)) {
+        if (likely(buf[pos] == 0x47 || buf[pos] == 0xB8))
+            break;
+        pos++;
+    }
+
+    return pos;
+}
+
 void dvb_dmx_swfilter_204(struct dvb_demux *demux, const u8 *buf,
size_t count)
 {
     int p = 0, i, j;
-    u8 tmppack[188];
+    const u8 *q;
 
     spin_lock(&demux->lock);
 
-    if (demux->tsbufp) {
+    if (likely(demux->tsbufp)) { /* tsbuf[0] is now 0x47. */
         i = demux->tsbufp;
         j = 204 - i;
-        if (count < j) {
+        if (unlikely(count < j)) {
             memcpy(&demux->tsbuf[i], buf, count);
             demux->tsbufp += count;
             goto bailout;
         }
         memcpy(&demux->tsbuf[i], buf, j);
-        if ((demux->tsbuf[0] == 0x47) || (demux->tsbuf[0] == 0xB8)) {
-            memcpy(tmppack, demux->tsbuf, 188);
-            if (tmppack[0] == 0xB8)
-                tmppack[0] = 0x47;
-            dvb_dmx_swfilter_packet(demux, tmppack);
-        }
+        if (likely(demux->tsbuf[0] == 0x47)) /* double check */
+            dvb_dmx_swfilter_packet(demux, demux->tsbuf);
         demux->tsbufp = 0;
         p += j;
     }
 
-    while (p < count) {
-        if ((buf[p] == 0x47) || (buf[p] == 0xB8)) {
-            if (count - p >= 204) {
-                memcpy(tmppack, &buf[p], 188);
-                if (tmppack[0] == 0xB8)
-                    tmppack[0] = 0x47;
-                dvb_dmx_swfilter_packet(demux, tmppack);
-                p += 204;
-            } else {
-                i = count - p;
-                memcpy(demux->tsbuf, &buf[p], i);
-                demux->tsbufp = i;
-                goto bailout;
-            }
-        } else {
-            p++;
+    while (likely((p = find204Sync(buf, p, count)) < count)) {
+        if (unlikely(count - p < 204))
+            break;
+
+        q = &buf[p];
+
+        if (unlikely(*q == 0xB8)) {
+            memcpy(demux->tsbuf, q, 188);
+            demux->tsbuf[0] = 0x47;
+            q = demux->tsbuf;
         }
+        dvb_dmx_swfilter_packet(demux, q);
+        p += 204;
+    }
+
+    i = count - p;
+    if (likely(i)) {
+        memcpy(demux->tsbuf, &buf[p], i);
+        demux->tsbufp = i;
+        if (unlikely(demux->tsbuf[0] == 0xB8))
+            demux->tsbuf[0] = 0x47;
     }