Skip to content

Commit

Permalink
Optimize fiopen by reducing jumps
Browse files Browse the repository at this point in the history
  • Loading branch information
AreaZR committed Nov 4, 2021
1 parent 178b840 commit 4a2af40
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions stl/src/fiopen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ FILE* _Xfsopen(_In_z_ const wchar_t* filename, _In_ int mode, _In_ int prot) {

template <class CharT>
FILE* _Xfiopen(const CharT* filename, ios_base::openmode mode, int prot) {
static const int valid[] = {
static const ios_base::openmode valid[] = {
// valid combinations of open flags
ios_base::in,
ios_base::out,
Expand All @@ -41,7 +41,6 @@ FILE* _Xfiopen(const CharT* filename, ios_base::openmode mode, int prot) {
ios_base::in | ios_base::out | ios_base::binary,
ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary,
ios_base::in | ios_base::out | ios_base::app | ios_base::binary,
0,
};

FILE* fp = nullptr;
Expand All @@ -58,13 +57,12 @@ FILE* _Xfiopen(const CharT* filename, ios_base::openmode mode, int prot) {

mode &= ~(ios_base::ate | ios_base::_Nocreate | ios_base::_Noreplace);

// look for a valid mode
int n = 0;
while (valid[n] != 0 && valid[n] != mode) { // look for a valid mode
++n;
}

if (valid[n] == 0) {
return nullptr; // no valid mode
for (; valid[n] != node; ++n) {
if (n == sizeof(valid) / sizeof(valid[0])) {
return nullptr; // no valid mode
}
}

if (norepflag && (mode & (ios_base::out | ios_base::app))
Expand All @@ -81,12 +79,12 @@ FILE* _Xfiopen(const CharT* filename, ios_base::openmode mode, int prot) {
return nullptr; // open failed
}

if (!atendflag || fseek(fp, 0, SEEK_END) == 0) {
return fp; // no need to seek to end, or seek succeeded
if (atendflag && fseek(fp, 0, SEEK_END) != 0) {
fclose(fp); // can't position at end
return nullptr;
}

fclose(fp); // can't position at end
return nullptr;
return fp; // no need to seek to end, or seek succeeded
}

_CRTIMP2_PURE FILE* __CLRCALL_PURE_OR_CDECL _Fiopen(
Expand Down

0 comments on commit 4a2af40

Please sign in to comment.