External Methods or Declares

  1. last week

    Graham B

    Feb 5 Pre-Release Testers, Xojo Pro The Canada's

    I was just wondering what the more efficient method of exposing macOS class functions and properties for items that could be used a lot, eg metal rendering.

    Should i create a external method:

    Private Function EX_arrayLength(p as Integer) as integer

    Which sets the selector etc, and is called by

    Public Property arrayLength as integer
    Get
      #if TargetMacOS then
        return EX_arrayLength(me.Handle)
      #endif
    End Get
    
    Set
      
    End Set
    
    End Property

    OR is this ok.

    Public Property arrayLength as integer
    Get
      #if TargetMacOS then
        soft declare function getValue lib QuartzCore selector "arrayLength" (p as integer) as integer
        return getValue(me.Handle)
      #endif
    End Get
    
    Set
      
    End Set
    
    End Property

    I was wondering when external functions are resolved, on each call, or on first call, or on startup.

    If i was rendering a scene as fast as possible and say the arraylength property was being got a lot on each render, which would be the best pratice for getting the values?

    @Graham B Just wondering if there was any speed improvements to be had.
    All my previous declares aren't called very often and never in performance code.

    Yes, there is. I don't have any numbers to hand, but in my tests when "External methods" were first introduced, I discovered they are faster and I try to use them as much as possible. It makes sense as declares wrapped in a method have the overhead of the wrapping method.

    In both of your tests, they're wrapped in a method. Try comparing one that's in a method and one that's declared as an External Method.

    For me personally, I have most of the Foundation and AppKit declares as external methods, while other functions I have as methods/properties of a NSObjectWrapper subclass. The latter is easier for functions/objects I don't use regularly.

  2. Graham B

    Feb 5 Pre-Release Testers, Xojo Pro The Canada's

    Just ran a test polling two different properties code the different ways and there is literally no difference in speed of hitting it 1,000,000 times. (~0.2 of a second).

  3. Andrew L

    Feb 5 San Francisco, CA, USA

    External methods and declares are one and the same.

    I was wondering when external functions are resolved, on each call, or on first call, or on startup.

    Soft declares are resolved on first use. "Hard" declares are resolved on startup.

  4. Graham B

    Feb 5 Pre-Release Testers, Xojo Pro The Canada's

    Just wondering if there was any speed improvements to be had.
    All my previous declares aren't called very often and never in performance code.

  5. Sam R

    Feb 6 Pre-Release Testers, Xojo Pro Answer Hengchun, Pingtung, Taiwan
    Edited last week

    @Graham B Just wondering if there was any speed improvements to be had.
    All my previous declares aren't called very often and never in performance code.

    Yes, there is. I don't have any numbers to hand, but in my tests when "External methods" were first introduced, I discovered they are faster and I try to use them as much as possible. It makes sense as declares wrapped in a method have the overhead of the wrapping method.

    In both of your tests, they're wrapped in a method. Try comparing one that's in a method and one that's declared as an External Method.

    For me personally, I have most of the Foundation and AppKit declares as external methods, while other functions I have as methods/properties of a NSObjectWrapper subclass. The latter is easier for functions/objects I don't use regularly.

  6. Graham B

    Feb 6 Pre-Release Testers, Xojo Pro The Canada's

    You are right!, i am now getting 10x speed improvements using direct calls to external method (0.2 seconds compared to 0.02)
    I think i will cache the handles to the object and call the functions directly in the code which is performance based.

    Wrapping all the metal classes up is very handy to figure out what is going on and get it running. Then optimizing later will be the smarter option i think. I get very confused very quickly trying to figure out what all the handles are :P

or Sign Up to reply!