Fix opcodes from vb compiler when initialize an array and function local variant, jump invoke and delegate invoke.

Topics: VB Language Design
Feb 12, 2015 at 3:32 AM
Edited Feb 12, 2015 at 3:49 AM
Hi guys,

I want to point out some flaw opcodes that vb compiler create and hope VB dev team have sometime to fix those and make VB better performance.

1) Array

When I create an array like this "Dim Data(input.Length - 1) As Char", compiler will rewrite to "Dim Data(input.Length - 1 + 1) As Char"; it's unnecessary 4 bytes and you loss 4 of 64 bytes if you aim to create a tiny method.

2) Function local variant

VB function away has self local variant like "Function foo() As string" will have "Dim foo As String" even it's unused and it bad for performance when you aim to create tiny method header, even a single local variant will make it become a fat method header.

3) Optional to invoke method by jmp

Jump invoke might be not fast as call or callvirt but it's safe for a stack overflow error and recursive method.

4) Delegate invoke

Instead to use invoke method of delegate class, make it become sugar syntax.

ldfld obj Delegate::ThisObj
ldfld native int Delegate::MethodPtr
//Some arguments
calli void(arguments) //maybe has a field stock method signature could helpful to do this line.

Cost for invoke method is really high, less method invoke more performance.

5) Inline method

Long method is hard to read and maintain but it's not suffer from method invoke cost, an inline method create by a non return method(aka Sub) could do this without any problem by use caller method's local variant as arguments of inline method.
  • Use keyword Inline when declare a method.
  • All argument and local variant of an inline method will belong to caller method's local variant.
  • Great performance with easily readable and maintainable without "#Region" in method.
Public Sub A()
      Dim First = 15, Second = 9
      Addition(First, Second)
      Subtraction(16, First)
End Sub

Inline Sub Addition(A As Integer, B As Integer)
      Console.WriteLine(A + B)
End Sub

Inline Sub Subtraction(A As Integer, B As Integer)
      Console.WriteLine(A - B)
End Sub
Code will compile to this.
Public Sub A()
      Dim First = 15, Second = 9
      Console.WriteLine(First + Second)

      Dim Third = 16
      Console.WriteLine(Third - First)
End Sub
Local variant will not create any new local variant, anything else (maybe include a field of class for performance) will create a new local variant.
Feb 14, 2015 at 10:19 PM
We are no longer using this codeplex forum. Please take your discussion to (create an issue to start a discussion)
Feb 15, 2015 at 4:28 PM
Thank you, nmgafter; I'm create an issue at .