8 Replies - 493 Views - Last Post: 22 March 2021 - 10:00 AM

#1 LessComplexity   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 21-March 21

A smart & efficient open-source memory pool

Posted 21 March 2021 - 02:31 PM

Hi guys,

I love open-source so I decided to start a start-up with some good friends and along the way to release all of our tools as open-source for everyone to use and enjoy.

A great thing I achieved lately is to create a very fast and efficient memory pool in C++ which was developed with according to data-oriented design principles. It can help you make much faster memory allocations, deallocations and reallocations (Up to 20 times faster than new/delete depending on operating system & compiler).

Here's the source code & readme: Github Link

I hope that it will help you with your project, and would love to get your help/suggestions and more to make it better for everyone :)/>

I plan to release many more tools under the "CPPShift" namespace for C++ that are helpful for big and performance based software, so stay tuned and happy programming ;)/>

Is This A Good Question/Topic? 0
  • +

Replies To: A smart & efficient open-source memory pool

#2 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7733
  • View blog
  • Posts: 25,850
  • Joined: 05-May 12

Re: A smart & efficient open-source memory pool

Posted 21 March 2021 - 03:21 PM

Moving to Share Your Project...
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7733
  • View blog
  • Posts: 25,850
  • Joined: 05-May 12

Re: A smart & efficient open-source memory pool

Posted 21 March 2021 - 07:21 PM

The following seems to crash:
#include <iostream>
#include "MemoryPool.h"

int main() {

    const char* str = "Hello ";
    int length = strlen(str);
    const char* add = "World";
    int add_length = strlen(add);

    CPPShift::Memory::MemoryPool* mp = CPPShift::Memory::MemoryPoolManager::create();
    for (int i = 0; i < 1000000; i++) {
        // Alloc
        char * start = new (mp) char[length];

        // Realloc
        char* old = start;
        start = new (mp) char[length + add_length];
        CPPShift::Memory::MemoryPoolManager::free(old);

        // Free
        CPPShift::Memory::MemoryPoolManager::free(start);
    }

    return 0;
}



I came up with the code above as a minimal repro because your BenchMark test is not a fair benchmark. It's not doing an apples-to-apple comparison of what the C++ runtime libary does when it allocates a string and then reallocates it to be longer, when compared to our String implementation where you are calling reallocate with a high probability of not having to move the data to a new block. As I started converting your String implementation to imitate the actual allocate a new block and copy the data to a new block, your library started crashing.

Also as an aside, your String implementation is failing to copy the null terminator at the end of the string.
Was This Post Helpful? 2
  • +
  • -

#4 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 1058
  • View blog
  • Posts: 4,055
  • Joined: 13-June 14

Re: A smart & efficient open-source memory pool

Posted 21 March 2021 - 11:18 PM

No unit tests? If you're releasing software that you want people to use, how are you going to do the following things without tests?

- Demonstrate that it does what it's supposed to
- Make changes without breaking things that are working
Was This Post Helpful? 2
  • +
  • -

#5 LessComplexity   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 21-March 21

Re: A smart & efficient open-source memory pool

Posted 22 March 2021 - 09:05 AM

View PostSkydiver, on 21 March 2021 - 07:21 PM, said:

The following seems to crash:

:

I came up with the code above as a minimal repro because your BenchMark test is not a fair benchmark.


Hi, I resolved the issues that you pointed out and thank you that kind enough to open an issue on the repository page. Appreciate it :)/>

Now I also added another string implementation that uses new/delete and compared it against the current implementation with the memory pool.

If you have anything more to add or comment, I would always welcome it.

View Postndc85430, on 21 March 2021 - 11:18 PM, said:

No unit tests? If you're releasing software that you want people to use, how are you going to do the following things without tests?


I just separated the test code from the library into the 'test' folder, later today I will add unit tests for each feature of the memory pool :)/>

This post has been edited by Skydiver: 22 March 2021 - 09:25 AM
Reason for edit:: Trimmed quotes.

Was This Post Helpful? 1
  • +
  • -

#6 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7733
  • View blog
  • Posts: 25,850
  • Joined: 05-May 12

Re: A smart & efficient open-source memory pool

Posted 22 March 2021 - 09:19 AM

View PostLessComplexity, on 22 March 2021 - 12:05 PM, said:

Now I also added another string implementation that uses new/delete and compared it against the current implementation with the memory pool.

Unfortunately, your new implementation does a big no-no: mixing the C++ new and delete with the C malloc(), realloc(), and free().

From [email protected]
	STDString& STDString::operator+=(const char* str)
	{
		int add_length = strlen(str);
		char* str_holder = new char[this->length + add_length];
		memcpy(str_holder, this->start, this->length);
		memcpy(str_holder + this->length, str, add_length);
		delete this->start;
		this->start = str_holder;
		this->length += add_length;
		return *this;
	}

	STDString& STDString::operator+=(const STDString& str)
	{
		char* prev = this->start;
		this->start = (char*) realloc(this->start, this->length + str.size());
		if (start == NULL) {
			this->start = prev;
			return *this;
		}
		memcpy(this->start + this->length, str.data(), str.size());
		this->length += str.size();
		return *this;
	}


Was This Post Helpful? 1
  • +
  • -

#7 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7733
  • View blog
  • Posts: 25,850
  • Joined: 05-May 12

Re: A smart & efficient open-source memory pool

Posted 22 March 2021 - 09:27 AM

BTW! Thanks for updating the benchmarks. It was very good to see that your code runs faster than the runtimes!

As a quick aside, you should be doing benchmarks against the Release or retail builds, not the Debug builds.
Was This Post Helpful? 1
  • +
  • -

#8 LessComplexity   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 21-March 21

Re: A smart & efficient open-source memory pool

Posted 22 March 2021 - 09:38 AM

View PostSkydiver, on 22 March 2021 - 09:19 AM, said:

Unfortunately, your new implementation does a big no-no: mixing the C++ new and delete with the C malloc(), realloc(), and free().


Oh, good that you noticed this!

View PostSkydiver, on 22 March 2021 - 09:27 AM, said:

As a quick aside, you should be doing benchmarks against the Release or retail builds, not the Debug builds.


The test are in release mode (You can see the release configuration in the CMakeLists.txt file in the 'test' directory). Glad you liked it :)/>

This post has been edited by Skydiver: 22 March 2021 - 10:48 AM
Reason for edit:: Trimmed quotes.

Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7733
  • View blog
  • Posts: 25,850
  • Joined: 05-May 12

Re: A smart & efficient open-source memory pool

Posted 22 March 2021 - 10:00 AM

There is no need to fully quote the post above yours. Just trim down to a specific that you want to address.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1