Fuck, as a note, I must use my usual words.
Preface: this series of articles is only for study notes, and crazysprite is a fuck. The development goal of the engine is to develop cross-platform games. Therefore, learning is the most important thing. There must be a lot of people who scold me for being stupid. Why do you need to write the basic library yourself... Fuck, I like it.
First, we started with vector, hashmap, linklist, and others. I am not that cheap. I have to rewrite a set of STL.
First, csstructarray is csarray <t, false, false, 8>.
This is the STL of MS, and stlport is much faster than this, but it is mainly on hashmap and list. But not several times faster.
Fuck is the first implementation of vector. The implementations of various STL basically increase exponentially at the level of 2. Imagine that the length of the array has increased from 1 to 1024, and the memory needs to be re-allocated many times... However, if tens of thousands of workers are used, for example, 65535 yuan is allocated at a time... I don't know how much it will waste... Finally, I chose capacity + (capacity * 3)> 3 + 32, calling, of course not what I got. Second, the creation and allocation of STL objects cannot be avoided, and allocator cannot be adjusted. Even if allocator is rewritten, it will inevitably keep running the loop, but the overhead is much smaller, but I hate writing this stuff. How nice is built-in support. So my array is defined in this way. template <typename T, bool needconstruct = true, bool needdestruct = true, s32 alignment = csalignment_default> class csarray, well, relies on these two parameters, suspected of being rogue. In fact, you can also use typeinfo to get these two pieces of information, but it is associated with the specific rtti implementation, fuck, forget it. What Should vector do soon? There are two points: first, using placement new to create an object is much faster than each allocation separately. Second, when the array length is insufficient, when the memory is re-allocated or the object is moved, needconstruct and needdestruct are both false and happy.
Fuck, then let's talk about hashmap. the STL Implementation of MS is a tragedy. stlport is better, and searching is almost 20% slower than my implementation. Even if I set the factor to 2 faster, faster than me. Of course, the hash function of hashmap is the most important to others. The main reason for the high speed of hashmap is that csarray is used internally to show everyone the data structure. I won't even talk about it any more. Nothing can be done:
Template <typename K, typename v>
Class cshashmap
{
Struct pair
{
Csinline pair (){}
Csinline pair (const K & K, const V & V): m_key (K), m_value (v ){}
K m_key;
V m_value;
S32 m_hashnext;
};
Protected:
S32 m_hashcount;
S32 * m_hash;
Csarray <pair> m_pairs;
};
//------------------------------------------------------------------------
Template <typename K, typename v> void
Cshashmap <K, V >:: _ rehash ()
{
Csdelete [] m_hash;
M_hash = csnew s32 [m_hashcount];
For (s32 I = 0; I <m_hashcount; I ++)
{
M_hash [I] = cshash_index_none;
}
For (s32 I = 0; I <m_pairs.size (); I ++)
{
Pair & P = m_pairs [I];
S32 ihash = (gettypehash (P. m_key) & (m_hashCount-1 ));
P. m_hashnext = m_hash [ihash];
M_hash [ihash] = I;
}
}
//------------------------------------------------------------------------
Template <typename K, typename v> csinline void
Cshashmap <K, V >:: _ relax ()
{
While (m_hashcount> m_pairs.size () * cshash_rehash_factor + 8)
{
M_hashcount/= 2;
}
_ Rehash ();
}
Basically, these are the core ones.
Fuck: Finally, let's talk about list. This is the most time I use. Although the implementation of the first version is faster than that of MS, stlport insertion is twice faster than that of me! I can't bear it at all... Hold off for 1 or 2 days... After reading the implementation of stlport list and malloc, he finally found that his core competitiveness is still placement new, but there are two points that limit his speed. First, only when the object is smaller than bytes can he use placement new. Second, only 20 objects are allocated at a time. Fuck, I continue to play rogue games. First, I implemented different cslinklist, which has no bright spots. It is quite reasonable. Second, to speed, I used csfastlist, which is like this:
Template <typename T, s32 poolgrowstep = 20>
Class csfastlist
{
Protected:
Node * m_head;
S32 m_size;
// Use to pre malloc memory
Csarray <node *, false, false> m_freenodes;
Csarray <memoryblock, false, false> m_memoryblocks;
};
The speed is faster than the next two csarrays. The reason for the speed is that they hold pre-allocated memory blocks. Of course, the amount of memory allocated at a time can be controlled. The first allocation of the number of initialization functions is csfastlist:: csfastlist (s32 capacity). If it is enough for two, 65535 is allocated at a time... Then, in trim, the memory wasted by m_freenodes is recycled... Insert 65535 nodes at a speed of 5-6 times of the stlport... STL dozens of times... Of course, if we initialize 20 and allocate 20 in the same way as stlport, it will be faster than stlport.
Fuck, the next step is file writing management...