<commit_before>#include <boost/smart_ptr.hpp>
#include <vector>
#include <iostream>
#include <Common/Timer.h>

//////////////////////////////////////////////////

struct ClassWithVector
{
  ClassWithVector() : refCount_(0), data_(1024) 
	{
	}
	~ClassWithVector()
	{
	}

	std::atomic<int> refCount_;
	
	std::vector<double> data_;
};

void intrusive_ptr_add_ref(ClassWithVector* p) 
{ 
	++(p->refCount_); 
}

void intrusive_ptr_release(ClassWithVector* p) 
{ 
	if ( ! --p->refCount_ ) delete p; 
}

//////////////////////////////////////////////////

struct ClassWithArray
{
  ClassWithArray() : refCount_(0)
	{
	}
	~ClassWithArray()
	{
	}

	std::atomic<int> refCount_;
	
	std::array<double,1024> data_;
};

void intrusive_ptr_add_ref(ClassWithArray* p) 
{ 
	++(p->refCount_); 
}

void intrusive_ptr_release(ClassWithArray* p) 
{ 
	if ( ! --p->refCount_ ) delete p; 
}

//////////////////////////////////////////////////
	
template <typename T>
void RunDefaultConstructableObjectTimings(size_t objectCount, const char* objectName)
{
	{
		// No timing, warm up the heap
		for ( int i=0; i<10; ++i )
		{
			std::vector<std::shared_ptr<T>> objects;
			std::generate_n(std::back_inserter(objects), objectCount, []{return std::shared_ptr<T>(new T());});
		}
	}
	
	{
		auto t = make_timer("std::shared_ptr and new : ", objectName, " ", objectCount);
		std::vector<std::shared_ptr<T>> objects;
		std::generate_n(std::back_inserter(objects), objectCount, []{return std::shared_ptr<T>(new T());});
	}
	
	std::cout << std::endl;

	{
		auto t = make_timer("std::shared_ptr and make_shared : ", objectName, " ", objectCount);
		std::vector<std::shared_ptr<T>> objects;
		std::generate_n(std::back_inserter(objects), objectCount, []{return std::make_shared<T>();});
	}
	
	std::cout << std::endl;
	
	{
		auto t = make_timer("std::unique_ptr and new : ", objectName, " ", objectCount);
		std::vector<std::unique_ptr<T>> objects;
		std::generate_n(std::back_inserter(objects), objectCount, []{return std::unique_ptr<T>(new T());});
	}

	std::cout << std::endl;
	
	{
		auto t = make_timer("boost::shared_ptr and new : ", objectName, " ", objectCount);
		std::vector<boost::shared_ptr<T>> objects;
		std::generate_n(std::back_inserter(objects), objectCount, []{return boost::shared_ptr<T>(new T());});
	}
	
	std::cout << std::endl;

	{
		auto t = make_timer("boost::shared_ptr and make_shared : ", objectName, " ", objectCount);
		std::vector<boost::shared_ptr<T>> objects;
		std::generate_n(std::back_inserter(objects), objectCount, []{return boost::make_shared<T>();});
	}
	
	std::cout << std::endl;
	
	{
		auto t = make_timer("boost::intrusive_ptr : ", objectName, " ", objectCount);
		std::vector<boost::intrusive_ptr<T>> objects;
		std::generate_n(std::back_inserter(objects), objectCount, []{return boost::intrusive_ptr<T>(new T());});
	}

	std::cout << std::endl;
}

//////////////////////////////////////////////////

int main(int argc, char* argv[])
{
  RunDefaultConstructableObjectTimings<ClassWithVector>(5e5, "ClassWithVector");
  RunDefaultConstructableObjectTimings<ClassWithArray>(1e6, "ClassWithArray");
}<commit_msg>Can handle more object types. refactored ref count<commit_after>#include <boost/smart_ptr.hpp>
#include <vector>
#include <iostream>
#include <Common/Timer.h>

//////////////////////////////////////////////////

template <typename T>
void intrusive_ptr_add_ref(T* p) 
{ 
	++(p->refCount_); 
}
       
template <typename T>
void intrusive_ptr_release(T* p) 
{ 
	if ( ! --p->refCount_ ) delete p; 
}

//////////////////////////////////////////////////

template<typename T>
class CRefCounted
{
	friend void intrusive_ptr_add_ref<T>(T* p);
  friend void intrusive_ptr_release<T>(T* p);
	std::atomic<int> refCount_{0};	
};

//////////////////////////////////////////////////

class ClassWithVector : public CRefCounted<ClassWithVector>
{
public:
  ClassWithVector() : data_(1024) {}

private:
	std::vector<double> data_;
};

//////////////////////////////////////////////////

struct ClassWithArray : public CRefCounted<ClassWithArray>
{
public:
  ClassWithArray() { std::fill(std::begin(data_), std::end(data_), 0); }

private:
	std::array<double,1024> data_;
};

//////////////////////////////////////////////////
	
template <typename T>
void RunDefaultConstructableObjectTimings(size_t objectCount, const char* objectName)
{
	std::cout << objectName << std::endl;

	{
		// No timing, warm up the heap
		for ( int i=0; i<1; ++i )
		{
			std::vector<std::shared_ptr<T>> objects;
			std::generate_n(std::back_inserter(objects), objectCount, []{return std::shared_ptr<T>(new T());});
		}
	}
	
	{
		auto t = make_timer("std::shared_ptr and new : ", objectName, " ", objectCount);
		std::vector<std::shared_ptr<T>> objects;
		std::generate_n(std::back_inserter(objects), objectCount, []{return std::shared_ptr<T>(new T());});
	}

	{
		auto t = make_timer("std::shared_ptr and make_shared : ", objectName, " ", objectCount);
		std::vector<std::shared_ptr<T>> objects;
		std::generate_n(std::back_inserter(objects), objectCount, []{return std::make_shared<T>();});
	}
	
	{
		auto t = make_timer("std::unique_ptr and new : ", objectName, " ", objectCount);
		std::vector<std::unique_ptr<T>> objects;
		std::generate_n(std::back_inserter(objects), objectCount, []{return std::unique_ptr<T>(new T());});
	}

	{
		auto t = make_timer("boost::shared_ptr and new : ", objectName, " ", objectCount);
		std::vector<boost::shared_ptr<T>> objects;
		std::generate_n(std::back_inserter(objects), objectCount, []{return boost::shared_ptr<T>(new T());});
	}
	
	{
		auto t = make_timer("boost::shared_ptr and make_shared : ", objectName, " ", objectCount);
		std::vector<boost::shared_ptr<T>> objects;
		std::generate_n(std::back_inserter(objects), objectCount, []{return boost::make_shared<T>();});
	}
	
	{
		auto t = make_timer("boost::intrusive_ptr : ", objectName, " ", objectCount);
		std::vector<boost::intrusive_ptr<T>> objects;
		std::generate_n(std::back_inserter(objects), objectCount, []{return boost::intrusive_ptr<T>(new T());});
	}

	std::cout << std::endl;
}

//////////////////////////////////////////////////

int main(int argc, char* argv[])
{
  RunDefaultConstructableObjectTimings<ClassWithVector>(1e5, "ClassWithVector");
  RunDefaultConstructableObjectTimings<ClassWithArray>(1e5, "ClassWithArray");
}
{'fraction_non_alphanumeric': 0.24501108647450112, 'fraction_numerical': 0.005226480836236934, 'mean_word_length': 3.7338830584707647, 'pattern_counts': {'":': 0, '<': 93, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2366107', 'n_tokens_mistral': 2231, 'n_tokens_neox': 2108, 'n_words': 366}