Hi all again.. Another newbie question for you all.. If I've got a dictionary
that has N elements in it, I figure there are two ways to empty it out : 1) use the #remove method (or perhaps #removeKey since it's a dictionary). 2) Just replace the current Dictionary with a new one and let the old (now dangling one) get garbage collected.. Is #2 a viable option to clear that thing out or is that naughty from a ST purist' point of view? I'm guessing that #2 is fine, but that you probably wouldn't want to do it too frequently.. Comments? Thanks! |
I'd stay away from #2, just do,
dict keys do: [:ea | dict removeKey: ea] Cheers! -Boris -- +1.604.689.0322 DeepCove Labs Ltd. 4th floor 595 Howe Street Vancouver, Canada V6C 2T5 [hidden email] CONFIDENTIALITY NOTICE This email is intended only for the persons named in the message header. Unless otherwise indicated, it contains information that is private and confidential. If you have received it in error, please notify the sender and delete the entire message including any attachments. Thank you. -----Original Message----- From: Rick F. [mailto:[hidden email]] Sent: Tuesday, July 25, 2006 2:07 PM To: [hidden email] Subject: Proper way to empty a Dictionary.. Hi all again.. Another newbie question for you all.. If I've got a dictionary that has N elements in it, I figure there are two ways to empty it out : 1) use the #remove method (or perhaps #removeKey since it's a dictionary). 2) Just replace the current Dictionary with a new one and let the old (now dangling one) get garbage collected.. Is #2 a viable option to clear that thing out or is that naughty from a ST purist' point of view? I'm guessing that #2 is fine, but that you probably wouldn't want to do it too frequently.. Comments? Thanks! smime.p7s (4K) Download Attachment |
Really? Given the simplicity of the problem statement, my preference
would be #2 for simplicity and speed: myDictionary := Dictionary new What's wrong with letting it be garbage collected - what potential consequences are you worried about? About the only reason that might inspire caution is if I had previously set some dependencies on that dictionary, but given that Dictionary is not the standard model for most widgets, I don't see that happening too often. Dave Boris Popov wrote: > I'd stay away from #2, just do, > > dict keys do: [:ea | dict removeKey: ea] > > Cheers! > > -Boris > |
My interpretation of #2 seems to be different from yours then, I thought
Rick wanted to do, dict become: Dictionary new No? -Boris -- +1.604.689.0322 DeepCove Labs Ltd. 4th floor 595 Howe Street Vancouver, Canada V6C 2T5 [hidden email] CONFIDENTIALITY NOTICE This email is intended only for the persons named in the message header. Unless otherwise indicated, it contains information that is private and confidential. If you have received it in error, please notify the sender and delete the entire message including any attachments. Thank you. -----Original Message----- From: Dave Stevenson [mailto:[hidden email]] Sent: Tuesday, July 25, 2006 2:33 PM To: Boris Popov Cc: Rick F.; [hidden email] Subject: Re: Proper way to empty a Dictionary.. Really? Given the simplicity of the problem statement, my preference would be #2 for simplicity and speed: myDictionary := Dictionary new What's wrong with letting it be garbage collected - what potential consequences are you worried about? About the only reason that might inspire caution is if I had previously set some dependencies on that dictionary, but given that Dictionary is not the standard model for most widgets, I don't see that happening too often. Dave Boris Popov wrote: > I'd stay away from #2, just do, > > dict keys do: [:ea | dict removeKey: ea] > > Cheers! > > -Boris > smime.p7s (4K) Download Attachment |
* Boris Popov <[hidden email]> [2006-07-25 14:39:35 -0700]:
> My interpretation of #2 seems to be different from yours then, I thought > Rick wanted to do, > > dict become: Dictionary new Actually, I was just looking for the easiest (and the method with the fewest side effects) way to clear out an existing Dictionary.. I guess this is one of those OO sorts of questions that some people have different opinions on -- which is why I asked.. I'm assuming that the ST garbage collector will eat up any objects that have been abandoned such as what would happen if you do a dict := Dictionary new. I'm not sure what the become: above gets you though.. |
It'll replace all third-party references to the dictionary as well.
"Common" a := (Dictionary new) at: $a put: $A; yourself. b := a. "1)" a := Dictionary new. b keys -->> Set ($a "16r0061") "2)" a become: Dictionary new. b keys -->> Set () See the difference? -Boris -- +1.604.689.0322 DeepCove Labs Ltd. 4th floor 595 Howe Street Vancouver, Canada V6C 2T5 [hidden email] CONFIDENTIALITY NOTICE This email is intended only for the persons named in the message header. Unless otherwise indicated, it contains information that is private and confidential. If you have received it in error, please notify the sender and delete the entire message including any attachments. Thank you. -----Original Message----- From: Rick F. [mailto:[hidden email]] Sent: Tuesday, July 25, 2006 2:41 PM To: Boris Popov Cc: Dave Stevenson; [hidden email] Subject: Re: Proper way to empty a Dictionary.. * Boris Popov <[hidden email]> [2006-07-25 14:39:35 -0700]: > My interpretation of #2 seems to be different from yours then, I thought > Rick wanted to do, > > dict become: Dictionary new Actually, I was just looking for the easiest (and the method with the fewest side effects) way to clear out an existing Dictionary.. I guess this is one of those OO sorts of questions that some people have different opinions on -- which is why I asked.. I'm assuming that the ST garbage collector will eat up any objects that have been abandoned such as what would happen if you do a dict := Dictionary new. I'm not sure what the become: above gets you though.. smime.p7s (4K) Download Attachment |
In reply to this post by Rick Flower
Rick
dict := Dictionary new. Do not play with #become: until you are quite familiar with Smalltalk. Terry =========================================================== Terry Raymond Smalltalk Professional Debug Package Crafted Smalltalk 80 Lazywood Ln. Tiverton, RI 02878 (401) 624-4517 [hidden email] <http://www.craftedsmalltalk.com> =========================================================== > -----Original Message----- > From: Rick F. [mailto:[hidden email]] > Sent: Tuesday, July 25, 2006 5:07 PM > To: [hidden email] > Subject: Proper way to empty a Dictionary.. > > Hi all again.. Another newbie question for you all.. If I've got a > dictionary > that has N elements in it, I figure there are two ways to empty it out : > > 1) use the #remove method (or perhaps #removeKey since it's a dictionary). > > 2) Just replace the current Dictionary with a new one and let the old (now > dangling one) > get garbage collected.. > > Is #2 a viable option to clear that thing out or is that naughty from a ST > purist' point > of view? I'm guessing that #2 is fine, but that you probably wouldn't > want to do it too > frequently.. Comments? > > Thanks! |
I'm actually a bit surprised that people would suggest replacing the value
of a variable just like that without giving consideration to the fact that other objects may be holding onto it as well explicitly (db cache, UI, running processes etc) or implicitly (registered event handlers etc). Why wouldn't you just remove all elements from the dictionary via, dict keys do: [:ea | dict removeKey: ea] ? Just curious, Cheers! -Boris -- +1.604.689.0322 DeepCove Labs Ltd. 4th floor 595 Howe Street Vancouver, Canada V6C 2T5 [hidden email] CONFIDENTIALITY NOTICE This email is intended only for the persons named in the message header. Unless otherwise indicated, it contains information that is private and confidential. If you have received it in error, please notify the sender and delete the entire message including any attachments. Thank you. -----Original Message----- From: Terry Raymond [mailto:[hidden email]] Sent: Tuesday, July 25, 2006 3:30 PM To: 'Rick F.'; [hidden email] Subject: RE: Proper way to empty a Dictionary.. Rick dict := Dictionary new. Do not play with #become: until you are quite familiar with Smalltalk. Terry =========================================================== Terry Raymond Smalltalk Professional Debug Package Crafted Smalltalk 80 Lazywood Ln. Tiverton, RI 02878 (401) 624-4517 [hidden email] <http://www.craftedsmalltalk.com> =========================================================== > -----Original Message----- > From: Rick F. [mailto:[hidden email]] > Sent: Tuesday, July 25, 2006 5:07 PM > To: [hidden email] > Subject: Proper way to empty a Dictionary.. > > Hi all again.. Another newbie question for you all.. If I've got a > dictionary > that has N elements in it, I figure there are two ways to empty it out : > > 1) use the #remove method (or perhaps #removeKey since it's a dictionary). > > 2) Just replace the current Dictionary with a new one and let the old (now > dangling one) > get garbage collected.. > > Is #2 a viable option to clear that thing out or is that naughty from a ST > purist' point > of view? I'm guessing that #2 is fine, but that you probably wouldn't > want to do it too > frequently.. Comments? > > Thanks! smime.p7s (4K) Download Attachment |
In reply to this post by Rick Flower
Boris Popov wrote:
> I'm actually a bit surprised that people would suggest replacing the value > of a variable just like that without giving consideration to the fact that > other objects may be holding onto it as well explicitly (db cache, UI, > running processes etc) or implicitly (registered event handlers etc). Why > wouldn't you just remove all elements from the dictionary via, > > dict keys do: [:ea | dict removeKey: ea] > > ? > > Just curious, I imagine different assumptions -- they assume that the Dictionary is being referenced in only one place, you are assuming the worst, which is that it's referenced all over. I agree with both. :-) If the Dictionary is known to be referenced from only one variable, it's simpler code and probably less execution time (garbage collection is pretty cheap) to do dict := Dictionary new. If the Dictionary might be referenced from multiple places, it's safer to do dict keys do: [:ea | dict removeKey: ea]. Regards, -Martin |
I guess I like to err on the side of caution, but not to the point where I'd
like the handholding of type checked languages ;) Cheers! -Boris -- +1.604.689.0322 DeepCove Labs Ltd. 4th floor 595 Howe Street Vancouver, Canada V6C 2T5 [hidden email] CONFIDENTIALITY NOTICE This email is intended only for the persons named in the message header. Unless otherwise indicated, it contains information that is private and confidential. If you have received it in error, please notify the sender and delete the entire message including any attachments. Thank you. -----Original Message----- From: Martin McClure [mailto:[hidden email]] Sent: Tuesday, July 25, 2006 3:51 PM To: Boris Popov Cc: Terry Raymond; Rick F.; [hidden email] Subject: Re: Proper way to empty a Dictionary.. Boris Popov wrote: > I'm actually a bit surprised that people would suggest replacing the value > of a variable just like that without giving consideration to the fact that > other objects may be holding onto it as well explicitly (db cache, UI, > running processes etc) or implicitly (registered event handlers etc). Why > wouldn't you just remove all elements from the dictionary via, > > dict keys do: [:ea | dict removeKey: ea] > > ? > > Just curious, being referenced in only one place, you are assuming the worst, which is that it's referenced all over. I agree with both. :-) If the Dictionary is known to be referenced from only one variable, it's simpler code and probably less execution time (garbage collection is pretty cheap) to do dict := Dictionary new. If the Dictionary might be referenced from multiple places, it's safer to do dict keys do: [:ea | dict removeKey: ea]. Regards, -Martin smime.p7s (4K) Download Attachment |
In reply to this post by Boris Popov, DeepCove Labs (SNN)
On Jul 25, 2006, at 15:39, Boris Popov wrote:
It depends on what is wanted. Whether the client wants that dictionary to be empty (and thus any other observers of it to see it as empty), or just wants his own empty version. In the former case, the become: has the same semantic effects as your example. Anyway... This got me to looking and thinking. Our BaseExtensions package has a removeAll method. It's a handy method; I'm surprised others aren't mentioning having a similar one. It's implemented naively and inefficiently as: removeAll self removeAll: self copy Dictionary and KeyedCollection both override at as you show removeAll self keysDo: [:key | self removeKey: key]. I probably should add a SequenceableCollection variant that doesn't do the copy. I think I may throw all of them away though :) Looking at Set(Dictionary), what's really tempting is to just do: removeAll tally := 0. This is VERY fast, and does indeed make the Set(or Dictionary) appear to be empty. For effective purposes, it no longer contains any elements. But stored in its memory, until some new add:(at:put:) overwrites the slot, the old object references still exist. This is most likely less desirable. Looking around even more, it would seem (to me) that the desirable and singular implementation would be: Collection>>removeAll self become: (self copyEmpty: 0). This is quite fast. Doing some timing tests, it's quicker than all of my implementations except for small collections with 2 or less elements. All of the other implementations are (N) variant, but this one has a fixed cost regardless of receiver size. By the time you get up to a Dictionary with 100 elements for example, it is about 10x faster. One drawback, is that you can do: #(1 3 4 5) removeAll --> #() which is not consistent with the fact that Array>>remove: is not supported. For consistency sake, one might argue that these should implement "shouldNotImplement" variants below. The original naive 'self removeAll: self copy' gets that as a side affect when it eventually gets to trying to send the remove:. <rant aside>As for the use of the become: and the "don't use that" stuff. Well, first off, I'm Experienced(tm), so I get to use it. But seriously, I don't get why we as a "helping" community freak out everytime someone goes near that stuff? What's the message we're sending to Rick F? "Whatever you do, don't look at how the other collections change their size and do something like that! Ignore any lesson you might garner from looking at the source code! Read our docs instead! (hehe, extra points for bridging to another recent debate)." It's not like this is C++ or something. Sometimes I wonder if we're jealous of the multitiered levels of ascension that mark the path to deeper C++ understanding and deification. So we carve out our own bit of "oo, tricky" stuff and exalt it. Ask yourself this, those of you that feel entitled to invoke the spells of become:, changeClassTo:, or utter the unforgivable verse "thisContext". Have you never just screwed up with one of these on your own? Or have you leveraged the use of them correctly since day one by following a rigid and structured catechism laid down by the early Kay-ites? I'm betting every Smalltalk guru out there who has effectively used these "deep" aspects of Smalltalk, learned the proper use of them through the school of hard knocks. I'm not suggesting Rick F. should or shouldn't make use of become:; but rather we try to avoid any sense of "pay no attention to that man behind the curtain."</rant aside> -- Travis Griggs Objologist "It's [a spec] _the_ single worst way to write software, because it by definition means that the software was written to match theory, not reality" - Linus Torvalds DISCLAIMER: This email is bound by the terms and conditions described at http://www.key.net/disclaimer.htm |
In reply to this post by Boris Popov, DeepCove Labs (SNN)
Boris
The question came from a novice, so in all likelyhood the simple answer is the correct one. If the dictionary is used elsewhere as well I would assume he would have mentioned that it is shared. Let's not overwhelm a novice with overly complicated answers. Terry =========================================================== Terry Raymond Smalltalk Professional Debug Package Crafted Smalltalk 80 Lazywood Ln. Tiverton, RI 02878 (401) 624-4517 [hidden email] <http://www.craftedsmalltalk.com> =========================================================== > -----Original Message----- > From: Boris Popov [mailto:[hidden email]] > Sent: Tuesday, July 25, 2006 6:40 PM > To: Terry Raymond; Rick F.; [hidden email] > Subject: RE: Proper way to empty a Dictionary.. > > I'm actually a bit surprised that people would suggest replacing the value > of a variable just like that without giving consideration to the fact that > other objects may be holding onto it as well explicitly (db cache, UI, > running processes etc) or implicitly (registered event handlers etc). Why > wouldn't you just remove all elements from the dictionary via, > > dict keys do: [:ea | dict removeKey: ea] > > ? > > Just curious, > > Cheers! > > -Boris > > -- > +1.604.689.0322 > DeepCove Labs Ltd. > 4th floor 595 Howe Street > Vancouver, Canada V6C 2T5 > > [hidden email] > > CONFIDENTIALITY NOTICE > > This email is intended only for the persons named in the message > header. Unless otherwise indicated, it contains information that is > private and confidential. If you have received it in error, please > notify the sender and delete the entire message including any > attachments. > > Thank you. > > -----Original Message----- > From: Terry Raymond [mailto:[hidden email]] > Sent: Tuesday, July 25, 2006 3:30 PM > To: 'Rick F.'; [hidden email] > Subject: RE: Proper way to empty a Dictionary.. > > Rick > > dict := Dictionary new. > > Do not play with #become: until you are quite familiar with > Smalltalk. > > Terry > > =========================================================== > Terry Raymond Smalltalk Professional Debug Package > Crafted Smalltalk > 80 Lazywood Ln. > Tiverton, RI 02878 > (401) 624-4517 [hidden email] > <http://www.craftedsmalltalk.com> > =========================================================== > > > -----Original Message----- > > From: Rick F. [mailto:[hidden email]] > > Sent: Tuesday, July 25, 2006 5:07 PM > > To: [hidden email] > > Subject: Proper way to empty a Dictionary.. > > > > Hi all again.. Another newbie question for you all.. If I've got a > > dictionary > > that has N elements in it, I figure there are two ways to empty it out : > > > > 1) use the #remove method (or perhaps #removeKey since it's a > dictionary). > > > > 2) Just replace the current Dictionary with a new one and let the old > (now > > dangling one) > > get garbage collected.. > > > > Is #2 a viable option to clear that thing out or is that naughty from a > ST > > purist' point > > of view? I'm guessing that #2 is fine, but that you probably wouldn't > > want to do it too > > frequently.. Comments? > > > > Thanks! > |
In reply to this post by Travis Griggs
First, I would like a #removeAll, but I
certainly would not do it by removing the keys. If more than one
element hashes to the same index then when the first item is removed the
other must be moved. If you don’t use #become:, I would
do a tally := 0 and nil all the slots. But even better would be if Dictionary was
implemented using a collection variable instead of the
receiver’s indexed slots, then you could just replace the variable. With respect to a novice using #become: I
did not say don’t use it, I said learn more about St first. The
original question leads me to believe that the author has only recently started
to learn Smalltalk. Have you seen code that improperly uses
#become: because the developer though it was cool? Also, while
#become: is cheap in VW, it is more expensive in other smalltalks. I would much rather see the collection
classes implemented using a ‘contents’ ivar instead of the
receiver’s indexed slots. In addition to other reasons, it would also eliminate the
need for the use of #become: to grow a collection. Terry From: Travis Griggs
[mailto:[hidden email]] On Jul 25, 2006, at 15:39, Boris Popov wrote:
I'm actually a bit surprised that people would suggest replacing the
value of a variable just like that without giving consideration to the fact
that other objects may be holding onto it as well explicitly (db cache, UI, running processes etc) or implicitly (registered event handlers etc).
Why wouldn't you just remove all elements from the dictionary via, dict keys do: [:ea | dict removeKey: ea] It depends on what is wanted. Whether the client wants that dictionary
to be empty (and thus any other observers of it to see it as empty), or just
wants his own empty version. In the former case, the become: has the same
semantic effects as your example. Anyway... This got me to looking and thinking. Our BaseExtensions package has a
removeAll method. It's a handy method; I'm surprised others aren't mentioning
having a similar one. It's implemented naively and inefficiently as: removeAll self
removeAll: self copy Dictionary and KeyedCollection both override at as you show removeAll self
keysDo: [:key | self removeKey: key]. I probably should add a SequenceableCollection variant that doesn't do
the copy. I think I may throw all of them away though :) Looking at Set(Dictionary), what's really tempting is to just do: removeAll tally
:= 0. This is VERY fast, and does indeed make the Set(or Dictionary) appear
to be empty. For effective purposes, it no longer contains any elements. But
stored in its memory, until some new add:(at:put:) overwrites the slot, the old
object references still exist. This is most likely less desirable. Looking
around even more, it would seem (to me) that the desirable and singular
implementation would be: Collection>>removeAll self
become: (self copyEmpty: 0). This is quite fast. Doing some timing tests, it's quicker than all of
my implementations except for small collections with 2 or less elements. All of
the other implementations are (N) variant, but this one has a fixed cost
regardless of receiver size. By the time you get up to a Dictionary with 100
elements for example, it is about 10x faster. One drawback, is that you can do: #(1 3 4 5) removeAll --> #() which is not consistent with the fact that Array>>remove: is not
supported. For consistency sake, one might argue that these should implement
"shouldNotImplement" variants below. The original naive 'self
removeAll: self copy' gets that as a side affect when it eventually gets to
trying to send the remove:. <rant aside>As for the use of the become: and the "don't use
that" stuff. Well, first off, I'm Experienced(tm), so I get to use it. But
seriously, I don't get why we as a "helping" community freak out
everytime someone goes near that stuff? What's the message we're sending to
Rick F? "Whatever you do, don't look at how the other collections change
their size and do something like that! Ignore any lesson you might garner from
looking at the source code! Read our docs instead! (hehe, extra points for
bridging to another recent debate)." It's not like this is C++ or something. Sometimes I wonder if we're
jealous of the multitiered levels of ascension that mark the path to deeper C++
understanding and deification. So we carve out our own bit of "oo,
tricky" stuff and exalt it. Ask yourself this, those of you that feel
entitled to invoke the spells of become:, changeClassTo:, or utter the
unforgivable verse "thisContext". Have you never just screwed up with
one of these on your own? Or have you leveraged the use of them correctly since
day one by following a rigid and structured catechism laid down by the early
Kay-ites? I'm betting every Smalltalk guru out there who has effectively used
these "deep" aspects of Smalltalk, learned the proper use of them
through the school of hard knocks. I'm not suggesting Rick F. should or
shouldn't make use of become:; but rather we try to avoid any sense of
"pay no attention to that man behind the curtain."</rant aside> -- Travis Griggs Objologist "It's [a spec]
_the_ single worst way to write software, because it by definition means that
the software was written to match theory, not reality" - Linus Torvalds
DISCLAIMER: This email is bound by the terms and conditions described at http://www.key.net/disclaimer.htm |
In reply to this post by Terry Raymond
Terry Raymond wrote:
> Boris > > The question came from a novice, so in all likelyhood > the simple answer is the correct one. If the dictionary > is used elsewhere as well I would assume he would have > mentioned that it is shared. Let's not overwhelm a novice > with overly complicated answers. Thanks everyone for your valuable input on this topic.. I am fairly new to ST and want to make sure my code quality is up-to-par with the proper way to do various things and that was what really prompted me to ask such a question.. Thanks again.. -- Rick |
Free forum by Nabble | Edit this page |