minimal perf code change

This forum is meant for questions and discussions about the X# language and tools
Post Reply
mainhatten
Posts: 200
Joined: Wed Oct 09, 2019 6:51 pm

minimal perf code change

Post by mainhatten »

Hi Robert,
reading up on the sources you pointed to in the vfp / local thread with own brain tuned to rather dumb/not optimizing vfp, the code in
https://github.com/X-Sharp/XSharpPublic ... /Macro.prg
is not nice to CPU...

Code: Select all

INTERNAL FUNCTION _IsIdentifier(cName AS STRING) AS LOGIC
    LOCAL lFirst := TRUE AS LOGIC
    FOREACH VAR c IN cName
        IF lFirst
            IF ! _IsIdentifierStartChar(c)
                RETURN FALSE
            ENDIF
        ELSE
            IF ! _IsIdentifierPartChar(c)
                RETURN FALSE
            ENDIF
        ENDIF
        lFirst := FALSE
    NEXT
    RETURN TRUE
minimal enhancement writes lFirst only on first, not on every char

Code: Select all

INTERNAL FUNCTION _IsIdentifier(cName AS STRING) AS LOGIC
    LOCAL lFirst := TRUE AS LOGIC
    FOREACH VAR c IN cName
        IF lFirst
            IF ! _IsIdentifierStartChar(c)
                RETURN FALSE
            ENDIF
            lFirst := FALSE
        ELSE
            IF ! _IsIdentifierPartChar(c)
                RETURN FALSE
            ENDIF
        ENDIF
    NEXT
    RETURN TRUE
but I'd simplify to

Code: Select all

INTERNAL FUNCTION _IsIdentifier(cName AS STRING) AS LOGIC
    *-- guessing this code is from base[0] land, perhaps replace with the constants for base/lowerbound index value
    *-- check for empty cName probably not needed, but as previous loop was for ... each, guarantee first char to exist
    IF cName:Length()=0 OR ! _IsIdentifierStartChar(cName[0])
       RETURN FALSE
    ENDIF
    *-- loop from 2. char, modify with base index if used not only in zero based strings 
    FOR var lnRun := 1 TO cName:Length()-1 
        IF ! _IsIdentifierPartChar(cName[lnRun])
           RETURN FALSE
        ENDIF
    NEXT
    RETURN TRUE
Roslyn pretty certain to be smarter than vfp compiler, but this is shorter, probably closer to optimized result IL and stays with traditional loops. Hindbrain does such stupid pet tricks automatically from long exposure to vfp shortcomings...

regards
thomas
User avatar
robert
Posts: 4529
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

minimal perf code change

Post by robert »

Thomas,

The code is not called a lot (only from Type()), but I've adjusted the implementation based on your suggestions.

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
Post Reply