Skip to content

Commit

Permalink
Avoid a copy in AuthSrv downloads.
Browse files Browse the repository at this point in the history
For auth files, this looked something like:
Disk/SDL Blob -> Temporary Buffer -> Send Buffer -> OS Buffer

For minified SDL, we had the same thing, but instead of the Disk as a
backing store, we were using a buffer in userspace, which meant there
were three redundant copies instead of just two in disk file case. This
removes the temporary buffer from the equation such that the process is
now:

Disk/SDL Blob -> Send Buffer -> OS Buffer
  • Loading branch information
Hoikas committed Aug 27, 2023
1 parent ed77283 commit 2f1595b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
10 changes: 4 additions & 6 deletions AuthServ/AuthServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,16 +646,15 @@ void cb_downloadStart(AuthServer_Private& client)
client.m_buffer.write<uint32_t>(stream->size());
client.m_buffer.write<uint32_t>(stream->tell());

uint8_t data[CHUNK_SIZE];
if (stream->size() > CHUNK_SIZE) {
client.m_buffer.write<uint32_t>(CHUNK_SIZE);
uint8_t* data = client.m_buffer.allocate(CHUNK_SIZE);
stream->readBytes(data, CHUNK_SIZE);
client.m_buffer.writeBytes(data, CHUNK_SIZE);
client.m_downloads[transId] = stream;
} else {
client.m_buffer.write<uint32_t>(stream->size());
uint8_t* data = client.m_buffer.allocate(stream->size());
stream->readBytes(data, stream->size());
client.m_buffer.writeBytes(data, stream->size());
delete stream;
}

Expand All @@ -680,16 +679,15 @@ void cb_downloadNext(AuthServer_Private& client)
client.m_buffer.write<uint32_t>(fi->second->size());
client.m_buffer.write<uint32_t>(fi->second->tell());

uint8_t data[CHUNK_SIZE];
size_t bytesLeft = fi->second->size() - fi->second->tell();
if (bytesLeft > CHUNK_SIZE) {
client.m_buffer.write<uint32_t>(CHUNK_SIZE);
uint8_t* data = client.m_buffer.allocate(CHUNK_SIZE);
fi->second->readBytes(data, CHUNK_SIZE);
client.m_buffer.writeBytes(data, CHUNK_SIZE);
} else {
client.m_buffer.write<uint32_t>(bytesLeft);
uint8_t* data = client.m_buffer.allocate(bytesLeft);
fi->second->readBytes(data, bytesLeft);
client.m_buffer.writeBytes(data, bytesLeft);
delete fi->second;
client.m_downloads.erase(fi);
}
Expand Down
21 changes: 21 additions & 0 deletions streams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,27 @@ void DS::BufferStream::steal(uint8_t* buffer, size_t size)
m_position = 0;
}

uint8_t* DS::BufferStream::allocate(size_t size)
{
if (m_position + size > m_alloc) {
// Resize stream
size_t bigger = m_alloc ? m_alloc : 4096;
while (m_position + size > bigger)
bigger *= 2;
uint8_t* newbuffer = new uint8_t[bigger];
if (m_size != 0)
memcpy(newbuffer, m_buffer, m_size);
delete[] m_buffer;
m_buffer = newbuffer;
m_alloc = bigger;
}
uint8_t* result = m_buffer + m_position;
m_position += size;
if (m_position > m_size)
m_size = m_position;
return result;
}


ssize_t DS::BlobStream::readBytes(void* buffer, size_t count)
{
Expand Down
2 changes: 2 additions & 0 deletions streams.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ namespace DS
void set(const void* buffer, size_t size);
void steal(uint8_t* buffer, size_t size);

uint8_t* allocate(size_t size);

void ref() { ++m_refs; }
void unref()
{
Expand Down

0 comments on commit 2f1595b

Please sign in to comment.