Question about strings and compiler

  • Thread starter Thread starter vcinquini
  • Start date Start date
V

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
 
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.
 
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.
 
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"


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.
 
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
 
Thanks for all replies.

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
 
Back
Top