Skip to content

Commit 8a9f65e

Browse files
committed
Performance improvements by replacing sprintf with strcpy/strcat. Additional avoid creating temporary String objects.
1 parent fd72cf4 commit 8a9f65e

File tree

1 file changed

+50
-16
lines changed

1 file changed

+50
-16
lines changed

Diff for: libraries/FS/src/vfs_api.cpp

+50-16
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ FileImplPtr VFSImpl::open(const char* fpath, const char* mode, const bool create
3636
return FileImplPtr();
3737
}
3838

39-
sprintf(temp,"%s%s", _mountpoint, fpath);
39+
strcpy(temp, _mountpoint);
40+
strcat(temp, fpath);
4041

4142
struct stat st;
4243
//file found
@@ -136,19 +137,25 @@ bool VFSImpl::rename(const char* pathFrom, const char* pathTo)
136137
log_e("%s does not exists", pathFrom);
137138
return false;
138139
}
139-
char * temp1 = (char *)malloc(strlen(pathFrom)+strlen(_mountpoint)+1);
140+
size_t mountpointLen = strlen(_mountpoint);
141+
char * temp1 = (char *)malloc(strlen(pathFrom)+mountpointLen+1);
140142
if(!temp1) {
141143
log_e("malloc failed");
142144
return false;
143145
}
144-
char * temp2 = (char *)malloc(strlen(pathTo)+strlen(_mountpoint)+1);
146+
char * temp2 = (char *)malloc(strlen(pathTo)+mountpointLen+1);
145147
if(!temp2) {
146148
free(temp1);
147149
log_e("malloc failed");
148150
return false;
149151
}
150-
sprintf(temp1,"%s%s", _mountpoint, pathFrom);
151-
sprintf(temp2,"%s%s", _mountpoint, pathTo);
152+
153+
strcpy(temp1, _mountpoint);
154+
strcat(temp1, pathFrom);
155+
156+
strcpy(temp2, _mountpoint);
157+
strcat(temp2, pathTo);
158+
152159
auto rc = ::rename(temp1, temp2);
153160
free(temp1);
154161
free(temp2);
@@ -182,7 +189,10 @@ bool VFSImpl::remove(const char* fpath)
182189
log_e("malloc failed");
183190
return false;
184191
}
185-
sprintf(temp,"%s%s", _mountpoint, fpath);
192+
193+
strcpy(temp, _mountpoint);
194+
strcat(temp, fpath);
195+
186196
auto rc = unlink(temp);
187197
free(temp);
188198
return rc == 0;
@@ -211,7 +221,10 @@ bool VFSImpl::mkdir(const char *fpath)
211221
log_e("malloc failed");
212222
return false;
213223
}
214-
sprintf(temp,"%s%s", _mountpoint, fpath);
224+
225+
strcpy(temp, _mountpoint);
226+
strcat(temp, fpath);
227+
215228
auto rc = ::mkdir(temp, ACCESSPERMS);
216229
free(temp);
217230
return rc == 0;
@@ -244,7 +257,10 @@ bool VFSImpl::rmdir(const char *fpath)
244257
log_e("malloc failed");
245258
return false;
246259
}
247-
sprintf(temp,"%s%s", _mountpoint, fpath);
260+
261+
strcpy(temp, _mountpoint);
262+
strcat(temp, fpath);
263+
248264
auto rc = ::rmdir(temp);
249265
free(temp);
250266
return rc == 0;
@@ -265,7 +281,9 @@ VFSFileImpl::VFSFileImpl(VFSImpl* fs, const char* fpath, const char* mode)
265281
if(!temp) {
266282
return;
267283
}
268-
sprintf(temp,"%s%s", _fs->_mountpoint, fpath);
284+
285+
strcpy(temp, _fs->_mountpoint);
286+
strcat(temp, fpath);
269287

270288
_path = strdup(fpath);
271289
if(!_path) {
@@ -362,7 +380,10 @@ void VFSFileImpl::_getStat() const
362380
if(!temp) {
363381
return;
364382
}
365-
sprintf(temp,"%s%s", _fs->_mountpoint, _path);
383+
384+
strcpy(temp, _fs->_mountpoint);
385+
strcat(temp, _path);
386+
366387
if(!stat(temp, &_stat)) {
367388
_written = false;
368389
}
@@ -466,14 +487,27 @@ FileImplPtr VFSFileImpl::openNextFile(const char* mode)
466487
if(file->d_type != DT_REG && file->d_type != DT_DIR) {
467488
return openNextFile(mode);
468489
}
469-
String fname = String(file->d_name);
470-
String name = String(_path);
471-
if(!fname.startsWith("/") && !name.endsWith("/")) {
472-
name += "/";
490+
491+
size_t pathLen = strlen(_path);
492+
size_t fileNameLen = strlen(file->d_name);
493+
char * name = (char *)malloc(pathLen+fileNameLen+2);
494+
495+
if(name == NULL) {
496+
return FileImplPtr();
473497
}
474-
name += fname;
475498

476-
return std::make_shared<VFSFileImpl>(_fs, name.c_str(), mode);
499+
strcpy(name, _path);
500+
501+
if ((file->d_name[0] != '/') && (_path[pathLen - 1] != '/'))
502+
{
503+
strcat(name, "/");
504+
}
505+
506+
strcat(name, file->d_name);
507+
508+
FileImplPtr fileImplPtr = std::make_shared<VFSFileImpl>(_fs, name, mode);
509+
free(name);
510+
return fileImplPtr;
477511
}
478512

479513
void VFSFileImpl::rewindDirectory(void)

0 commit comments

Comments
 (0)