Skip to content

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

Description

@RevensofT

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.

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

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

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

Ps. Sorry for no label, I can't add a label, don't see any option like in help document in topic apply labels to an issue.

No activity

Activity on this issue will appear here.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions