473,789 Members | 2,729 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Fastest way to clone a hash_map

Hi,

I have the need to take a snapshot of a hash_map during execution
(actually transform it to a vector). This map is a shared resource
and therefore must be locked prior to any read/write operations thus I
need to minimize the amount of time the map resource is locked.

The map is defined as type <string, boost::shared_p tr<myobject>>. My
algorithm is as such:

void SnapShotToVecto r( vector< pair< string,
boost::shared_p tr<myobject& vec )
{

lockResource(th is->map);
vec.resize( map.size() );
copy(this->map.begin(), this->map.end(),list .begin());
unlockResource( this->map);
}

For about 3M elements w/in the map, I'm noticing that the resize op
takes about 150ms and the copy takes ~850ms. Is there any way to do
better? I suppose the total time doesn' t matter as it's the time the
resource is actually locked is the primary concern.

TIA
Jun 27 '08 #1
5 3910
devdude wrote:
For about 3M elements w/in the map, I'm noticing that the resize op
takes about 150ms and the copy takes ~850ms. Is there any way to do
better? I suppose the total time doesn' t matter as it's the time the
resource is actually locked is the primary concern.
You won't come around to copy the list of elements within the mutex
section. You might do the resize outside the lock, but you risk that a
second more expensive resize is required during the copy.

If you need shorter locks then you have to use a collection with an
iterator that is thread safe in a way that iteration over the mutable
collection gives defined behavior. Directory scans behave like that for
example. But the results are not reproduceable, of course.
Marcel
Jun 27 '08 #2
On 2008-04-29 17:33, devdude wrote:
Hi,

I have the need to take a snapshot of a hash_map during execution
(actually transform it to a vector). This map is a shared resource
and therefore must be locked prior to any read/write operations thus I
need to minimize the amount of time the map resource is locked.

The map is defined as type <string, boost::shared_p tr<myobject>>. My
algorithm is as such:

void SnapShotToVecto r( vector< pair< string,
boost::shared_p tr<myobject& vec )
{

lockResource(th is->map);
vec.resize( map.size() );
copy(this->map.begin(), this->map.end(),list .begin());
unlockResource( this->map);
}

For about 3M elements w/in the map, I'm noticing that the resize op
takes about 150ms and the copy takes ~850ms. Is there any way to do
better? I suppose the total time doesn' t matter as it's the time the
resource is actually locked is the primary concern.
You can start by moving the vec.resize() outside the lock (actually you
should probably only have to use a reserve() and not resize()). You can
probably also use a readers/writers lock to allow reads to the map while
copying it, which might be enough if you do not perform to many
modifications on it.

Depending on how you have implemented the hash-map you might be able to
add some kind of copy on write functionality, so that if an element is
added/ remove/changed in the map it will not affect the original map but
a copy, which shares the unmodified elements with the original map.

If the readers/writers lock is not enough you need to either make the
copying faster to do something smart with the map, either way you need
some information about the implementation of the map.

--
Erik Wikström
Jun 27 '08 #3
On Apr 29, 12:25 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2008-04-29 17:33, devdude wrote:
Hi,
I have the need to take a snapshot of a hash_map during execution
(actually transform it to a vector). This map is a shared resource
and therefore must be locked prior to any read/write operations thus I
need to minimize the amount of time the map resource is locked.
The map is defined as type <string, boost::shared_p tr<myobject>>. My
algorithm is as such:
void SnapShotToVecto r( vector< pair< string,
boost::shared_p tr<myobject& vec )
{
lockResource(th is->map);
vec.resize( map.size() );
copy(this->map.begin(), this->map.end(),list .begin());
unlockResource( this->map);
}
For about 3M elements w/in the map, I'm noticing that the resize op
takes about 150ms and the copy takes ~850ms. Is there any way to do
better? I suppose the total time doesn' t matter as it's the time the
resource is actually locked is the primary concern.

You can start by moving the vec.resize() outside the lock (actually you
should probably only have to use a reserve() and not resize()). You can
probably also use a readers/writers lock to allow reads to the map while
copying it, which might be enough if you do not perform to many
modifications on it.

Depending on how you have implemented the hash-map you might be able to
add some kind of copy on write functionality, so that if an element is
added/ remove/changed in the map it will not affect the original map but
a copy, which shares the unmodified elements with the original map.

If the readers/writers lock is not enough you need to either make the
copying faster to do something smart with the map, either way you need
some information about the implementation of the map.

--
Erik Wikström
I am actually using a reader/writer lock (are there standard
implementations of r/w locks avail by chance?) but the numbers are
from stress testing using all writes so it doesn't really matter.
hash_map is google's dense_map. moving vec.resize() outside the lock
gives me problems with the copy() -- assertion occurs in the bowels of
stl. Are there strategies to "chunk" the copy through multiple
iterations over the map?

Jun 27 '08 #4
On 2008-04-29 19:51, devdude wrote:
On Apr 29, 12:25 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
>On 2008-04-29 17:33, devdude wrote:
Hi,
I have the need to take a snapshot of a hash_map during execution
(actually transform it to a vector). This map is a shared resource
and therefore must be locked prior to any read/write operations thus I
need to minimize the amount of time the map resource is locked.
The map is defined as type <string, boost::shared_p tr<myobject>>. My
algorithm is as such:
void SnapShotToVecto r( vector< pair< string,
boost::shared_p tr<myobject& vec )
{
lockResource(th is->map);
vec.resize( map.size() );
copy(this->map.begin(), this->map.end(),list .begin());
unlockResource( this->map);
}
For about 3M elements w/in the map, I'm noticing that the resize op
takes about 150ms and the copy takes ~850ms. Is there any way to do
better? I suppose the total time doesn' t matter as it's the time the
resource is actually locked is the primary concern.

You can start by moving the vec.resize() outside the lock (actually you
should probably only have to use a reserve() and not resize()). You can
probably also use a readers/writers lock to allow reads to the map while
copying it, which might be enough if you do not perform to many
modification s on it.

Depending on how you have implemented the hash-map you might be able to
add some kind of copy on write functionality, so that if an element is
added/ remove/changed in the map it will not affect the original map but
a copy, which shares the unmodified elements with the original map.

If the readers/writers lock is not enough you need to either make the
copying faster to do something smart with the map, either way you need
some information about the implementation of the map.
Please do not quote signatures. And by the way, multi-posting is
generally not a good idea, if you have to post in multiple groups you
should cross-post (that way we will see what others have to say).
I am actually using a reader/writer lock (are there standard
implementations of r/w locks avail by chance?) but the numbers are
from stress testing using all writes so it doesn't really matter.
hash_map is google's dense_map. moving vec.resize() outside the lock
gives me problems with the copy() -- assertion occurs in the bowels of
stl.
Probably because the size of the map changes between the resize and the
copying and you iterate past the end of the vector. You should insert
the elements instead of assigning them, try using reserve()and an insert
iterator.

Notice that if the size of the map increases between the reserve and the
copying the vector might have to reallocate which will cost a lot, but
since reserve() is quite cheap you can probably do it inside the lock.
If that does not work you might try a deque, insertions at the end are
O(1), and not amortised O(1) like the vector, and you never have to
reallocate.
Are there strategies to "chunk" the copy through multiple
iterations over the map?
Do you mean to only copy parts of the map and then wait a bit and copy
the next part? You probably could but the results would be quite
meaningless since some elements will be added after you copy that part,
and others will be deleted.

Have you tried to create a copy of the map inside the lock, and copy to
the vector outside it?

--
Erik Wikström
Jun 27 '08 #5
On Apr 29, 2:42 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
Have you tried to create a copy of the map inside the lock, and copy to
the vector outside it?
If you mean:

hash_map<T1,T2n ewMap (oldMap);

Then I have tried this and the copy const. takes 2+ secs alone to
perform. Is there something faster?

Jun 27 '08 #6

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

Similar topics

6
5494
by: Jonathan | last post by:
I am hoping that someone more experienced than myself can point me towards what might be the fastest data lookup method to use for storing ip addresses. My situation is that I will need to maintain a list of perhaps 50 ip addresses to be used in a packet sniffing application. For each packet that goes through the application (which will be monitoring all traffic through a switch), I need to see if an entry for the source ip of that packet...
5
8625
by: peter_k | last post by:
Hi I've defined hash_map in my code using this: ------------------------------------------- #include <string> #include <hash_map.h> & namespace __gnu_cxx {
2
1748
by: Danny Ni | last post by:
Hi, I would like to know the fastest way to clone a dataset with filter inVB.Net or C#. Say I have a dataset that has one datatable having several data rows. I want to clone the structure to another dataset but with selection of datarows. Please Help!
6
6116
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or more minutes. 'REMOVE DUBLICATED VALUE FROM ARRAY +++++++++++++++++ Dim col As New Scripting.Dictionary Dim ii As Integer = 0
1
9402
by: jayesah | last post by:
Hi All, I am developing my code with Apache stdcxx. I am bound to use STL of Apache only. Now today I need hash_map in code but as I learned, it is not available in Apache since it is not standard c++. Though it is available with GNU STL. The code module where I use hash_map will generate separate object file during compilation. This code module is also using STL string.
2
4282
by: Amit Bhatia | last post by:
Hi, I am trying to use hash maps from STL on gcc 3.3 as follows: #ifndef NODE_H #define NODE_H #include <ext/hash_map> #include "node_hasher.h" class Node; typedef hash_map<pair<int,int>, Node, Node_HasherLoc_Tree;
4
3415
by: James Kanze | last post by:
On Jul 16, 10:53 pm, Mirco Wahab <wa...@chemie.uni-halle.dewrote: It depends. You might like to have a look at my "Hashing.hh" header (in the code at kanze.james.neuf.fr/code-en.html---the Hashing component is in the Basic section). Or for a discussion and some benchmarks, http://kanze.james.neuf.fr/code/Docs/html/Hashcode.html. (That article is a little out of date now, as I've tried quite a few more hashing algorithms. But the...
5
3374
by: frankw | last post by:
Hi, I have a hash_map with string as key and an object pointer as value. the object is like class{ public: float a; float b; ...
2
8146
by: marek.vondrak | last post by:
Hi, I am wondering if there are any functional differences between SGI's hash_map and tr1's unordered_map. Can these two containers be interchanged? What would it take to switch from hash_map to unordered_map? Thank you. -Marek
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10199
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9020
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.