Identity vs equality (was Re: [Newbies] Assignment)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
40 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Identity vs equality (was Re: [Newbies] Assignment)

Göran Krampe
Hi!

(cc to squeak-dev)

>> All the character is unique so is better (faster) to use #==.
>
>   This is not true for Characters whose code is bigger than 256.
>
>   To me, the identity comparison is more or less in the "meta" level
> or touching implementation details.  Unless you eally need to check
> the "identity" for a good reason, you should always use #= for
> comparison, I think.  (Some performance critical potion of code could
> be exempted... but it should still be limited very carefully.)
>
> -- Yoshiki

Just wanted to mention that this came up on IRC the other day and IMHO one
can generally go by this rule - use #= for equality and only use #== if
you actually *intend* to check for identity. It is slightly poor style to
use #== just to gain a bit of speed, when you in fact *mean* equality.

If we generalise the rule is - program by intention as much as possible
and instead focus on Compiler and friends for speed tricks. :)

It might be interesting to hear what Bryce thinks about this - could for
example Exupery get "tricked" by using #== when you actually mean #= and
actually end up making slower code than if the developer had used #=?

In this particular case it may very well be so that identity checks are
faster than any conceivable equality check - but who knows. :)

regards, Göran

PS. Sure, I have also broken this rule, no doubt about that. ;)


Reply | Threaded
Open this post in threaded view
|

Re: Identity vs equality (was Re: [Newbies] Assignment)

Nicolas Cellier-3
Göran Krampe a écrit :

> Hi!
>
> (cc to squeak-dev)
>
>>> All the character is unique so is better (faster) to use #==.
>>   This is not true for Characters whose code is bigger than 256.
>>
>>   To me, the identity comparison is more or less in the "meta" level
>> or touching implementation details.  Unless you eally need to check
>> the "identity" for a good reason, you should always use #= for
>> comparison, I think.  (Some performance critical potion of code could
>> be exempted... but it should still be limited very carefully.)
>>
>> -- Yoshiki
>
> Just wanted to mention that this came up on IRC the other day and IMHO one
> can generally go by this rule - use #= for equality and only use #== if
> you actually *intend* to check for identity. It is slightly poor style to
> use #== just to gain a bit of speed, when you in fact *mean* equality.
>
> If we generalise the rule is - program by intention as much as possible
> and instead focus on Compiler and friends for speed tricks. :)
>
> It might be interesting to hear what Bryce thinks about this - could for
> example Exupery get "tricked" by using #== when you actually mean #= and
> actually end up making slower code than if the developer had used #=?
>
> In this particular case it may very well be so that identity checks are
> faster than any conceivable equality check - but who knows. :)
>
> regards, Göran
>
> PS. Sure, I have also broken this rule, no doubt about that. ;)
>
>

For sure, #== has been used with Symbols which can compare really fast
instead of enumerating characters...

But since in modern Squeak 'Hello' = #'Hello', i'am not sure that old
style optimizations #== don't introduce bugs now...

Nicolas



Reply | Threaded
Open this post in threaded view
|

Re: Identity vs equality (was Re: [Newbies] Assignment)

Göran Krampe
Hi!

> For sure, #== has been used with Symbols which can compare really fast
> instead of enumerating characters...
>
> But since in modern Squeak 'Hello' = #'Hello', i'am not sure that old
> style optimizations #== don't introduce bugs now...
>
> Nicolas

Hmmm, odd:

'Hello' = #'Hello' -> true

#'Hello' = 'Hello' -> false

...that seems to indicate something is a bit wrong. ;)

regards, Göran


Reply | Threaded
Open this post in threaded view
|

Re: Identity vs equality (was Re: [Newbies] Assignment)

Philippe Marschall
2007/8/10, Göran Krampe <[hidden email]>:

> Hi!
>
> > For sure, #== has been used with Symbols which can compare really fast
> > instead of enumerating characters...
> >
> > But since in modern Squeak 'Hello' = #'Hello', i'am not sure that old
> > style optimizations #== don't introduce bugs now...
> >
> > Nicolas
>
> Hmmm, odd:
>
> 'Hello' = #'Hello' -> true
>
> #'Hello' = 'Hello' -> false
>
> ...that seems to indicate something is a bit wrong. ;)
It reflects precisely the difference between Strings and Symbols IMHO.

Cheers
Philippe


Reply | Threaded
Open this post in threaded view
|

Re: Identity vs equality (was Re: [Newbies] Assignment)

Lukas Renggli
In reply to this post by Göran Krampe
> Hmmm, odd:
>
> 'Hello' = #'Hello' -> true
>
> #'Hello' = 'Hello' -> false
>
> ...that seems to indicate something is a bit wrong. ;)

Yes, this is one of the portability issues Seaside run into when
porting to other Smalltalk dialects. I think a Symbol should never be
equal to a String.

Lukas

--
Lukas Renggli
http://www.lukas-renggli.ch

Reply | Threaded
Open this post in threaded view
|

Re: Identity vs equality (was Re: [Newbies] Assignment)

Andrew Tween
In reply to this post by Göran Krampe

"Göran Krampe" <[hidden email]> wrote in message
news:[hidden email]...

> Hi!
>
>> For sure, #== has been used with Symbols which can compare really fast
>> instead of enumerating characters...
>>
>> But since in modern Squeak 'Hello' = #'Hello', i'am not sure that old
>> style optimizations #== don't introduce bugs now...
>>
>> Nicolas
>
> Hmmm, odd:
>
> 'Hello' = #'Hello' -> true
>
> #'Hello' = 'Hello' -> false
>
> ...that seems to indicate something is a bit wrong. ;)
>
> regards, Göran

In 3.10 they both evaluate to true.

But what about this....

d := Dictionary new
 at: 2 put: 10;
 at: 2.0 put: 100;
 yourself.
          ->   a Dictionary(2->10 2.0->100 )

d:=Dictionary new
 at: #a put: 10;
 at: 'a' put: 100;
 yourself.
        ->     a Dictionary(#a->100 )

2 = 2.0 and #a = 'a' , so why the difference ?

2 hash = 2.0 hash -> false (shouldn't this be true?)

---
I recently modified Shout to use == instead of = when comparing a character
from the input stream to a literal char ( e.g. next == $- ). It produced a
measurable speed-up; but now I am feeling guilty ;)
----
Cheers,
Andy



Reply | Threaded
Open this post in threaded view
|

Re: Identity vs equality (was Re: [Newbies] Assignment)

Lukas Renggli
> I recently modified Shout to use == instead of = when comparing a character
> from the input stream to a literal char ( e.g. next == $- ). It produced a
> measurable speed-up; but now I am feeling guilty ;)

Please note that:

    (Character value: 6543) == (Character value: 6543) -> false

This will break as soon as you use WideString as source-code. I had to
modify the runtime and compiler of SmaCC to send #= to characters to
fix some subtle bugs.

Cheers,
Lukas

--
Lukas Renggli
http://www.lukas-renggli.ch

Reply | Threaded
Open this post in threaded view
|

Re: Identity vs equality (was Re: [Newbies] Assignment)

Philippe Marschall
2007/8/10, Lukas Renggli <[hidden email]>:
> > I recently modified Shout to use == instead of = when comparing a character
> > from the input stream to a literal char ( e.g. next == $- ). It produced a
> > measurable speed-up; but now I am feeling guilty ;)
>
> Please note that:
>
>     (Character value: 6543) == (Character value: 6543) -> false
>
> This will break as soon as you use WideString as source-code.

Unicode arrow assignment operator anyone?

Cheers
Philippe

> I had to
> modify the runtime and compiler of SmaCC to send #= to characters to
> fix some subtle bugs.
>
> Cheers,
> Lukas
>
> --
> Lukas Renggli
> http://www.lukas-renggli.ch
>
>

Reply | Threaded
Open this post in threaded view
|

re: Identity vs equality

ccrraaiigg
In reply to this post by Göran Krampe

Hi Göran--

> Hmmm, odd:
>
> 'Hello' = #'Hello' -> true
>
> #'Hello' = 'Hello' -> false
>
> ...that seems to indicate something is a bit wrong. ;)

     To elaborate on Philippe's answer a little...

     The first expression is asking whether the two are equivalent as
far as the String is concerned. Since the Symbol has the same
characters, the String says yes. The second expression asks the same
question of the Symbol. Since the String is not the same object as the
Symbol, the Symbol says no.

     The rationale used in answering a message is the responsibility of
the receiver; different receivers may have different points of view.


-C

--
Craig Latta
improvisational musical informaticist
www.netjam.org
Smalltalkers do: [:it | All with: Class, (And love: it)]


Reply | Threaded
Open this post in threaded view
|

Re: Identity vs equality (was Re: [Newbies] Assignment)

Andreas.Raab
In reply to this post by Göran Krampe
Göran Krampe wrote:
> Hmmm, odd:
>
> 'Hello' = #'Hello' -> true
>
> #'Hello' = 'Hello' -> false
>
> ...that seems to indicate something is a bit wrong. ;)

This has been fixed in Squeak 3.8 or later. You must be a few versions
behind ;-)

   - A.

Reply | Threaded
Open this post in threaded view
|

Re: Identity vs equality (was Re: [Newbies] Assignment)

Andrew Tween
In reply to this post by Philippe Marschall

"Philippe Marschall" <[hidden email]> wrote in message
news:[hidden email]...

> 2007/8/10, Lukas Renggli <[hidden email]>:
>> > I recently modified Shout to use == instead of = when comparing a
>> > character
>> > from the input stream to a literal char ( e.g. next == $- ). It
>> > produced a
>> > measurable speed-up; but now I am feeling guilty ;)
>>
>> Please note that:
>>
>>     (Character value: 6543) == (Character value: 6543) -> false
>>
>> This will break as soon as you use WideString as source-code.
>
> Unicode arrow assignment operator anyone?

Is detected by string comparison rather than by character...

currentToken = ':=' or: [currentToken = '_' or:[currentToken = 16r2190
asCharacter asString]]

>
> Cheers
> Philippe
>
>> I had to
>> modify the runtime and compiler of SmaCC to send #= to characters to
>> fix some subtle bugs.
>>
>> Cheers,
>> Lukas
>>
>> --
>> Lukas Renggli
>> http://www.lukas-renggli.ch
>>
>>
>
>



Reply | Threaded
Open this post in threaded view
|

Re: Identity vs equality (was Re: [Newbies] Assignment)

Andrew Tween
In reply to this post by Lukas Renggli

----- Original Message -----
From: "Lukas Renggli" <[hidden email]>
Newsgroups: gmane.comp.lang.smalltalk.squeak.general
To: "The general-purpose Squeak developers list"
<[hidden email]>
Sent: Friday, August 10, 2007 5:14 PM
Subject: Re: Identity vs equality (was Re: [Newbies] Assignment)


>> I recently modified Shout to use == instead of = when comparing a
>> character
>> from the input stream to a literal char ( e.g. next == $- ). It produced
>> a
>> measurable speed-up; but now I am feeling guilty ;)
>
> Please note that:
>
>    (Character value: 6543) == (Character value: 6543) -> false

Noted.

>
> This will break as soon as you use WideString as source-code. I had to
> modify the runtime and compiler of SmaCC to send #= to characters to
> fix some subtle bugs.

No. It think it will still be ok.

((((Character value: 6543) asString , $- asString) at: 2) == $- ) -> true

I only have literal chars on the right hand side of the ==.
Literal chars can only be < 256 , and are unique.
If they stop being unique, so (Character value: 32) == (Character value:
32) -> false, then you will be right.

Cheers,
Andy

>
> Cheers,
> Lukas
>
> --
> Lukas Renggli
> http://www.lukas-renggli.ch
>
>



Reply | Threaded
Open this post in threaded view
|

Re: Identity vs equality (was Re: [Newbies] Assignment)

Klaus D. Witzel
In reply to this post by Philippe Marschall
On Fri, 10 Aug 2007 18:38:22 +0200, Philippe Marschall wrote:

> 2007/8/10, Lukas Renggli <[hidden email]>:
>> > I recently modified Shout to use == instead of = when comparing a  
>> character
>> > from the input stream to a literal char ( e.g. next == $- ). It  
>> produced a
>> > measurable speed-up; but now I am feeling guilty ;)
>>
>> Please note that:
>>
>>     (Character value: 6543) == (Character value: 6543) -> false
>>
>> This will break as soon as you use WideString as source-code.
>
> Unicode arrow assignment operator anyone?

&larr; ← U+2190 (8592) HTML 4.0 leftwards arrow

Enjoy
/Klaus

> Cheers
> Philippe
>
>> I had to
>> modify the runtime and compiler of SmaCC to send #= to characters to
>> fix some subtle bugs.
>>
>> Cheers,
>> Lukas
>>
>> --
>> Lukas Renggli
>> http://www.lukas-renggli.ch
>>
>>
>
>



Reply | Threaded
Open this post in threaded view
|

Re: Identity vs equality (was Re: [Newbies] Assignment)

Yoshiki Ohshima
In reply to this post by Andrew Tween
  Andrew,

> No. It think it will still be ok.
>
> ((((Character value: 6543) asString , $- asString) at: 2) == $- ) -> true
>
> I only have literal chars on the right hand side of the ==.
> Literal chars can only be < 256 , and are unique.

  This is just a nit-picking, but of course literal chars can be >=
256.  You can write an expression at the end of this email in a
Workspace.

-- Yoshiki

=====
aStream next == $あ
aStream next = $い
=====

Reply | Threaded
Open this post in threaded view
|

Re: Identity vs equality (was Re: [Newbies] Assignment)

Andrew Tween

"Yoshiki Ohshima" <[hidden email]> wrote in message
news:u7io3rrzf.wl%[hidden email]...

>  Andrew,
>
>> No. It think it will still be ok.
>>
>> ((((Character value: 6543) asString , $- asString) at: 2) == $- ) -> true
>>
>> I only have literal chars on the right hand side of the ==.
>> Literal chars can only be < 256 , and are unique.
>
>  This is just a nit-picking, but of course literal chars can be >=
> 256.  You can write an expression at the end of this email in a
> Workspace.

Ah. Cool. I didn't know (or forgot) that. It works in methods too.

(If I replace 'Literal chars can only be < 256' with 'The only literal chars
that I compare against are < 256' , then I think that my reasoning about not
breaking things is then valid)
Cheers,
Andy

>
> -- Yoshiki
>
> =====
> aStream next == $あ
> aStream next = $い
> =====
>
>



Reply | Threaded
Open this post in threaded view
|

Re: Identity vs equality (was Re: [Newbies] Assignment)

Göran Krampe
In reply to this post by Andreas.Raab
Hi!

Andreas Raab <[hidden email]> wrote:

> Göran Krampe wrote:
> > Hmmm, odd:
> >
> > 'Hello' = #'Hello' -> true
> >
> > #'Hello' = 'Hello' -> false
> >
> > ...that seems to indicate something is a bit wrong. ;)
>
> This has been fixed in Squeak 3.8 or later. You must be a few versions
> behind ;-)
>
>    - A.

Ok, I checked in a 3.9 image of some version (at work) but in latest
3.10 it is fine. Solly. :)

regards, Göran

Reply | Threaded
Open this post in threaded view
|

Re: Identity vs equality (was Re: [Newbies] Assignment)

stephane ducasse
In reply to this post by Andrew Tween
>>
>> 'Hello' = #'Hello' -> true
>>
>> #'Hello' = 'Hello' -> false
>>
>> ...that seems to indicate something is a bit wrong. ;)
>>
>> regards, Göran
>
> In 3.10 they both evaluate to true.

Why does it make sense?
I do not understand why #'hello' as a symbol should be = to 'hello'.

It would have been good that such an important change would have been  
discussed
on the mailing-list.

Stef


Reply | Threaded
Open this post in threaded view
|

Re: Identity vs equality (was Re: [Newbies] Assignment)

Andres Valloud-3
stephane ducasse wrote:

>>>
>>> 'Hello' = #'Hello' -> true
>>>
>>> #'Hello' = 'Hello' -> false
>>>
>>> ...that seems to indicate something is a bit wrong. ;)
>>>
>>> regards, Göran
>>
>> In 3.10 they both evaluate to true.
>
> Why does it make sense?
> I do not understand why #'hello' as a symbol should be = to 'hello'.
>
> It would have been good that such an important change would have been
> discussed
> on the mailing-list.
>
> Stef
I hate to bring this up, but Luciano Notarfrancesco and myself fixed
this back in August of 2000.

Thanks,
Andres.

Reply | Threaded
Open this post in threaded view
|

Re: Identity vs equality (was Re: [Newbies] Assignment)

Andreas.Raab
In reply to this post by stephane ducasse
stephane ducasse wrote:
>> In 3.10 they both evaluate to true.
>
> Why does it make sense?

Symmetry. If foo = bar but bar ~= foo you end up in strange situations.
Like here:

s1 := Set new.
s1 add: #foo.
s1 add: 'foo'.

s2 := Set new.
s2 add: 'foo'.
s2 add: #foo.

Do you expect these sets to have the same number of elements? If so, you
better make sure comparisons are symmetric.

> I do not understand why #'hello' as a symbol should be = to 'hello'.

Because 'hello' is equal to #'hello' as a symbol.

> It would have been good that such an important change would have been
> discussed on the mailing-list.

It has. Before your time. The original version of the method has a stamp
saying "di 4/11/2000". And if you look at the archives of Squeak-dev in
the second half of 2000 I'm sure you'll find the discussion.

Cheers,
   - Andreas

Reply | Threaded
Open this post in threaded view
|

Re: Identity vs equality (was Re: [Newbies] Assignment)

Blake-5
On Sat, 11 Aug 2007 11:59:45 -0700, Andreas Raab <[hidden email]>  
wrote:

>> I do not understand why #'hello' as a symbol should be = to 'hello'.
>
> Because 'hello' is equal to #'hello' as a symbol.

Sorta makes the asSymbol/asString methods redundant.

12