Home All Groups Group Topic Archive Search About

Question about strings and compiler

Author
8 Jun 2006 2:40 PM
vcinquini
I'm doing some maintanence in a application where I've found a lot of
code like this:

strSql = _
"SELECT u.id_usr, u.cd_usr, p.nm_psoa, u.nr_niv_hier_usr" & _
" FROM " & GlobalDB.TABLE_USERS & _
" u, " & GlobalDB.TABLE_PERSONS & _
" p WHERE u.fl_exc_usr = 0 AND p.id_psoa = u.id_psoa" & _
" ORDER BY p.nm_psoa, u.nr_niv_hier_usr"

I'd like to know if vb.net compiler is smart enough to detect this at
compiler time and replace for a unique string resulting from the
concatenation of all strings or  the concatenation will occur at run
time?

Thanks in advance

Author
8 Jun 2006 2:46 PM
Herfried K. Wagner [MVP]
<vcinqu***@gmail.com> schrieb:
Show quoteHide quote
> I'm doing some maintanence in a application where I've found a lot of
> code like this:
>
> strSql = _
> "SELECT u.id_usr, u.cd_usr, p.nm_psoa, u.nr_niv_hier_usr" & _
> " FROM " & GlobalDB.TABLE_USERS & _
> " u, " & GlobalDB.TABLE_PERSONS & _
> " p WHERE u.fl_exc_usr = 0 AND p.id_psoa = u.id_psoa" & _
> " ORDER BY p.nm_psoa, u.nr_niv_hier_usr"
>
> I'd like to know if vb.net compiler is smart enough to detect this at
> compiler time and replace for a unique string resulting from the
> concatenation of all strings or  the concatenation will occur at run
> time?

If the string consists of constants (string literals), the concatenation
will be performed at compile-time and only the concatenated string will be
stored as a literal inside the IL.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
8 Jun 2006 3:55 PM
Phill W.
vcinqu***@gmail.com wrote:

> strSql = _
> "SELECT u.id_usr, u.cd_usr, p.nm_psoa, u.nr_niv_hier_usr" & _
> " FROM " & GlobalDB.TABLE_USERS & _
> " u, " & GlobalDB.TABLE_PERSONS & _
> " p WHERE u.fl_exc_usr = 0 AND p.id_psoa = u.id_psoa" & _
> " ORDER BY p.nm_psoa, u.nr_niv_hier_usr"
>
> I'd like to know if vb.net compiler is smart enough to detect this at
> compiler time and replace for a unique string resulting from the
> concatenation of all strings

Yes.  Hacking the above about a little to get this ...

Public Sub SC()
    Const TABLE_USERS As String = "USERS"
    Const TABLE_PERSONS As String = "PERSONS"
    Dim s As String
    s = _
       "SELECT u.id_usr, u.cd_usr, p.nm_psoa, u.nr_niv_hier_usr" & _
       " FROM " & TABLE_USERS & _
       " u, " & TABLE_PERSONS & _
       " p WHERE u.fl_exc_usr = 0 AND p.id_psoa = u.id_psoa" & _
       " ORDER BY p.nm_psoa, u.nr_niv_hier_usr"
End Sub

.... building it and then pulling the resulting .exe apart again using
ILDAsm, the I.L.Code created looks something like

..method public instance void  SC() cil managed
{
   // Code size       9 (0x9)
   .maxstack  1
   .locals init ([0] string s)
   IL_0000:  nop
   IL_0001:  ldstr      "SELECT u.id_usr, u.cd_usr, p.nm_psoa, u.nr_niv_hie"
   + "r_usr FROM USERS u, PERSONS p WHERE u.fl_exc_usr = 0 AND p.id_psoa
= u."
   + "id_psoa ORDER BY p.nm_psoa, u.nr_niv_hier_usr"
   IL_0006:  stloc.0
   IL_0007:  nop
   IL_0008:  ret
}

Not a call to String.Concat in sight, so it's all done at compile time.

HTH,
    Phill  W.
Author
8 Jun 2006 4:52 PM
vcinquini
Which one would have best performance?

(no '+' operation)

IL_0001:  ldstr      "SELECT u.id_usr, u.cd_usr, p.nm_psoa,
u.nr_niv_hie r_usr FROM USERS u, PERSONS p WHERE u.fl_exc_usr = 0 AND
p.id_psoa u.id_psoa ORDER BY p.nm_psoa, u.nr_niv_hier_usr

or

('+' operations)

IL_0001:  ldstr      "SELECT u.id_usr, u.cd_usr, p.nm_psoa,
u.nr_niv_hie"
              "r_usr FROM USERS u, PERSONS p WHERE u.fl_exc_usr = 0 AND
p.id_psoa
              u." + "id_psoa ORDER BY p.nm_psoa, u.nr_niv_hier_usr"



Phill W. wrote:
Show quoteHide quote
> vcinqu***@gmail.com wrote:
>
> > strSql = _
> > "SELECT u.id_usr, u.cd_usr, p.nm_psoa, u.nr_niv_hier_usr" & _
> > " FROM " & GlobalDB.TABLE_USERS & _
> > " u, " & GlobalDB.TABLE_PERSONS & _
> > " p WHERE u.fl_exc_usr = 0 AND p.id_psoa = u.id_psoa" & _
> > " ORDER BY p.nm_psoa, u.nr_niv_hier_usr"
> >
> > I'd like to know if vb.net compiler is smart enough to detect this at
> > compiler time and replace for a unique string resulting from the
> > concatenation of all strings
>
> Yes.  Hacking the above about a little to get this ...
>
> Public Sub SC()
>     Const TABLE_USERS As String = "USERS"
>     Const TABLE_PERSONS As String = "PERSONS"
>     Dim s As String
>     s = _
>        "SELECT u.id_usr, u.cd_usr, p.nm_psoa, u.nr_niv_hier_usr" & _
>        " FROM " & TABLE_USERS & _
>        " u, " & TABLE_PERSONS & _
>        " p WHERE u.fl_exc_usr = 0 AND p.id_psoa = u.id_psoa" & _
>        " ORDER BY p.nm_psoa, u.nr_niv_hier_usr"
> End Sub
>
> ... building it and then pulling the resulting .exe apart again using
> ILDAsm, the I.L.Code created looks something like
>
> .method public instance void  SC() cil managed
> {
>    // Code size       9 (0x9)
>    .maxstack  1
>    .locals init ([0] string s)
>    IL_0000:  nop
>    IL_0001:  ldstr      "SELECT u.id_usr, u.cd_usr, p.nm_psoa, u.nr_niv_hie"
>    + "r_usr FROM USERS u, PERSONS p WHERE u.fl_exc_usr = 0 AND p.id_psoa
> = u."
>    + "id_psoa ORDER BY p.nm_psoa, u.nr_niv_hier_usr"
>    IL_0006:  stloc.0
>    IL_0007:  nop
>    IL_0008:  ret
> }
>
> Not a call to String.Concat in sight, so it's all done at compile time.
>
> HTH,
>     Phill  W.
Author
8 Jun 2006 6:58 PM
Mattias Sjögren
>Which one would have best performance?

No difference. There's no concatenation being performed at runtime in
either case. It's just the IL disassembler that breaks up the string
for readability. Just like the VB compiler, the IL assembler can
concatenate string constants at compile time.

But even if there was a difference, it would be negligible compared to
the actual database query. Don't waste your time on micro
optimizations.


Mattias

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
9 Jun 2006 12:20 PM
vcinquini
Thanks for all replies.


Mattias Sjögren wrote:
Show quoteHide quote
> >Which one would have best performance?
>
> No difference. There's no concatenation being performed at runtime in
> either case. It's just the IL disassembler that breaks up the string
> for readability. Just like the VB compiler, the IL assembler can
> concatenate string constants at compile time.
>
> But even if there was a difference, it would be negligible compared to
> the actual database query. Don't waste your time on micro
> optimizations.
>
>
> Mattias
>
> --
> Mattias Sjögren [C# MVP]  mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.