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

UnPackTo disable merge by default #7527

Merged
merged 4 commits into from Sep 14, 2022
Merged

Conversation

dbaileychess
Copy link
Collaborator

Fixes: #7458

Adds a new field bool _merge = false to UnPackTo that dictates if the receiving object's existing state is merged with the incoming state or not. When _merge == false, it will reset any pointer in the receiving object to nullptr if there is nothing to unpack. For vectors, the vector is cleared (resized to 0) and then shrunk to reclaim memory.

Previously, the merging of states was on by default, but I don't think that is the correct default value. See #7458 for background discussion. So this change makes merging off by default, and have to be explicitly opted in.

This is tested in two new tests that test with and without merging.

@github-actions github-actions bot added c++ codegen Involving generating code from schema labels Sep 13, 2022
@dbaileychess
Copy link
Collaborator Author

@RWauson FYI

Copy link
Collaborator

@aardappel aardappel left a comment

Choose a reason for hiding this comment

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

I think it is admirable that you are supporting both the "merge" and "reuse" use cases, but like I said in the linked issue, "reuse" was the intended use case from the start, and "merge" is a unintended niche use case.

Since supporting code complicates the code (generates more, and slower code), I personally would vote for only supporting reuse. But up to you.

aardappel
aardappel previously approved these changes Sep 13, 2022
@dbaileychess
Copy link
Collaborator Author

My only reason to include it as an option is because I am switching defaults (I don't know when it was originally switched away from what we wanted). So having the parameter gives people an easy way to go back to the old way.

@aardappel
Copy link
Collaborator

That old way was never intended though, the amount of people relying on that behavior should be minimal. I'd be fine not supporting that.

Copy link
Collaborator

@aardappel aardappel left a comment

Choose a reason for hiding this comment

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

do you feel the shrink_to_fit is worth it? likely if something is reused, it will have a similar amount of elements, and shrink_to_fit might entail alloc/copy/dealloc?

@dbaileychess
Copy link
Collaborator Author

Yeah, I can remove the shrink to fit part.

I also removed the merge parameter, so it is non-mergeable by default.

aardappel
aardappel previously approved these changes Sep 14, 2022
tests/cpp17/generated_cpp17/optional_scalars_generated.h Outdated Show resolved Hide resolved
@dbaileychess dbaileychess merged commit bc44fad into google:master Sep 14, 2022
@dbaileychess dbaileychess added the release-notes The PR should be highlighted in the release notes of the next release it is in. label Sep 14, 2022
copybara-service bot pushed a commit to tensorflow/tensorflow that referenced this pull request Oct 30, 2022
…e memory leak

The existing code relies on UnPackTo to merge the existing state with the new incoming state. However, this behavior was changed in google/flatbuffers#7527 and now leads to memory leak. Ignore the commit description there as it is outdated. During the review, they decided to not have the old behavior as an option.

PiperOrigin-RevId: 484798857
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ codegen Involving generating code from schema release-notes The PR should be highlighted in the release notes of the next release it is in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[C++] Possible memory stomping in Object API
2 participants