Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize big stack allocation (move to heap) #3016

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,18 @@ namespace Sass {
bool file_exists(const std::string& path)
{
#ifdef _WIN32
wchar_t resolved[32768];
std::vector<wchar_t> resolved(32768);
glebm marked this conversation as resolved.
Show resolved Hide resolved
mgreter marked this conversation as resolved.
Show resolved Hide resolved
// windows unicode filepaths are encoded in utf16
std::string abspath(join_paths(get_cwd(), path));
if (!(abspath[0] == '/' && abspath[1] == '/')) {
abspath = "//?/" + abspath;
}
std::wstring wpath(UTF_8::convert_to_utf16(abspath));
std::replace(wpath.begin(), wpath.end(), '/', '\\');
DWORD rv = GetFullPathNameW(wpath.c_str(), 32767, resolved, NULL);
DWORD rv = GetFullPathNameW(wpath.c_str(), 32767, &resolved[0], NULL);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
DWORD rv = GetFullPathNameW(wpath.c_str(), 32767, &resolved[0], NULL);
DWORD rv = GetFullPathNameW(wpath.c_str(), max_chars, resolved.data(), NULL);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be necessary, since GetFullPathNameW will fill the buffer anyway!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This suggestion is to avoid repeating 32767 and to use .data() instead of &[0]

if (rv > 32767) throw Exception::OperationError("Path is too long");
if (rv == 0) throw Exception::OperationError("Path could not be resolved");
DWORD dwAttrib = GetFileAttributesW(resolved);
DWORD dwAttrib = GetFileAttributesW(&resolved[0]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
DWORD dwAttrib = GetFileAttributesW(&resolved[0]);
DWORD dwAttrib = GetFileAttributesW(resolved.data());

return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
(!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)));
#else
Expand Down Expand Up @@ -437,18 +437,18 @@ namespace Sass {
#ifdef _WIN32
BYTE* pBuffer;
DWORD dwBytes;
wchar_t resolved[32768];
std::vector<wchar_t> resolved(32768);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::vector<wchar_t> resolved(32768);
const std::size_t max_chars = 32767;
std::vector<wchar_t> resolved(max_chars + 1, 0);

// windows unicode filepaths are encoded in utf16
std::string abspath(join_paths(get_cwd(), path));
if (!(abspath[0] == '/' && abspath[1] == '/')) {
abspath = "//?/" + abspath;
}
std::wstring wpath(UTF_8::convert_to_utf16(abspath));
std::replace(wpath.begin(), wpath.end(), '/', '\\');
DWORD rv = GetFullPathNameW(wpath.c_str(), 32767, resolved, NULL);
DWORD rv = GetFullPathNameW(wpath.c_str(), 32767, &resolved[0], NULL);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
DWORD rv = GetFullPathNameW(wpath.c_str(), 32767, &resolved[0], NULL);
DWORD rv = GetFullPathNameW(wpath.c_str(), max_chars, resolved.data(), NULL);

if (rv > 32767) throw Exception::OperationError("Path is too long");
if (rv == 0) throw Exception::OperationError("Path could not be resolved");
HANDLE hFile = CreateFileW(resolved, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
HANDLE hFile = CreateFileW(&resolved[0], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
HANDLE hFile = CreateFileW(&resolved[0], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
HANDLE hFile = CreateFileW(resolved.data(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);

if (hFile == INVALID_HANDLE_VALUE) return 0;
DWORD dwFileLength = GetFileSize(hFile, NULL);
if (dwFileLength == INVALID_FILE_SIZE) return 0;
Expand Down