• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

shllow copy in FastHashMap

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


this map designed to operate in a multithreaded environment where the large majority of method calls are read-only, Read calls are non-synchronized and write calls perform the following steps:
1.Clone the existing collection
2.Perform the modification on the clone
3.Replace the existing collection with the (modified) clone

my question is : why it uses shallow copy here? Can someone show me the mechanism of the FastHashMap? thanx.
 
blacksmith
Posts: 1332
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you want it to do?

My comment on observing it is: it clones the entire map every time an element is added? That doesn't seem very "fast" to me.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree. Insertion operations are incredibly fast -- mainly because the operation is relatively simple. You need a huge amount of read over writes to justify cloning, and even then, the hashmap can't get very large.

On a side note, if you are using Java 5.0, take a look at the ConcurrentHashMap. It allows parallel reads, and sometimes, parallel writes. It works because it uses optimistic locking, and segmented data.

Henry
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"DeltaTang", please take a moment to read our display name policy and then edit your display name to have a first and last name, separated by a space. Thanks.

As for the question - note that FastHashMap is not a class "Delta" is writing; rather it comes from Jakarta Commons BeanUtils. As far as I know most (all?) of the Jakarta Commons stuff is written for pre-5.0 code. I agree ConcurrentHashMap is a better choice, but BeanUtils assumes you may not have access to it. (Though in this case one could probably use Doug Lea's earlier ConcurrentHashMap.)

[Warren]: My comment on observing it is: it clones the entire map every time an element is added? That doesn't seem very "fast" to me.

It isn't, in general, but it does make read operations a little faster because they're now unsynchronized. So if reads are much, much more common than writes, this may be faster than a synchronized HashMap or Hashtable. I expect that it's fairly rare to find a situation where "FastHashMap" is actually faster, and even rarer that it's significantly faster. But it's possible, at least in theory.

[Delta]: my question is : why it uses shallow copy here?

Well, a shallow copy is generally (always?) faster than a deep copy, and I don't see any reason why we'd need a deep copy here. In fact using a deep copy would break the contract of the Map - when you get() an object from it, ythe object would not == the original object you put into the map. The API says get() returns the value put into the Map; it doesn't return a copy of the value.

[Delta]: Can someone show me the mechanism of the FastHashMap? thanx.

You're already looking at the source code, so I guess I don't understand the question.
[ May 21, 2005: Message edited by: Jim Yingst ]
 
aleyx chow
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much for the great replies.
Sorry for my ambigous question and description for some details.
Yes, the FastHashMap comes from Jakarta.(it wasnt denoted in my first posted topic :-) Oh...I shoud say sorry again)
I think what i really wanna know is why invoking clone method is used in the synchronized block, regardless of whether it's shallow or deep?Is it an integrant part there?
Can u give me a clue?thanx
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by aleyx chow:
I think what i really wanna know is why invoking clone method is used in the synchronized block, regardless of whether it's shallow or deep?Is it an integrant part there?
Can u give me a clue?thanx



Well, that is the algorithm... It speeds up reads by not synchronizing them. In order to do that, it operates on a copy of the map for writes, and then switch to the copy. (assuming the variable is declared volatile, as the code is not listed) The clone() method is used to make the copy.

Henry
 
What I don't understand is how they changed the earth's orbit to fit the metric calendar. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic