From 8ed25535a966f6386e663612a6eb7816546e1798 Mon Sep 17 00:00:00 2001 From: Alfonso Gregory <83477269+gAlfonso-bit@users.noreply.github.com> Date: Thu, 5 Aug 2021 15:02:27 -0400 Subject: [PATCH 1/4] Optimize fiopen by reducing jumps --- stl/src/fiopen.cpp | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/stl/src/fiopen.cpp b/stl/src/fiopen.cpp index d304f37e08..d04ac13167 100644 --- a/stl/src/fiopen.cpp +++ b/stl/src/fiopen.cpp @@ -25,7 +25,7 @@ FILE* _Xfsopen(_In_z_ const wchar_t* filename, _In_ int mode, _In_ int prot) { template 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, @@ -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; @@ -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] != mode; ++n) { + if (n == sizeof(valid) / sizeof(valid[0])) { + return nullptr; // no valid mode + } } if (norepflag && (mode & (ios_base::out | ios_base::app)) @@ -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( From 536dc3a3504062c24799a30baa89d270539030a3 Mon Sep 17 00:00:00 2001 From: Rose <83477269+AtariDreams@users.noreply.github.com> Date: Wed, 9 Feb 2022 12:04:08 -0500 Subject: [PATCH 2/4] Update stl/src/fiopen.cpp Co-authored-by: Casey Carter --- stl/src/fiopen.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stl/src/fiopen.cpp b/stl/src/fiopen.cpp index d04ac13167..92e299d716 100644 --- a/stl/src/fiopen.cpp +++ b/stl/src/fiopen.cpp @@ -60,7 +60,7 @@ FILE* _Xfiopen(const CharT* filename, ios_base::openmode mode, int prot) { // look for a valid mode int n = 0; for (; valid[n] != mode; ++n) { - if (n == sizeof(valid) / sizeof(valid[0])) { + if (n == size(valid)) { return nullptr; // no valid mode } } From 8e16005c08a12684a598a823546699c79250f3a6 Mon Sep 17 00:00:00 2001 From: Casey Carter Date: Wed, 16 Feb 2022 13:42:18 -0800 Subject: [PATCH 3/4] Update stl/src/fiopen.cpp --- stl/src/fiopen.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stl/src/fiopen.cpp b/stl/src/fiopen.cpp index 92e299d716..115a9bdef5 100644 --- a/stl/src/fiopen.cpp +++ b/stl/src/fiopen.cpp @@ -59,8 +59,8 @@ FILE* _Xfiopen(const CharT* filename, ios_base::openmode mode, int prot) { // look for a valid mode int n = 0; - for (; valid[n] != mode; ++n) { - if (n == size(valid)) { + while (valid[n] != mode) { + if (++n == _STD size(valid)) { return nullptr; // no valid mode } } From fdd346c2b2cfc874d3cdd5b69d682b6506054430 Mon Sep 17 00:00:00 2001 From: Casey Carter Date: Wed, 16 Feb 2022 14:12:00 -0800 Subject: [PATCH 4/4] Update stl/src/fiopen.cpp oops bugs. --- stl/src/fiopen.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stl/src/fiopen.cpp b/stl/src/fiopen.cpp index 115a9bdef5..9a1386f83c 100644 --- a/stl/src/fiopen.cpp +++ b/stl/src/fiopen.cpp @@ -60,7 +60,7 @@ FILE* _Xfiopen(const CharT* filename, ios_base::openmode mode, int prot) { // look for a valid mode int n = 0; while (valid[n] != mode) { - if (++n == _STD size(valid)) { + if (++n == static_cast(_STD size(valid))) { return nullptr; // no valid mode } }