Skip to content
This repository has been archived by the owner on Jul 1, 2024. It is now read-only.

Working on source code [Refactor] #836

Merged
merged 1 commit into from
Aug 19, 2022
Merged

Conversation

GermanAizek
Copy link
Contributor

Explain the Pull Request

Minor optimizations and code simplification. Bringing to a modern C++ standard.

Related Issues

None

Checklist

  • I will become the maintainer for this part of code.
  • I have tested this code on all supported Platforms.
  • I have tested this code on Windows.

@GermanAizek GermanAizek requested a review from Xaymar as a code owner July 21, 2022 11:21
Copy link
Owner

@Xaymar Xaymar left a comment

Choose a reason for hiding this comment

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

Minor issues with code clarity, otherwise fine.

  • It seems like many of the const std::string& can be replaced with std::string_view with no impact on code clarity, but a positive impact on performance.
  • auto is fine if used with specifies like const, & and * for code clarity. Always provide the full intent behind the code to both the reader and the compiler.
  • Formatting of multiple files is incorrect - ensure you have the latest LLVM/Clang version installed and have run it.
  • The commits are formatted incorrectly: https://github.com/Xaymar/obs-StreamFX/blob/master/CONTRIBUTING.md#commits

data/locale/fr-FR.ini Outdated Show resolved Hide resolved
data/locale/si-LK.ini Outdated Show resolved Hide resolved
source/obs/obs-tools.hpp Outdated Show resolved Hide resolved
source/ui/ui-about.cpp Outdated Show resolved Hide resolved
source/ui/ui-updater.cpp Outdated Show resolved Hide resolved
source/filters/filter-blur.cpp Outdated Show resolved Hide resolved
source/filters/filter-blur.cpp Outdated Show resolved Hide resolved
source/encoders/handlers/dnxhd_handler.cpp Show resolved Hide resolved
source/gfx/shader/gfx-shader.cpp Outdated Show resolved Hide resolved
source/gfx/shader/gfx-shader.cpp Outdated Show resolved Hide resolved
@Xaymar
Copy link
Owner

Xaymar commented Jul 22, 2022

FWIW, the use of auto is fine, but the use of explicit types should be preferred for code clarity. As it is, this PR sacrifices code clarity for slightly more compiler flexibility.

@Xaymar Xaymar added changes-required Additional changes or information required. testing-required Change requires additional testing labels Jul 22, 2022
@GermanAizek
Copy link
Contributor Author

@Xaymar,
OK, I'd rather replace const std::string& with std::string_view. It will be better for updating the code to C++17
I will correct the formatting of the code, today I will correct the code according to your requirements with your authorship

FWIW, the use of auto is fine, but the use of explicit types should be preferred for code clarity. As it is, this PR sacrifices code clarity for slightly more compiler flexibility.

@Xaymar,
If the function returns const char*, applying the type to the result variable can have a positive effect on optimization, the compiler itself will determine that it is better to use C-string, std::string or std::string_view, but provided that no one explicitly overrides to another type. const auto or auto in modern C++ code is already the norm for iterators, lambdas, range-based for loop, it's worth getting used to.

Example:

how readable is the code?

before:

std::map<int, std::string> myMap;
myMap.emplace(0, "Ergo");
myMap.emplace(1, "Proxy");

for (std::map<int, string>::iterator it = myMap.begin(); it != myMap.end(); ++it){
    std::cout << it->first<< " has value " << it->second << std::endl;
}

after:

std::map<int, std::string> myMap;
myMap.emplace(0, "Ergo");
myMap.emplace(1, "Proxy");

for (const auto& [key, value]: myMap) {
    std::cout << key << " has value " << value << std::endl;
}

In terms of readability in loops, variables were assigned as key and value rather than it->first and it->second.
Writing smaller code allows you to achieve a similar algorithm without iterators.

@Xaymar
Copy link
Owner

Xaymar commented Jul 22, 2022

how readable is the code?

In the example you provided you can either opt for decltype(...)::iterator or const auto&, but not auto. This is because auto by itself drastically reduces code clarity since it does not convey any information other than "Try guessing what this will be!". However this example doesn't even remotely compare to the errors made in this Pull Request.

We are dealing with a third party API here, which if changed could drastically change how the code behaves. While we can somewhat guarantee that it won't happen, we still want the compiler to pick it up at the point where we use the API - and not any later. This also helps with code clarity as a reader would not have to open the API documentation to figure out the return type.

Code Clarity is always about explicitly stating intent. When the options are unclear intent and human-decoding taking longer, or clear intent and human-decoding is instant, the latter option is always preferred if not even required. This project isn't the only project following this rule - many other massive open source projects follow the same pattern.

@Xaymar
Copy link
Owner

Xaymar commented Jul 22, 2022

Forgot to mention the other problem of const char* possibly being a nullptr, thus invoking undefined behavior in many compilers and libraries that are still being supported. Therefore the use of auto is even more dangerous when the compiler starts assuming that functions which return const char* will never return nullptr, and replaces it with std::string_view instead.

@GermanAizek
Copy link
Contributor Author

@Xaymar, should const char* be changed to std::string_view?

@Xaymar
Copy link
Owner

Xaymar commented Jul 22, 2022

should const char* be changed to std::string_view?

Only if we control the entire initialization part of std::string_view. It should not be changed to std::string_view if it faces an external API which may return nullptr (not supported by the C++17 standard, or all C++17 compatible libraries). So:

std::string_view potential_nullptr = obs_data_get_string(data, "text")); // No, not supported by the standard without extensions.
const char* potential_nullptr = obs_data_get_string(data, "text")); // Yes.
std::string_view technique = "Draw"; // Also yes, no external API dependency.

@GermanAizek GermanAizek requested a review from Xaymar July 25, 2022 19:46
Xaymar
Xaymar previously requested changes Jul 27, 2022
Copy link
Owner

@Xaymar Xaymar left a comment

Choose a reason for hiding this comment

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

Remove the translation files, fix the commit titles and descriptions and this is good.

source/obs/gs/gs-effect.cpp Show resolved Hide resolved
@Xaymar Xaymar removed the changes-required Additional changes or information required. label Jul 30, 2022
@Xaymar
Copy link
Owner

Xaymar commented Aug 6, 2022

Will test this on all platforms this weekend. If it fails, I'll reject the PR, if it works, I'll merge.

@GermanAizek
Copy link
Contributor Author

GermanAizek commented Aug 8, 2022

@Xaymar, hi
I've been busy for two weeks, I can fix remaining changes if necessary

@Xaymar
Copy link
Owner

Xaymar commented Aug 9, 2022

There's only a rebase left, as well as verifying that the & doesn't cause memory corruptions.

@Xaymar
Copy link
Owner

Xaymar commented Aug 14, 2022

As I'm preparing to build for Qt6+OBS Studio 28.0, testing for this is a bit delayed still. I handled the rebasing and commit style though, so that part is done.

source/obs/gs/gs-rendertarget.hpp Outdated Show resolved Hide resolved
source/obs/gs/gs-vertexbuffer.hpp Outdated Show resolved Hide resolved
source/util/util-event.hpp Outdated Show resolved Hide resolved
source/util/util-event.hpp Outdated Show resolved Hide resolved
- Use auto in places where code clarity is improved or identical.
- Replace trivial constructors and destructors with default.
- Use true random for random generation.
- Use std::string_view where it is valid to do so.
- Apply const where it is valid to do so.
- Use references where it is valid to do so.
- Manually optimize memory usage with std::move and std::copy.
- Opt for memory efficient containers where the size is known ahead of time.

Signed-off-by: lainon <GermanAizek@yandex.ru>
@Xaymar
Copy link
Owner

Xaymar commented Aug 19, 2022

LGTM

@Xaymar Xaymar merged commit 3a79b1b into Xaymar:master Aug 19, 2022
Xaymar added a commit that referenced this pull request Aug 19, 2022
The use of const references breaks '#include' for relative paths.
Xaymar added a commit that referenced this pull request Aug 19, 2022
The use of const references breaks '#include' for relative paths.
Xaymar added a commit that referenced this pull request Aug 19, 2022
The use of const references breaks '#include' for relative paths.
Xaymar added a commit that referenced this pull request Aug 29, 2022
The compiler will choose the optimal way automatically, and forcing std::move here actually results in two objects existing side by side, before being "moved" into one.
Xaymar added a commit that referenced this pull request Aug 30, 2022
The compiler will choose the optimal way automatically, and forcing std::move here actually results in two objects existing side by side, before being "moved" into one.
Xaymar added a commit that referenced this pull request Aug 30, 2022
It is not valid to pass std::string_view to snprintf's %s.
Xaymar added a commit that referenced this pull request Aug 30, 2022
The compiler will choose the optimal way automatically, and forcing std::move here actually results in two objects existing side by side, before being "moved" into one.
Xaymar added a commit that referenced this pull request Aug 30, 2022
It is not valid to pass std::string_view to snprintf's %s.
Xaymar added a commit that referenced this pull request Aug 30, 2022
The compiler will choose the optimal way automatically, and forcing std::move here actually results in two objects existing side by side, before being "moved" into one.
Xaymar added a commit that referenced this pull request Mar 27, 2023
The use of const references breaks '#include' for relative paths.
Xaymar added a commit that referenced this pull request Mar 27, 2023
It is not valid to pass std::string_view to snprintf's %s.
Xaymar added a commit that referenced this pull request Mar 27, 2023
The compiler will choose the optimal way automatically, and forcing std::move here actually results in two objects existing side by side, before being "moved" into one.
Xaymar added a commit that referenced this pull request Mar 28, 2023
The use of const references breaks '#include' for relative paths.
Xaymar added a commit that referenced this pull request Mar 28, 2023
It is not valid to pass std::string_view to snprintf's %s.
Xaymar added a commit that referenced this pull request Mar 28, 2023
The compiler will choose the optimal way automatically, and forcing std::move here actually results in two objects existing side by side, before being "moved" into one.
Xaymar added a commit that referenced this pull request Apr 5, 2023
The use of const references breaks '#include' for relative paths.
Xaymar added a commit that referenced this pull request Apr 5, 2023
It is not valid to pass std::string_view to snprintf's %s.
Xaymar added a commit that referenced this pull request Apr 5, 2023
The compiler will choose the optimal way automatically, and forcing std::move here actually results in two objects existing side by side, before being "moved" into one.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
testing-required Change requires additional testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants