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,6 +203,14 @@ virtual_read (void *ptr, sf_count_t count, void *data)
SFInputStream::VirtualData *vdata = static_cast<SFInputStream::VirtualData *> (data);
int rcount = 0;
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
{
unsigned char *uptr = static_cast<unsigned char *> (ptr);
for (sf_count_t i = 0; i < count; i++)
{
......@@ -212,6 +221,7 @@ virtual_read (void *ptr, sf_count_t count, void *data)
rcount++;
}
}
}
vdata->offset += rcount;
return 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