Use a raw function pointer instead of std::function objects in thread pool.
std::function can have an allocation cost when it's created, and also adds overhead to function calls. Since this API must be used from C, using a raw function pointer is good enough. PiperOrigin-RevId: 562760324 Change-Id: I2b714a13e07a2b71566d013f21f76c506e817a52
This commit is contained in:
committed by
Copybara-Service
parent
edbe9ce86f
commit
f976d6d228
@@ -1440,7 +1440,7 @@ struct mjrContext_ { // custom OpenGL context
|
|||||||
};
|
};
|
||||||
typedef struct mjrContext_ mjrContext;
|
typedef struct mjrContext_ mjrContext;
|
||||||
struct mjTask_ {
|
struct mjTask_ {
|
||||||
char buffer[48];
|
char buffer[24];
|
||||||
};
|
};
|
||||||
typedef struct mjTask_ mjTask;
|
typedef struct mjTask_ mjTask;
|
||||||
struct mjThreadPool_ {
|
struct mjThreadPool_ {
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ extern "C" {
|
|||||||
// These types are implemented in C++, they're just used as opaque pointers in C
|
// These types are implemented in C++, they're just used as opaque pointers in C
|
||||||
// to provide type safety for functions.
|
// to provide type safety for functions.
|
||||||
struct mjTask_ {
|
struct mjTask_ {
|
||||||
char buffer[48];
|
char buffer[24];
|
||||||
};
|
};
|
||||||
typedef struct mjTask_ mjTask;
|
typedef struct mjTask_ mjTask;
|
||||||
|
|
||||||
|
|||||||
@@ -6992,7 +6992,7 @@ STRUCTS: Mapping[str, StructDecl] = dict([
|
|||||||
name='buffer',
|
name='buffer',
|
||||||
type=ArrayType(
|
type=ArrayType(
|
||||||
inner_type=ValueType(name='char'),
|
inner_type=ValueType(name='char'),
|
||||||
extents=(48,),
|
extents=(24,),
|
||||||
),
|
),
|
||||||
doc='',
|
doc='',
|
||||||
),
|
),
|
||||||
|
|||||||
+3
-3
@@ -20,13 +20,13 @@
|
|||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <functional>
|
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
namespace mujoco {
|
namespace mujoco {
|
||||||
|
|
||||||
class Task {
|
class Task {
|
||||||
public:
|
public:
|
||||||
|
using FunctionPtr = void* (*)(void*);
|
||||||
enum Status {
|
enum Status {
|
||||||
QUEUED,
|
QUEUED,
|
||||||
COMPLETE,
|
COMPLETE,
|
||||||
@@ -34,7 +34,7 @@ class Task {
|
|||||||
|
|
||||||
static void Initialize(
|
static void Initialize(
|
||||||
Task* task,
|
Task* task,
|
||||||
std::function<void*(void*)> start_routine,
|
FunctionPtr start_routine,
|
||||||
void* args) {
|
void* args) {
|
||||||
// instantiate a task at the pointer passed in
|
// instantiate a task at the pointer passed in
|
||||||
new(task) Task();
|
new(task) Task();
|
||||||
@@ -55,7 +55,7 @@ class Task {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::function<void*(void*)> start_routine_;
|
FunctionPtr start_routine_;
|
||||||
|
|
||||||
void* args_;
|
void* args_;
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,6 @@
|
|||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <functional>
|
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
#include "thread/lockless_queue.h"
|
#include "thread/lockless_queue.h"
|
||||||
@@ -43,7 +42,7 @@ class ThreadPool {
|
|||||||
|
|
||||||
// start a task in the threadpool
|
// start a task in the threadpool
|
||||||
void Enqueue(
|
void Enqueue(
|
||||||
Task* task, std::function<void*(void*)> start_routine, void* args) {
|
Task* task, Task::FunctionPtr start_routine, void* args) {
|
||||||
Task::Initialize(task, start_routine, args);
|
Task::Initialize(task, start_routine, args);
|
||||||
lockless_queue_.push(static_cast<void*>(task));
|
lockless_queue_.push(static_cast<void*>(task));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2358,7 +2358,7 @@ public unsafe struct mjrContext_ {
|
|||||||
|
|
||||||
[StructLayout(LayoutKind.Sequential)]
|
[StructLayout(LayoutKind.Sequential)]
|
||||||
public unsafe struct mjTask_ {
|
public unsafe struct mjTask_ {
|
||||||
public fixed sbyte buffer[48];
|
public fixed sbyte buffer[24];
|
||||||
}
|
}
|
||||||
|
|
||||||
[StructLayout(LayoutKind.Sequential)]
|
[StructLayout(LayoutKind.Sequential)]
|
||||||
|
|||||||
Reference in New Issue
Block a user