Better forwarding to improve read performance a little#484
Conversation
There was a problem hiding this comment.
Summary of Changes
Hello @GregTheMadMonk, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request introduces targeted optimizations to the rfl::json library, primarily focusing on improving read performance by leveraging C++11 move semantics more effectively. The changes aim to reduce unnecessary data copying, particularly within the rfl::json::readrfl::Generic path, leading to notable speedups in release builds. Additionally, the PR enhances error handling efficiency and optimizes memory allocation in parsing routines.
Highlights
- Performance Enhancement through Move Semantics: Optimized data flow by consistently applying std::move and std::forward in parsing and error handling logic, significantly reducing object copying and improving read operation performance, as demonstrated by benchmarks.
- Improved Error Object Handling: The rfl::Error class now supports move construction and assignment, allowing for more efficient propagation and storage of error messages without redundant string copies.
- Optimized Variant Parsing Memory: Pre-allocation of memory for error vectors in rfl::Variant and std::variant parsing paths using errors.reserve helps mitigate reallocations and improve performance when multiple parsing errors occur.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Code Review
This pull request introduces several excellent performance improvements by leveraging move semantics and perfect forwarding more effectively across the parsing logic. The changes are well-implemented and the benchmark results clearly demonstrate significant speedups, especially for rfl::json::read<rfl::Generic>. I have found one potential issue regarding exception safety in a noexcept function that could lead to program termination under certain conditions. Please see the detailed comment for a suggested fix.
|
@GregTheMadMonk that sounds great, but some of the tests appear to be failing. Could you take a look? |
|
@liuzicheng1987 if you're talking about the compilation errors ( The "offending" piece of code in static std::optional<Error> read_object_or_array_with_default(
const R& _r, const InputObjectOrArrayType& _obj_or_arr,
NamedTupleType* _view) noexcept {
std::vector<Error> errors;
const auto reader = ViewReaderWithDefaultType(&_r, _view, &errors);
std::optional<Error> err; // <-- declaration here
if constexpr (_no_field_names) {
err = _r.read_array(reader, _obj_or_arr);
} else {
err = _r.read_object(reader, _obj_or_arr);
}
if (err) {
return err; // <-- -Werror=maybe-uninitialized - why?!
}
if (errors.size() != 0) {
return to_single_error_message(errors);
}
return std::nullopt;
}
};(and a similar function above it) appears safe and straightforward to me... I tried to reproduce the error in a similar scenario and didn't succeed: https://godbolt.org/z/4scKPvnnM (this also didn't produce the same My only two ideas would be either a) GCC gets confused and it's a misfire of this warning or b) there is something in the particular implementations of |
|
Ok. Could you try to just set err = std::nullopt when you first declare it? |
|
@GregTheMadMonk , this should fix the issue. We'll wait for the tests to run through and then this is ready to be merged. Thank you for the contribution! |
|
Thank you! How did you come to this fix? Or was that the warning system in GCC has actually misfired and you had to do it by trial and error? |
|
@GregTheMadMonk I had figured that the problem only occurs where we have a mutable optional variable, moreover I recall having seen something like this before. So that's how I figured it out. |

Hello!
This isn't really a significant change, but it helps speed up
rfl::json::read<rfl::Generic>almost twice in the release build (at least in the benchmarked case).Sorry for not using your benchmark suite here, I had problems getting the
vcpkgdependencies properly compiled with GCC15 that ships with my system.I also tried to look into improving the debug build performance of
rfl::to_generic, but couldn't find anything to improve there, at least not as simple.Benchmark results (on my PC, Ryzen 3600)
In release mode
Before:
After:
In debug mode
Before:
After:
Benchmark code