-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
[WIP, RFC] Problem: lot of use of dynamic memory allocation, leading to performance degradation and non-determism #3911
base: master
Are you sure you want to change the base?
Changes from 57 commits
4f84758
ea0dc06
a24f2af
1bd2ae1
252e8d4
4a30795
577232e
ff8d79f
00e514e
18c52c4
a720a31
b9e1f01
1649701
0baafa4
59cbfac
f0a7a7f
f682600
3a3d877
b416348
cfd4c85
1dd2304
d06f868
cfa228b
d96d616
caf7798
5fbc4cc
d2c53c5
348865f
59c6a6c
1f5abc1
2c29abc
d7f9452
1450beb
f4973dc
3b9ec2f
a260668
7dafdf7
a3bfc67
cd90418
a575335
74dd371
e9c3a01
aaa10dd
77293ab
1309316
d666af8
73807f8
ffcede1
312f8c3
bf495f8
61870a4
c13f837
ea9c5dc
20f49ec
ba05e8f
afb858d
32d827d
84b4f8f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
This license file applies to everything in this repository except that which | ||
is explicitly annotated as being written by other authors, i.e. the Boost | ||
queue (included in the benchmarks for comparison), Intel's TBB library (ditto), | ||
the CDSChecker tool (used for verification), the Relacy model checker (ditto), | ||
and Jeff Preshing's semaphore implementation (used in the blocking queue) which | ||
has a zlib license (embedded in lightweightsempahore.h). | ||
|
||
--- | ||
|
||
Simplified BSD License: | ||
|
||
Copyright (c) 2013-2016, Cameron Desrochers. | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without modification, | ||
are permitted provided that the following conditions are met: | ||
|
||
- Redistributions of source code must retain the above copyright notice, this list of | ||
conditions and the following disclaimer. | ||
- Redistributions in binary form must reproduce the above copyright notice, this list of | ||
conditions and the following disclaimer in the documentation and/or other materials | ||
provided with the distribution. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY | ||
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | ||
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | ||
THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT | ||
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR | ||
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, | ||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
--- | ||
|
||
I have also chosen to dual-license under the Boost Software License as an alternative to | ||
the Simplified BSD license above: | ||
|
||
Boost Software License - Version 1.0 - August 17th, 2003 | ||
|
||
Permission is hereby granted, free of charge, to any person or organization | ||
obtaining a copy of the software and accompanying documentation covered by | ||
this license (the "Software") to use, reproduce, display, distribute, | ||
execute, and transmit the Software, and to prepare derivative works of the | ||
Software, and to permit third-parties to whom the Software is furnished to | ||
do so, all subject to the following: | ||
|
||
The copyright notices in the Software and this entire statement, including | ||
the above license grant, this restriction and the following disclaimer, | ||
must be included in all copies of the Software, in whole or in part, and | ||
all derivative works of the Software, unless such copies or derivative | ||
works are solely in the form of machine-executable object code generated by | ||
a source language processor. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT | ||
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE | ||
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, | ||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
DEALINGS IN THE SOFTWARE. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
https://github.com/cameron314/concurrentqueue/commit/38e6a6f0185a98c3aaf2a95aa109ba041221d527 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -699,6 +699,35 @@ ZMQ_EXPORT int zmq_ctx_get_ext (void *context_, | |
void *optval_, | ||
size_t *optvallen_); | ||
|
||
// ZMQ-provided message-pool implementations. */ | ||
// default allocator using malloc/free | ||
#define ZMQ_MSG_ALLOCATOR_DEFAULT 0 | ||
// using internally a SPSC queue (cannot be used with inproc maybe?) or perhaps an MPMC queue anyway | ||
#define ZMQ_MSG_ALLOCATOR_PER_THREAD_POOL 1 | ||
// using internally a MPMC queue, C++11 required | ||
#define ZMQ_MSG_ALLOCATOR_GLOBAL_POOL 2 | ||
|
||
ZMQ_EXPORT void *zmq_msg_allocator_new (int type_); | ||
ZMQ_EXPORT int zmq_msg_allocator_destroy (void **allocator_); | ||
ZMQ_EXPORT int | ||
zmq_msg_init_allocator (zmq_msg_t *msg_, size_t size_, void *allocator_); | ||
|
||
struct zmq_allocator_t | ||
{ | ||
// Allocate a chunk of memory of size len and return the pointer | ||
void*(*allocate_fn) (void *allocator, size_t len); | ||
|
||
// Deallocate the memory chunk pointed to by data_ | ||
void(*deallocate_fn) (void *allocator, void *data_); | ||
|
||
// Return true if this is an allocator and alive, otherwise false | ||
bool(*check_tag_fn) (void *allocator); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this required? In what circumstances will this be useful? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When the users wants to provide his/her own allocator instead of using one of the built-ins. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I mean is specifically the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would allow you to check if a void pointer is a pointer to what you think it is. It was in the previous PR so I left it in. Need to check if it is actually used somewhere. |
||
|
||
void(*destroy_fn)( void *allocator); | ||
|
||
void *allocator; | ||
}; | ||
|
||
/* DRAFT Socket methods. */ | ||
ZMQ_EXPORT int zmq_join (void *s, const char *group); | ||
ZMQ_EXPORT int zmq_leave (void *s, const char *group); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
Copyright (c) 2019-2020 Contributors as noted in the AUTHORS file | ||
|
||
This file is part of libzmq, the ZeroMQ core engine in C++. | ||
|
||
libzmq is free software; you can redistribute it and/or modify it under | ||
the terms of the GNU Lesser General Public License (LGPL) as published | ||
by the Free Software Foundation; either version 3 of the License, or | ||
(at your option) any later version. | ||
|
||
As a special exception, the Contributors give you permission to link | ||
this library with independent modules to produce an executable, | ||
regardless of the license terms of these independent modules, and to | ||
copy and distribute the resulting executable under terms of your choice, | ||
provided that you also meet, for each linked independent module, the | ||
terms and conditions of the license of that module. An independent | ||
module is a module which is not derived from or based on this library. | ||
If you modify this library, you must extend this exception to your | ||
version of the library. | ||
|
||
libzmq is distributed in the hope that it will be useful, but WITHOUT | ||
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public | ||
License for more details. | ||
|
||
You should have received a copy of the GNU Lesser General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "precompiled.hpp" | ||
#include "allocator_default.hpp" | ||
|
||
#include <new> | ||
|
||
zmq::allocator_default_t::allocator_default_t () | ||
{ | ||
_tag = 0xCAFEEBEB; | ||
} | ||
|
||
|
||
zmq::allocator_default_t::~allocator_default_t () | ||
{ | ||
// Mark this instance as dead | ||
_tag = 0xdeadbeef; | ||
} | ||
|
||
void *zmq::allocator_default_t::allocate (size_t len_) | ||
{ | ||
return operator new (len_, std::nothrow); | ||
} | ||
|
||
void zmq::allocator_default_t::deallocate (void *data_) | ||
{ | ||
operator delete (data_); | ||
} | ||
|
||
bool zmq::allocator_default_t::check_tag () const | ||
{ | ||
return _tag == 0xCAFEEBEB; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
Copyright (c) 2019-2020 Contributors as noted in the AUTHORS file | ||
|
||
This file is part of libzmq, the ZeroMQ core engine in C++. | ||
|
||
libzmq is free software; you can redistribute it and/or modify it under | ||
the terms of the GNU Lesser General Public License (LGPL) as published | ||
by the Free Software Foundation; either version 3 of the License, or | ||
(at your option) any later version. | ||
|
||
As a special exception, the Contributors give you permission to link | ||
this library with independent modules to produce an executable, | ||
regardless of the license terms of these independent modules, and to | ||
copy and distribute the resulting executable under terms of your choice, | ||
provided that you also meet, for each linked independent module, the | ||
terms and conditions of the license of that module. An independent | ||
module is a module which is not derived from or based on this library. | ||
If you modify this library, you must extend this exception to your | ||
version of the library. | ||
|
||
libzmq is distributed in the hope that it will be useful, but WITHOUT | ||
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public | ||
License for more details. | ||
|
||
You should have received a copy of the GNU Lesser General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef __ZMQ_I_ALLOCATOR_HPP_INCLUDED__ | ||
#define __ZMQ_I_ALLOCATOR_HPP_INCLUDED__ | ||
|
||
namespace zmq | ||
{ | ||
class allocator_default_t | ||
{ | ||
public: | ||
allocator_default_t (); | ||
|
||
~allocator_default_t (); | ||
|
||
static void *allocate_fn (void *allocator_, size_t len_) | ||
{ | ||
return static_cast<allocator_default_t *> (allocator_)->allocate (len_); | ||
} | ||
|
||
static void deallocate_fn (void *allocator_, void *data_) | ||
{ | ||
return static_cast<allocator_default_t *> (allocator_) | ||
->deallocate (data_); | ||
} | ||
|
||
static bool check_tag_fn (void *allocator_) | ||
{ | ||
return static_cast<allocator_default_t *> (allocator_)->check_tag (); | ||
} | ||
|
||
// allocate() typically gets called by the consumer thread: the user app thread(s) | ||
void *allocate (size_t len_); | ||
|
||
void deallocate (void *data_); | ||
|
||
bool check_tag () const; | ||
|
||
private: | ||
// Used to check whether the object is a socket. | ||
uint32_t _tag; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see |
||
}; | ||
} | ||
|
||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need to be exposed? If so, can the type not be used in the API, as in
ZMQ_EXPORT zmq_allocator_t *zmq_msg_allocator_new (int type_);
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I don't expose this one users cannot provide their own allocator with the current api. This is a design decision. I would allow users to provide their own allocators (maybe they want fixed bins with new, fixed bins with errors, an off the shelves allocator etc.). But of course that depends on how much you want to expose to the user. What do you think about this?