Skip to content

Commit

Permalink
tune the buffer size for reading file
Browse files Browse the repository at this point in the history
  • Loading branch information
kolyvan committed Jan 8, 2016
1 parent eed7681 commit 30fc7b8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
15 changes: 11 additions & 4 deletions KxSMBProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -2158,31 +2158,38 @@ - (id)readDataOfLength:(NSUInteger)length
NSError *error = [self openFile];
if (error) return error;
}

Byte buffer[32768];

// it seems 512 kb is an optimal buffer size
const size_t bufferSize = MIN(length, 512*1024);
Byte *buffer = malloc(bufferSize);
if (!buffer) {
return mkKxSMBError(KxSMBErrorOutOfMemory, nil);
}

smbc_read_fn readFn = smbc_getFunctionRead(_context);
NSMutableData *md = [NSMutableData data];
NSInteger bytesToRead = length;

while (bytesToRead > 0) {

ssize_t r = readFn(_context, _file, buffer, MIN(bytesToRead, sizeof(buffer)));
ssize_t r = readFn(_context, _file, buffer, MIN(bytesToRead, bufferSize));

if (r == 0)
break;

if (r < 0) {

const int err = errno;
free(buffer);
return mkKxSMBError(errnoToSMBErr(err),
NSLocalizedString(@"Unable read file:%@ (errno:%d)", nil), _path, err);
}

[md appendBytes:buffer length:r];
bytesToRead -= r;
}


free(buffer);
return md;
}

Expand Down
2 changes: 1 addition & 1 deletion KxSMBSample/FileViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ -(void) updateDownloadStatus: (id) result
- (void) download
{
__weak __typeof(self) weakSelf = self;
[_smbFile readDataOfLength:32768
[_smbFile readDataOfLength:1024*1024
block:^(id result)
{
FileViewController *p = weakSelf;
Expand Down

0 comments on commit 30fc7b8

Please sign in to comment.