Commit 2bc38ad1 authored by Stefan Westerfeld's avatar Stefan Westerfeld

SFInputStream: optimize common case in virtual read

Signed-off-by: Stefan Westerfeld's avatarStefan Westerfeld <stefan@space.twc.de>
parent ec3df94f
......@@ -18,6 +18,7 @@
#include "sfinputstream.hh"
#include <assert.h>
#include <string.h>
using std::string;
using std::vector;
......@@ -202,14 +203,23 @@ virtual_read (void *ptr, sf_count_t count, void *data)
SFInputStream::VirtualData *vdata = static_cast<SFInputStream::VirtualData *> (data);
int rcount = 0;
unsigned char *uptr = static_cast<unsigned char *> (ptr);
for (sf_count_t i = 0; i < count; i++)
if (size_t (vdata->offset + count) <= vdata->mem->size())
{
/* fast case: read can be fully satisfied with the data we have */
memcpy (ptr, &(*vdata->mem)[vdata->offset], count);
rcount = count;
}
else
{
size_t rpos = i + vdata->offset;
if (rpos < vdata->mem->size())
unsigned char *uptr = static_cast<unsigned char *> (ptr);
for (sf_count_t i = 0; i < count; i++)
{
uptr[i] = (*vdata->mem)[rpos];
rcount++;
size_t rpos = i + vdata->offset;
if (rpos < vdata->mem->size())
{
uptr[i] = (*vdata->mem)[rpos];
rcount++;
}
}
}
vdata->offset += rcount;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment