Improved patch for bandwidth estimation: Avoid reading only 1 or 2
bytes at a time.
git-svn-id: svn://svn.code.sf.net/p/tigervnc/code/trunk@75 3789f03b-4d11-0410-bbf8-ca57d06f2519
diff --git a/rdr/FdInStream.cxx b/rdr/FdInStream.cxx
index 26e6d0d..38e12ac 100644
--- a/rdr/FdInStream.cxx
+++ b/rdr/FdInStream.cxx
@@ -36,6 +36,14 @@
#include <sys/time.h>
#endif
+#ifndef min
+#define min(a,b) (((a) < (b)) ? (a) : (b))
+#endif
+
+#ifndef max
+#define max(a,b) (((a) > (b)) ? (a) : (b))
+#endif
+
// XXX should use autoconf HAVE_SYS_SELECT_H
#ifdef _AIX
#include <sys/select.h>
@@ -130,14 +138,15 @@
int bytes_to_read;
while (end < start + itemSize) {
- if (timing) {
- bytes_to_read = start + bufSize - end;
- } else {
- // When not timing, we must be careful not to read extra data
- // into the buffer. Otherwise, the line speed estimation might
- // stay at zero for a long time: All reads during timing=1 can
- // be satisfied without calling readWithTimeoutOrCallback.
- bytes_to_read = start + itemSize*nItems - end;
+ bytes_to_read = start + bufSize - end;
+ if (!timing) {
+ // When not timing, we must be careful not to read too much
+ // extra data into the buffer. Otherwise, the line speed
+ // estimation might stay at zero for a long time: All reads
+ // during timing=1 can be satisfied without calling
+ // readWithTimeoutOrCallback. However, reading only 1 or 2 bytes
+ // bytes is ineffecient.
+ bytes_to_read = min(bytes_to_read, max(itemSize*nItems, 8));
}
int n = readWithTimeoutOrCallback((U8*)end, bytes_to_read, wait);
if (n == 0) return 0;