Skip to content

Use emplace instead of insert/push in ObserverPool::add#242

Closed
frmdstryr wants to merge 2 commits intonucleic:mainfrom
frmdstryr:use-emplace-in-observer-pool-add
Closed

Use emplace instead of insert/push in ObserverPool::add#242
frmdstryr wants to merge 2 commits intonucleic:mainfrom
frmdstryr:use-emplace-in-observer-pool-add

Conversation

@frmdstryr
Copy link
Contributor

This avoids an extra xincref/xdecref on the cppy::ptr because insert & push_back do a copy.

See https://godbolt.org/z/zGvTzGahn

The *obs_free = Observer() does the same thing but I'm not sure how to cleany fix that (other than manually setting the fields).

@codecov
Copy link

codecov bot commented Feb 1, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.67%. Comparing base (0750392) to head (c666579).
Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #242   +/-   ##
=======================================
  Coverage   97.67%   97.67%           
=======================================
  Files          24       24           
  Lines        1074     1074           
  Branches      162      162           
=======================================
  Hits         1049     1049           
  Misses         12       12           
  Partials       13       13           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sccolbert
Copy link
Member

sccolbert commented Feb 2, 2025

For the last part, I think you can do:

obs_free->m_change_types = change_types;
obs_free->m_observer = observer;

Or you could add a method to the Observer struct to swap the fields for you. You could add a couple overloads to that method to also eliminate the other places where we manually twiddle the field.

Honestly, I can't remember why I made Observer a struct instead of class with more encapsulated behavior.

I think I would take the later approach if I were to do it again.

@frmdstryr frmdstryr closed this Jun 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants