Skip to content

Commit e04ca5b

Browse files
committed
Optimize: Eliminate C++ stream dependencies to reduce binary footprint by ~18.5 KB
1 parent 65abc3f commit e04ca5b

3 files changed

Lines changed: 86 additions & 49 deletions

File tree

QuickView/RenderEngine.cpp

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#include <vector>
66
#include <algorithm>
77
#include <array>
8-
#include <fstream>
9-
108
#include "RenderEngine.h"
119
#define CURRENT_MODULE "RenderEngine"
1210
#include "QuickViewETW.h"
@@ -224,21 +222,27 @@ bool BuildSrgbProfileBytes(std::vector<uint8_t> *outBytes) {
224222
}
225223
profilePath.resize(wcsnlen(profilePath.c_str(), pathLen));
226224

227-
std::ifstream stream(profilePath, std::ios::binary);
228-
if (!stream) {
225+
HANDLE hFile = CreateFileW(profilePath.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
226+
if (hFile == INVALID_HANDLE_VALUE) {
229227
return false;
230228
}
231229

232-
stream.seekg(0, std::ios::end);
233-
const std::streamoff size = stream.tellg();
234-
if (size <= 0) {
230+
LARGE_INTEGER fileSize;
231+
if (!GetFileSizeEx(hFile, &fileSize) || fileSize.QuadPart <= 0) {
232+
CloseHandle(hFile);
235233
return false;
236234
}
237235

238-
outBytes->resize(static_cast<size_t>(size));
239-
stream.seekg(0, std::ios::beg);
240-
stream.read(reinterpret_cast<char *>(outBytes->data()), size);
241-
return stream.good() || stream.eof();
236+
outBytes->resize(static_cast<size_t>(fileSize.QuadPart));
237+
DWORD bytesRead = 0;
238+
BOOL success = ReadFile(hFile, outBytes->data(), static_cast<DWORD>(fileSize.QuadPart), &bytesRead, nullptr);
239+
CloseHandle(hFile);
240+
241+
if (!success || bytesRead != fileSize.QuadPart) {
242+
outBytes->clear();
243+
return false;
244+
}
245+
return true;
242246
}
243247

244248
bool TryLoadProfileBytesForPrimaries(QuickView::ColorPrimaries primaries,
@@ -1536,14 +1540,21 @@ bool ResolveTargetProfileBlob(const CRenderEngine::GamutWarningAnalysisOptions&
15361540

15371541
if (options.targetKind == CRenderEngine::GamutTargetKind::ProofTarget &&
15381542
options.enableSoftProofing && !options.softProofProfilePath.empty()) {
1539-
std::ifstream stream(options.softProofProfilePath, std::ios::binary);
1540-
if (!stream) return false;
1541-
stream.seekg(0, std::ios::end);
1542-
const std::streamoff size = stream.tellg();
1543-
if (size <= 0) return false;
1544-
outBlob->bytes.resize(static_cast<size_t>(size));
1545-
stream.seekg(0, std::ios::beg);
1546-
stream.read(reinterpret_cast<char*>(outBlob->bytes.data()), size);
1543+
HANDLE hFile = CreateFileW(options.softProofProfilePath.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
1544+
if (hFile == INVALID_HANDLE_VALUE) return false;
1545+
LARGE_INTEGER fileSize;
1546+
if (!GetFileSizeEx(hFile, &fileSize) || fileSize.QuadPart <= 0) {
1547+
CloseHandle(hFile);
1548+
return false;
1549+
}
1550+
outBlob->bytes.resize(static_cast<size_t>(fileSize.QuadPart));
1551+
DWORD bytesRead = 0;
1552+
BOOL success = ReadFile(hFile, outBlob->bytes.data(), static_cast<DWORD>(fileSize.QuadPart), &bytesRead, nullptr);
1553+
CloseHandle(hFile);
1554+
if (!success || bytesRead != fileSize.QuadPart) {
1555+
outBlob->bytes.clear();
1556+
return false;
1557+
}
15471558
outBlob->name = options.softProofProfilePath.substr(
15481559
options.softProofProfilePath.find_last_of(L"\\/") == std::wstring::npos
15491560
? 0
@@ -1560,19 +1571,25 @@ bool ResolveTargetProfileBlob(const CRenderEngine::GamutWarningAnalysisOptions&
15601571
TryGetMonitorProfilePath(options.displayState, &monitorProfilePath) &&
15611572
!monitorProfilePath.empty() &&
15621573
!IsGenericSrgbProfilePath(monitorProfilePath)) {
1563-
std::ifstream stream(monitorProfilePath, std::ios::binary);
1564-
if (stream) {
1565-
stream.seekg(0, std::ios::end);
1566-
const std::streamoff size = stream.tellg();
1567-
if (size > 0) {
1568-
outBlob->bytes.resize(static_cast<size_t>(size));
1569-
stream.seekg(0, std::ios::beg);
1570-
stream.read(reinterpret_cast<char*>(outBlob->bytes.data()), size);
1571-
outBlob->name = monitorProfilePath.substr(
1572-
monitorProfilePath.find_last_of(L"\\/") == std::wstring::npos
1573-
? 0
1574-
: monitorProfilePath.find_last_of(L"\\/") + 1);
1575-
return !outBlob->bytes.empty();
1574+
HANDLE hFile = CreateFileW(monitorProfilePath.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
1575+
if (hFile != INVALID_HANDLE_VALUE) {
1576+
LARGE_INTEGER fileSize;
1577+
if (GetFileSizeEx(hFile, &fileSize) && fileSize.QuadPart > 0) {
1578+
outBlob->bytes.resize(static_cast<size_t>(fileSize.QuadPart));
1579+
DWORD bytesRead = 0;
1580+
BOOL success = ReadFile(hFile, outBlob->bytes.data(), static_cast<DWORD>(fileSize.QuadPart), &bytesRead, nullptr);
1581+
CloseHandle(hFile);
1582+
if (success && bytesRead == fileSize.QuadPart) {
1583+
outBlob->name = monitorProfilePath.substr(
1584+
monitorProfilePath.find_last_of(L"\\/") == std::wstring::npos
1585+
? 0
1586+
: monitorProfilePath.find_last_of(L"\\/") + 1);
1587+
return !outBlob->bytes.empty();
1588+
} else {
1589+
outBlob->bytes.clear();
1590+
}
1591+
} else {
1592+
CloseHandle(hFile);
15761593
}
15771594
}
15781595
}

QuickView/ThemeSystem.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "pch.h"
22
#include "ThemeSystem.h"
33
#include "yyjson.h"
4-
#include <fstream>
54
#include <commdlg.h>
65
#include <shlwapi.h>
76

@@ -74,13 +73,14 @@ namespace QuickView::UI::ThemeSystem {
7473
size_t len;
7574
char *json = yyjson_mut_write(doc, YYJSON_WRITE_PRETTY, &len);
7675
if (json) {
77-
std::ofstream ofs(path);
78-
if (ofs.is_open()) {
79-
ofs.write(json, len);
80-
ofs.close();
76+
HANDLE hFile = CreateFileW(path.c_str(), GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
77+
if (hFile != INVALID_HANDLE_VALUE) {
78+
DWORD written = 0;
79+
BOOL res = WriteFile(hFile, json, static_cast<DWORD>(len), &written, nullptr);
80+
CloseHandle(hFile);
8181
free(json);
8282
yyjson_mut_doc_free(doc);
83-
return true;
83+
return res && (written == len);
8484
}
8585
free(json);
8686
}
@@ -92,9 +92,21 @@ namespace QuickView::UI::ThemeSystem {
9292
std::wstring path = ShowFileDialog(hwnd, false);
9393
if (path.empty()) return false;
9494

95-
std::ifstream ifs(path, std::ios::binary);
96-
if (!ifs.is_open()) return false;
97-
std::string json_str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
95+
HANDLE hFile = CreateFileW(path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
96+
if (hFile == INVALID_HANDLE_VALUE) return false;
97+
98+
LARGE_INTEGER fileSize;
99+
if (!GetFileSizeEx(hFile, &fileSize) || fileSize.QuadPart <= 0) {
100+
CloseHandle(hFile);
101+
return false;
102+
}
103+
104+
std::string json_str(static_cast<size_t>(fileSize.QuadPart), '\0');
105+
DWORD bytesRead = 0;
106+
BOOL success = ReadFile(hFile, json_str.data(), static_cast<DWORD>(fileSize.QuadPart), &bytesRead, nullptr);
107+
CloseHandle(hFile);
108+
109+
if (!success || bytesRead != fileSize.QuadPart) return false;
98110

99111
yyjson_doc *doc = yyjson_read(json_str.c_str(), json_str.size(), 0);
100112
if (!doc) return false;

QuickView/UpdateManager.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
#include <string>
33
#include <vector>
44
#include <algorithm>
5-
#include <fstream>
6-
#include <ios>
75
#include <ctime>
86
#include "UpdateManager.h"
97
#include "yyjson.h"
@@ -78,8 +76,16 @@ void UpdateManager::CheckThread(int delaySeconds) {
7876
valid = false;
7977
}
8078
} else {
81-
std::ifstream f(dest, std::ios_base::binary | std::ios_base::ate);
82-
if (!f.good() || f.tellg() < 100000) valid = false;
79+
HANDLE hFile = CreateFileW(dest.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
80+
if (hFile == INVALID_HANDLE_VALUE) {
81+
valid = false;
82+
} else {
83+
LARGE_INTEGER size;
84+
if (!GetFileSizeEx(hFile, &size) || size.QuadPart < 100000) {
85+
valid = false;
86+
}
87+
CloseHandle(hFile);
88+
}
8389
}
8490
if (valid) {
8591
cached = true;
@@ -242,10 +248,12 @@ bool UpdateManager::DownloadUpdate(const std::string& url, const std::wstring& d
242248
}
243249

244250
// Write to file
245-
std::ofstream outfile(destPath, std::ios_base::binary);
246-
if (!outfile.is_open()) return false;
247-
outfile.write(data.c_str(), data.size());
248-
outfile.close();
251+
HANDLE hFile = CreateFileW(destPath.c_str(), GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
252+
if (hFile == INVALID_HANDLE_VALUE) return false;
253+
DWORD written = 0;
254+
BOOL res = WriteFile(hFile, data.data(), static_cast<DWORD>(data.size()), &written, nullptr);
255+
CloseHandle(hFile);
256+
return res && (written == data.size());
249257

250258
return true;
251259
}

0 commit comments

Comments
 (0)