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

Platform: replace use of std::string with String and StringView #559

Closed

Conversation

Squareys
Copy link
Contributor

@Squareys Squareys commented Apr 15, 2022

Hi @mosra!

Since all the std::string is removed elsewhere, it's now very worth removing it from GlfwApplication.h also!

Best,
Jonathan

TODO

  • Compile the test
  • Run GlfwApplicationTest (currently crashes on some unset dpi policy?)

Copy link
Owner

@mosra mosra left a comment

Choose a reason for hiding this comment

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

Err... it's not that simple 😅

And while at it, it'd be good to make corresponding changes in other application implementations, because otherwise e.g. switching between EmscriptenApplication and GlfwApplication may cause nasty behavioral differences.

... I guess I take over and finish this together with the other apps, eventually. Hopefully you don't need to rely on this too soon? :)

src/Magnum/Platform/GlfwApplication.cpp Outdated Show resolved Hide resolved
@@ -113,7 +113,7 @@ GlfwApplication::GlfwApplication(const Arguments& arguments, NoCreateT):
else if(dpiScaling == "physical")
_commandLineDpiScalingPolicy = Implementation::GlfwDpiScalingPolicy::Physical;
#endif
else if(dpiScaling.find_first_of(" \t\n") != std::string::npos)
else if(dpiScaling.find(' ').data() || dpiScaling.find('\t').data() || dpiScaling.find('\n').data())
Copy link
Owner

Choose a reason for hiding this comment

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

What's up with the .data()?

Actually, this change should wait until I expose an alternative to find_first_of(). It's there, just not public.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What's up with the .data()?

Ah, I guess you mean I could be using operator bool() instead? I see now, that the only empty view ever returned here would be the nullptr one 👍

Copy link
Owner

Choose a reason for hiding this comment

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

Yeah that, but more like I should finish the API first so this doesn't need to be temporarily implemented via insufficient lacking APIs. Something like if(dpiScaling.containsAnyOf(...)) would be ideal.

src/Magnum/Platform/GlfwApplication.cpp Outdated Show resolved Hide resolved
src/Magnum/Platform/GlfwApplication.cpp Show resolved Hide resolved
src/Magnum/Platform/GlfwApplication.cpp Outdated Show resolved Hide resolved
src/Magnum/Platform/GlfwApplication.h Show resolved Hide resolved
src/Magnum/Platform/GlfwApplication.h Outdated Show resolved Hide resolved
@Squareys Squareys force-pushed the glfw-application-remove-string branch from 8437538 to f7fc0e0 Compare April 15, 2022 20:55
@Squareys
Copy link
Contributor Author

@mosra Thanks for the review! This is more a "nice to have", since it will improve our compile times (eventually, when we're able to turn off MAGNUM_BUILD_DEPRECATED).

I might also have a look at making these changes for EmscriptenApplication tomorrow.

@mosra
Copy link
Owner

mosra commented Apr 15, 2022

.. and SDL, and Android, and ... ;)

Signed-off-by: Squareys <squareys@googlemail.com>
Signed-off-by: Squareys <squareys@googlemail.com>
@Squareys Squareys force-pushed the glfw-application-remove-string branch from f7fc0e0 to 4768ea6 Compare April 18, 2022 19:27
Signed-off-by: Squareys <squareys@googlemail.com>
Copy link
Owner

@mosra mosra left a comment

Choose a reason for hiding this comment

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

Uh, I don't know but, like, the changes don't even compile 😅 I just gave up reviewing midway through because every change in the EmscriptenApplication was wrong one way or another.

What's the point of even pushing such a commit. Sorry to be so harsh, but it's easier for me to just discard the whole PR and start over, figuring out the changes from scratch, than attempting to review what's here and risk that I accidentally overlook a unhandled corner case in what looks like a hasty find&replace operation.

std::string str = id;
std::free(id);
return str;
return Containers::String{id, std::strlen(id), &std::free};
Copy link
Owner

Choose a reason for hiding this comment

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

The deleter takes a pointer and a size, so you can't just pass std::free() here.

@@ -469,14 +467,14 @@ Vector2i EmscriptenApplication::framebufferSize() const {
}
#endif

void EmscriptenApplication::setWindowTitle(const std::string& title) {
void EmscriptenApplication::setWindowTitle(const Containers::StringView title) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdollar-in-identifier-extension"
EM_ASM_({document.title = UTF8ToString($0);}, title.data());
Copy link
Owner

Choose a reason for hiding this comment

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

💥

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdollar-in-identifier-extension"
EM_ASM_({document.title = UTF8ToString($0);}, title.data());
#pragma GCC diagnostic pop
}

void EmscriptenApplication::setContainerCssClass(const std::string& cssClass) {
void EmscriptenApplication::setContainerCssClass(const Containers::StringView cssClass) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdollar-in-identifier-extension"
EM_ASM_({
Copy link
Owner

Choose a reason for hiding this comment

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

💥 here also

@@ -605,7 +603,7 @@ void EmscriptenApplication::setupCallbacks(bool resizable) {
}));
#pragma GCC diagnostic pop

std::string keyboardListeningElementString;
Containers::StringView keyboardListeningElementString;
Copy link
Owner

Choose a reason for hiding this comment

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

💥, again, and the += doesn't even compile.

@mosra
Copy link
Owner

mosra commented May 26, 2022

I took your changes as a base for a6da074...53a74b5 and kept you as a co-author, but the changes are significantly different from what's in this PR. Tried to convince GitHub to show a diff between the two so you could see what additionally had to be done, but the best I got includes also a ton of other changes that happened since the PR was opened.

Things worth mentioning:

  • All functions that had to take care of non-null-terminated views are now tested in the corresponding application tests, by passing a slice out of a larger string. It's manual testing, but better than nothing.
  • The default application title now also uses a non-owning String, instead of always allocating a copy
  • Emscripten actually has an API to create a string from a pointer + size, so that's what I used instead of potentially allocating another copy. Important: the documentation of UTF8ToString() mentions that one should consistently use either the sized or the unsized variant, to make the JIT optimize the function better. I bet you use this quite a lot in your codebase, so maybe check which variant you have and make it consistent.
  • The TextInputEvent and TextEditingEvent used to use an ArrayView to avoid a string allocation. That was a temporary solution and they now use a StringView as well, which allowed me to remove an ArrayView.h include from all application headers, further reducing the bloat.

@mosra mosra closed this May 26, 2022
@Squareys
Copy link
Contributor Author

Squareys commented May 30, 2022

I bet you use this quite a lot in your codebase, so maybe check which variant you have and make it consistent.

Quick note, because we're not using UTF8ToString() at all anymore: UTF8ToString() will call a JavaScript implementation of strlen in any case, it uses the second parameter only as a "buffer size, but the string might be shorter" parameter. We switched during the process of switching to StringView.

See also emscripten-core/emscripten#12517.

@mosra
Copy link
Owner

mosra commented May 30, 2022

it uses the second parameter only as a "buffer size, but the string might be shorter" parameter

Yes, I'm aware, but apart from the unnecessary strlen it's the behavior I want, to not make it any longer than the originating StringView is.

we're not using UTF8ToString() at all anymore

So you have your own implementation of this, based on emscripten-core/emscripten#12517, and call that instead? In my case I'm not sure I'd want to bother (providing a JS file with a non-shitty implementation, bundling it to all produced executables, all that extra pain...).

The Stale Bot is a cancer, FFS. So many issues get just sweeped under the rug due to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

2 participants