If the following possible using the API?

Maybe it would be nice if you guys could show us how the following is done.
Assume I want to write several code optimizations.

For example I want the following lines of code:

ListBox listBox = sender as ListBox;
if (listBox == null)

to be replaced by

ListBox listBox;
if ((listBox = sender as ListBox) == null)

A million thanks

Gabriel Lozano-Morán
MCSD .NET
Real Software
http://www.realdn.net
http://www.realsoftware.be


5 comments

Hello Gabriel,

just curious, what's the point of this optimization? For me,
it only makes code less readable.

Sorry, my post doesn't answer your original question in any way :)


Regards,
Dmitry Shaporenkov
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

Maybe it would be nice if you guys could show us how the following is
done. Assume I want to write several code optimizations.

For example I want the following lines of code:

ListBox listBox = sender as ListBox;
if (listBox == null)
to be replaced by

ListBox listBox;
if ((listBox = sender as ListBox) == null)
A million thanks

Gabriel Lozano-Morán
MCSD .NET
Real Software
http://www.realdn.net
http://www.realsoftware.be



0

The difference is in the the MSIL instructions. Because in the second method
the result of "sender as ListBox" will be copied to the top of the
evaluation stack with the MSIL DUP instruction which is slightly faster than
popping the result from "sender as ListBox" from the evaluation stack to the
local variable listBox and then pushing it back to the evaluation stack for
the if statement.

To be honest the difference is almost nothing but I was curious on how this
could be accomplished using the API...

Gabriel Lozano-Morán

"Dmitry Shaporenkov (JetBrains)" <dsha@jetbrains.com> wrote in message
news:c8a894d9b1cb88c7bcb0cca39fc9@news.intellij.net...

Hello Gabriel,

>

just curious, what's the point of this optimization? For me,
it only makes code less readable.

>

Sorry, my post doesn't answer your original question in any way :)

>
>

Regards,
Dmitry Shaporenkov
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

>

Maybe it would be nice if you guys could show us how the following is
done. Assume I want to write several code optimizations.

>

For example I want the following lines of code:

>

ListBox listBox = sender as ListBox;
if (listBox == null)
to be replaced by

>

ListBox listBox;
if ((listBox = sender as ListBox) == null)
A million thanks

>

Gabriel Lozano-Morán
MCSD .NET
Real Software
http://www.realdn.net
http://www.realsoftware.be

>
>


0

Hello Gabriel,

yes, I've already figured out the difference - I've been curious enough to
look at the
generated MSIL. Strange, I'd expect both code sequences to produce the same
MSIL.
It is also interesting whether both variants of MSIL result in different
native code under
the optimizing JIT, or the difference will finally get away at the native
machine code level.

Back to your original question, you'll need to use what we call PSI (Program
Structure Information).
This is an API that enables to access and modify source code. A large part
of it is a
regular syntax tree consisting of types, members, statements, expressions
and so on.
I guess that the most appropriate way for such an optimization is an context
action - i.e.
an action that is available on certain code fragments in the form of light
bulb (the same bulb
you see in quick fixes). We've recently introduced context action (they should
be included in 210,
if I'm not wrong), and plugins may define new context actions. Unfortunately,
the relevant APIs are
currently not documented, but by the release date I believe at least some
documentation will be
provided.

Regards,
Dmitry Shaporenkov
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

The difference is in the the MSIL instructions. Because in the second
method the result of "sender as ListBox" will be copied to the top of
the evaluation stack with the MSIL DUP instruction which is slightly
faster than popping the result from "sender as ListBox" from the
evaluation stack to the local variable listBox and then pushing it
back to the evaluation stack for the if statement.

To be honest the difference is almost nothing but I was curious on how
this could be accomplished using the API...

Gabriel Lozano-Morán

"Dmitry Shaporenkov (JetBrains)" <dsha@jetbrains.com> wrote in message
news:c8a894d9b1cb88c7bcb0cca39fc9@news.intellij.net...

>> Hello Gabriel,
>>
>> just curious, what's the point of this optimization? For me, it only
>> makes code less readable.
>>
>> Sorry, my post doesn't answer your original question in any way :)
>>
>> Regards,
>> Dmitry Shaporenkov
>> JetBrains, Inc
>> http://www.jetbrains.com
>> "Develop with pleasure!"
>>> Maybe it would be nice if you guys could show us how the following
>>> is done. Assume I want to write several code optimizations.
>>>
>>> For example I want the following lines of code:
>>>
>>> ListBox listBox = sender as ListBox;
>>> if (listBox == null)
>>> to be replaced by
>>> ListBox listBox;
>>> if ((listBox = sender as ListBox) == null)
>>> A million thanks
>>> Gabriel Lozano-Morán
>>> MCSD .NET
>>> Real Software
>>> http://www.realdn.net
>>> http://www.realsoftware.be


0

Hello Gabriel,

Like Dmitry's response, this is not addressing the real issue... Why not
write it as:

if ( sender is ListBox )
listBox = sender as ListBox;

I could figure out how to get the MSIL, but I wouldn't know how to read it
anyways. ;)

Ed

Maybe it would be nice if you guys could show us how the following is
done. Assume I want to write several code optimizations.

For example I want the following lines of code:

ListBox listBox = sender as ListBox;
if (listBox == null)
to be replaced by

ListBox listBox;
if ((listBox = sender as ListBox) == null)
A million thanks

Gabriel Lozano-Morán
MCSD .NET
Real Software
http://www.realdn.net
http://www.realsoftware.be



0

Hello Ed,

it seems for me that your variant of code has higher redundancy than
the original one. That's because if you know that 'sender is ListBox' is
true,
then '(ListBox)sender' is (to the best of my knowledge) a faster way for
cast than
'sender as ListBox'.

Regards,
Dmitry Shaporenkov
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

Hello Gabriel,

Like Dmitry's response, this is not addressing the real issue... Why
not write it as:

if ( sender is ListBox )
listBox = sender as ListBox;
I could figure out how to get the MSIL, but I wouldn't know how to
read it anyways. ;)

Ed

>> Maybe it would be nice if you guys could show us how the following is
>> done. Assume I want to write several code optimizations.
>>
>> For example I want the following lines of code:
>>
>> ListBox listBox = sender as ListBox;
>> if (listBox == null)
>> to be replaced by
>> ListBox listBox;
>> if ((listBox = sender as ListBox) == null)
>> A million thanks
>> Gabriel Lozano-Morán
>> MCSD .NET
>> Real Software
>> http://www.realdn.net
>> http://www.realsoftware.be


0

Please sign in to leave a comment.