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

Make Obj trivial and add a separate ObjCollectionParent type #7402

Merged
merged 6 commits into from Mar 13, 2024

Conversation

tgoyne
Copy link
Member

@tgoyne tgoyne commented Mar 1, 2024

Adding a base class and virtual functions to Obj turned out to have a measurable performance impact due to that it inhibits a lot of optimizations that are possible for trivial types (10-15% in the obj-c read benchmarks, which includes some SDK overhead so the slowdown in the core part is bigger). Moving the implementation of CollectionParent to a separate adaptor type fixes this, but requires some code movement.

I found the logic around when collections need to update difficult to follow and so attempted to simplify it. update_if_needed() did meaningfully different things on collections and Obj, with Obj::update_if_needed_with_status() being equivalent to Collection::update_if_needed(). I renamed these to make Obj match collection and use checked_update_if_needed() for the version that throws when detached. A lot of the updating logic was duplicated between the various collections, so I consolidated the parts which logically should always be identical.

All of the tests passed with clearly incorrect handling of m_parent_version (such as never setting it at all on ObjCollectionParent), which made it hard to tell what it was even needed for. I shifted it to only ever being incremented in Obj and then propagated downwards, which makes it harder to mask bugs. Making it uint32_t cuts the size of Obj by 8 bytes on 64-bit platforms since there's otherwise 7 bytes of trailing padding; it's unclear if this is actually useful.

@tgoyne tgoyne self-assigned this Mar 1, 2024
@cla-bot cla-bot bot added the cla: yes label Mar 1, 2024
Base automatically changed from tg/replication-logging to master March 1, 2024 12:26
Copy link

coveralls-official bot commented Mar 5, 2024

Pull Request Test Coverage Report for Build thomas.goyne_232

Details

  • 496 of 559 (88.73%) changed or added relevant lines in 21 files are covered.
  • 848 unchanged lines in 34 files lost coverage.
  • Overall coverage increased (+0.9%) to 91.787%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/realm/collection.hpp 18 19 94.74%
src/realm/dictionary.cpp 13 14 92.86%
src/realm/obj.cpp 99 100 99.0%
src/realm/object-store/impl/results_notifier.cpp 9 10 90.0%
src/realm/collection_parent.cpp 49 52 94.23%
src/realm/obj.hpp 41 47 87.23%
test/test_list.cpp 152 166 91.57%
src/realm/object-store/impl/collection_notifier.cpp 29 65 44.62%
Files with Coverage Reduction New Missed Lines %
src/realm/collection.hpp 1 87.28%
src/realm/list.cpp 1 89.17%
src/realm/list.hpp 1 85.13%
src/realm/obj.hpp 1 90.1%
src/realm/sync/network/network.cpp 1 89.86%
src/realm/table_ref.hpp 1 55.81%
src/realm/set.hpp 2 78.28%
src/realm/sync/network/http.hpp 2 82.41%
test/test_lang_bind_helper.cpp 2 94.24%
src/realm/obj.cpp 3 91.21%
Totals Coverage Status
Change from base Build 2115: 0.9%
Covered Lines: 242761
Relevant Lines: 264484

💛 - Coveralls

@tgoyne tgoyne force-pushed the tg/obj-perf branch 3 times, most recently from bd61f4b to be73d09 Compare March 5, 2024 23:56
@tgoyne tgoyne marked this pull request as ready for review March 6, 2024 02:04
@tgoyne tgoyne requested a review from jedelbo March 6, 2024 02:04
@tgoyne tgoyne force-pushed the tg/obj-perf branch 2 times, most recently from eabaac3 to d05739a Compare March 8, 2024 17:07
@jedelbo
Copy link
Contributor

jedelbo commented Mar 11, 2024

This PR contains 4 different commits, where only the last one is about making Obj trivial, so perhaps this should have been handled in 4 different commits. I will try to address each commit individually:

Less data:
Looks good.

Data race:
Looks good.

Consolidate ...:
You are right that this area has been quite confusing - for me at least since #4057. You are right that it is only neccesary to increment the parent_version in Obj. I think a central case to test is when 2 collection accesors share a common parent collection. I have made an update to the test in #7446. Feel free to merge and/or modify.
Otherwise fine.

ObjCollectionParent:
Looks good

Copy link
Contributor

@jedelbo jedelbo left a comment

Choose a reason for hiding this comment

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

Seems like the only reservation I have left is if the test should be updated as indicated in #7446

We only need the very next path element, not the entire remaining path.
…cated code

m_run_time_point was read on the target thread while being written on the
worker thread without any locking involved.
Giving Obj a virtual base class turns out to have a meaningful performance
impact.
@tgoyne
Copy link
Member Author

tgoyne commented Mar 12, 2024

I've expanded the tests to include the scenario tested in #7446 plus a few other things. This ended up revealing a pre-existing bug (accessors for collections with depth > 1 are considered not up to date after mutating via that accessor, and so update every time) that I'll fix in a subsequent PR.

@tgoyne tgoyne force-pushed the tg/obj-perf branch 2 times, most recently from b736962 to 728ba67 Compare March 13, 2024 01:52
@tgoyne tgoyne added the no-jira-ticket Skip checking the PR title for Jira reference label Mar 13, 2024
@tgoyne tgoyne merged commit 687bb98 into master Mar 13, 2024
38 of 41 checks passed
@tgoyne tgoyne deleted the tg/obj-perf branch March 13, 2024 04:51
@tgoyne tgoyne mentioned this pull request Mar 13, 2024
4 tasks
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes no-jira-ticket Skip checking the PR title for Jira reference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants