Skip to content
Browse files

Speed optimizations

Suggest some branch predictions.

Signed-off-by: Frediano Ziglio <freddy77@gmail.com>
  • Loading branch information...
1 parent 55ed625 commit 4fe4d15ee30cdeaef6bbafc004bbd9bcd52ae412 @freddy77 freddy77 committed Feb 16, 2014
Showing with 8 additions and 8 deletions.
  1. +2 −2 src/tds/read.c
  2. +6 −6 src/tds/stream.c
View
4 src/tds/read.c
@@ -262,7 +262,7 @@ tds_get_char_data(TDSSOCKET * tds, char *row_buffer, size_t wire_size, TDSCOLUMN
*/
in_left = blob ? curcol->column_cur_size : curcol->column_size;
curcol->column_cur_size = read_and_convert(tds, curcol->char_conv, &wire_size, dest, in_left);
- if (wire_size > 0) {
+ if (TDS_UNLIKELY(wire_size > 0)) {
tds_get_n(tds, NULL, wire_size);
tdsdump_log(TDS_DBG_NETWORK, "error: tds_get_char_data: discarded %u on wire while reading %d into client. \n",
(unsigned int) wire_size, curcol->column_cur_size);
@@ -301,7 +301,7 @@ tds_get_n(TDSSOCKET * tds, void *dest, size_t need)
dest = (char *) dest + have;
}
need -= have;
- if (tds_read_packet(tds) < 0)
+ if (TDS_UNLIKELY(tds_read_packet(tds) < 0))
return NULL;
}
if (need > 0) {
View
12 src/tds/stream.c
@@ -115,7 +115,7 @@ tds_convert_stream(TDSSOCKET * tds, TDSICONV * char_conv, TDS_ICONV_DIRECTION di
/* write converted chunk */
len = ostream->write(ostream, ob - ostream->buffer);
- if (len < 0)
+ if (TDS_UNLIKELY(len < 0))
break;
if ((size_t) -1 == ol) {
@@ -129,7 +129,7 @@ tds_convert_stream(TDSSOCKET * tds, TDSICONV * char_conv, TDS_ICONV_DIRECTION di
tdsdump_dump_buf(TDS_DBG_NETWORK, "Troublesome bytes:", ib, bufleft);
}
- if (ib == temp) { /* tds_iconv did not convert anything, avoid infinite loop */
+ if (TDS_UNLIKELY(ib == temp)) { /* tds_iconv did not convert anything, avoid infinite loop */
tdsdump_log(TDS_DBG_NETWORK, "No conversion possible: some bytes left.\n");
break;
}
@@ -158,12 +158,12 @@ tds_copy_stream(TDSSOCKET * tds, TDSINSTREAM * istream, TDSOUTSTREAM * ostream)
int len = istream->read(istream, ostream->buffer, ostream->buf_len);
if (len == 0)
return TDS_SUCCESS;
- if (len < 0)
+ if (TDS_UNLIKELY(len < 0))
break;
/* write chunk */
len = ostream->write(ostream, len);
- if (len < 0)
+ if (TDS_UNLIKELY(len < 0))
break;
}
return TDS_FAIL;
@@ -298,7 +298,7 @@ tds_dynamic_stream_write(TDSOUTSTREAM *stream, size_t len)
wanted = s->size + 2048;
if (wanted > s->allocated) {
void *p = realloc(*s->buf, wanted);
- if (!p) return -1;
+ if (TDS_UNLIKELY(!p)) return -1;
*s->buf = p;
s->allocated = wanted;
}
@@ -332,7 +332,7 @@ tds_dynamic_stream_init(TDSDYNAMICSTREAM * stream, void **ptr, size_t allocated)
if (allocated < initial_size) {
if (*ptr) free(*ptr);
*ptr = malloc(initial_size);
- if (!*ptr) return TDS_FAIL;
+ if (TDS_UNLIKELY(!*ptr)) return TDS_FAIL;
allocated = initial_size;
}
stream->allocated = allocated;

0 comments on commit 4fe4d15

Please sign in to comment.
Something went wrong with that request. Please try again.