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

perf: change shared_ptrs from copy to reference #2863

Open
wants to merge 64 commits into
base: main
Choose a base branch
from
Open

Conversation

beats-dh
Copy link
Collaborator

@beats-dh beats-dh commented Aug 31, 2024

Description

This pull request changes multiple instances of std::shared_ptr from copies to constant references. This optimization aims to significantly reduce the overhead caused by unnecessary reference count increments and improve performance, especially in critical sections where shared_ptr copies were frequently being made.

These changes help in avoiding expensive operations related to reference count increments and decrements, thus providing a boost in performance, particularly in loops or commonly accessed functions.

Behaviour

Actual

Unnecessary copies of shared_ptr occur frequently, leading to increased reference count management overhead.

Expected

The code now uses constant references for shared_ptr where appropriate, reducing the number of unnecessary copies and resulting in improved performance.

Type of change

  • Performance improvement

How Has This Been Tested

  • Performance profiling

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I checked the PR checks reports
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works

@beats-dh beats-dh force-pushed the beats-fixs branch 3 times, most recently from b954f4f to 4d0a313 Compare September 2, 2024 04:57
@@ -135,7 +135,7 @@ void Dispatcher::executeScheduledEvents() {

if (task->execute() && task->isCycle()) {
task->updateTime();
threadScheduledTasks.emplace_back(task);
threadScheduledTasks.emplace(task);
Copy link
Contributor

Choose a reason for hiding this comment

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

rollback

if (!thread->tasks[serial].empty()) {
m_tasks[serial].insert(m_tasks[serial].end(), make_move_iterator(thread->tasks[serial].begin()), make_move_iterator(thread->tasks[serial].end()));
thread->tasks[serial].clear();
{
Copy link
Contributor

Choose a reason for hiding this comment

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

rollback

@@ -207,12 +207,11 @@ class Dispatcher {
}

std::array<std::vector<Task>, static_cast<uint8_t>(TaskGroup::Last)> tasks;
std::vector<std::shared_ptr<Task>> scheduledTasks;
phmap::parallel_flat_hash_set_m<std::shared_ptr<Task>> scheduledTasks;
Copy link
Contributor

Choose a reason for hiding this comment

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

rollback

@beats-dh beats-dh force-pushed the beats-fixs branch 2 times, most recently from ef5cdf0 to d3718ac Compare September 12, 2024 00:00
src/utils/lockfree.hpp Outdated Show resolved Hide resolved
src/utils/lockfree.hpp Outdated Show resolved Hide resolved
src/utils/lockfree.hpp Outdated Show resolved Hide resolved
src/utils/lockfree.hpp Outdated Show resolved Hide resolved
@beats-dh
Copy link
Collaborator Author

beats-dh commented Sep 13, 2024

}
return static_cast<T*>(p);
return static_cast<T*>(::operator new(n * sizeof(T), static_cast<std::align_val_t>(alignof(T))));

Choose a reason for hiding this comment

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

Why not array-new? new[n] T(…)

@@ -8,114 +8,62 @@
*/

#pragma once
#include <atomic_queue/atomic_queue.h>

Choose a reason for hiding this comment

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

Do we need this? It introduces another layer of complexity that I have a hard time seeing a need for, given how this is currently used.

The per-thread caches are a great idea on their own even without this layer. Is there a reason for the buffers to be recycled across threads?

@dudantas dudantas force-pushed the beats-fixs branch 2 times, most recently from e176c09 to 6bc24bf Compare October 17, 2024 19:18
Copy link

Copy link
Contributor

This PR is stale because it has been open 45 days with no activity.

@github-actions github-actions bot added the Stale No activity label Nov 30, 2024
@github-actions github-actions bot removed the Stale No activity label Dec 8, 2024
Copy link
Contributor

github-actions bot commented Jan 8, 2025

This PR is stale because it has been open 45 days with no activity.

@github-actions github-actions bot added the Stale No activity label Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Stale No activity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants