473,396 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

a refinement of STL??

when I studing the "stl_algobase.h" (SGI Imp. of STL), I find the following
defination of swap:
template <class _Tp>
inline void swap (_Tp& __a, _Tp& __b){
_Tp __tmp = __a;
__a = __b;
__b = __tmp;
}
Couples of assignment operator, copy constructor and destructor is called
which seens affecting the efficiency greatly. imagine how many calls to
swap() in a sorting algorithm. so I started to do the refinement (not
exactly the code I used, just to show the concept):

template <class Tp>_allocate (size_t _size =1); //malloc
template <class Tp> _deallocate (Tp*);
template <class Tp> _copy (Tp *_dest, const Tp&_src); //memcpy

template <class _Tp>
inline void swap (_Tp& __a, _Tp& __b){
_Tp *_tmp = _allocate<_Tp> (); //To allocate space for temp object.
_copy (_tmp , __a);
_copy (&__a, __b);
_copy (&__b, *_tmp);
_deallocate (_tmp); //reverse of _allocate
}

No constructor nor operator= called any more. I have done a simple test with
container like vector, and it works just as I expected. what I worry about
is that,
would there be any safety problem, such as memory alignment?
Is it portable? would it be as fine if I use it on another platform.

One potential danger I can see is for a class, which store the pointer
"this" for its internal use. but what is the practical use to store "this"
statically? so I finally ignore this case.

anyway, could anybody give me a suggestion??


Jul 19 '05 #1
1 2309
"alex leung" <al**@hitogo.com> wrote in message news:<3f050099$1@shknews01>...
when I studing the "stl_algobase.h" (SGI Imp. of STL), I find the following
defination of swap:
template <class _Tp>
inline void swap (_Tp& __a, _Tp& __b){
_Tp __tmp = __a;
__a = __b;
__b = __tmp;
}
Couples of assignment operator, copy constructor and destructor is called
which seens affecting the efficiency greatly. imagine how many calls to
swap() in a sorting algorithm.
Watch it - this is the "base" version. For T's where a more efficient swap
can be done, a specialized version may exist. For UDT's these will not
even be part of the STL implementation delivered with your compiler,
but come with the UDT.
so I started to do the refinement (not
exactly the code I used, just to show the concept):

template <class Tp>_allocate (size_t _size =1); //malloc
template <class Tp> _deallocate (Tp*);
template <class Tp> _copy (Tp *_dest, const Tp&_src); //memcpy

template <class _Tp>
inline void swap (_Tp& __a, _Tp& __b){
_Tp *_tmp = _allocate<_Tp> (); //To allocate space for temp object.
_copy (_tmp , __a);
_copy (&__a, __b);
_copy (&__b, *_tmp);
_deallocate (_tmp); //reverse of _allocate
}
Undefined behavior if T is not a POD. Going to the heap is much
slower as simply defining char tmp[sizeof(T)];. This isn't portable
either, though.
No constructor nor operator= called any more. I have done a simple test with
container like vector, and it works just as I expected. what I worry about
is that, would there be any safety problem, such as memory alignment?
Is it portable? would it be as fine if I use it on another platform.
Nor portable, of course. It certainly won't work with many string
implementations that contain the small-string optimalization. Of course,
a STL implementaion isn't needed on recent compilers which have a proper
Standard Library in std::. On such implementations, you can't even
change it.
One potential danger I can see is for a class, which store the pointer
"this" for its internal use. but what is the practical use to store "this"
statically? so I finally ignore this case.
A common case is the small-string optimalization. For long strings, the
char* of a string will point to the heap, but for small strings the
char* points within the string object itself. This saves expensive
heap allocations, and improves locality of reference. In this case,
when you swap two strings the lengths will be swapped but the pointers
will still point to their original locations. This will be a disaster
if only one string is short enough to be located within the object,
else it will be just surprising. Sorting will obviously fail.
anyway, could anybody give me a suggestion??


Don't. On current std:: implementations, the compiler vendor can
use a non-portable hook to the compiler internals to look at each T.
It can then provide your swap implementation if that works, use the
char[sizeof(T)] trick if that makes sense, use a specialized swap
version or use the trivial version if and only if the other
approaches fail. That's the advantage of an integrated customized
Standard Library, it can use tricks internally you may not use.

For your own classes, provide your own
template< > std::swap( myUDT&, myUDT& );
specialization. It can of course do a C-style memcpy if myUDT is a
simple C-style struct.

Regards,
--
Michiel Salters
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: avinash | last post by:
We apologize if this is a duplicate email. EIGHTEENTH INTERNATIONAL CONFERENCE ON SYSTEMS ENGINEERING (ICSEng05) LAS VEGAS, USA, AUGUST 16-18, 2005 (http://www.icseng.info) This series of...
125
by: Raymond Hettinger | last post by:
I would like to get everyone's thoughts on two new dictionary methods: def count(self, value, qty=1): try: self += qty except KeyError: self = qty def appendlist(self, key, *values): try:
112
by: Andy | last post by:
Hi All! We are doing new development for SQL Server 2000 and also moving from SQL 7.0 to SQL Server 2000. What are cons and pros for using IDENTITY property as PK in SQL SERVER 2000? Please,...
1
by: hre1 | last post by:
hello stan, thank you very much for your fast replay! your solution will help to solve my problem. but i try to understand !why! sqc and xmlspy produce this messages: SQC means:TYPE...
2
by: John Hoge | last post by:
I'm working on a product search page that will allow the user to refine their search results based on a few criteria. The user will do a text search, which returns a set of products, and then be...
0
by: Richard Rosser | last post by:
Greetings. Not being used to Access, I have built a search form using the wizard but am not getting the desired results. Have tried both the 'and' and the 'or' operator but neither help. What I...
1
by: None | last post by:
Hi, I have developed webshop application using asp.net 1.1. I'm using DataGrid in one of the pages of my site. During the page load the DataGrid will be binded by around 7500 products(rows). At...
10
by: Alan Johnson | last post by:
24.1.1.3 says about InputIterators: Algorithms on input iterators should never attempt to pass through the same iterator twice. They should be single pass algorithms. In 24.3.4.4 summarizes the...
14
by: khaichiew85 | last post by:
i am trying to write a C code which request input from user and store them in array.Number of inputs is determined by the user and it is limited to 100 inputs.Then I need to pass the array to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.