Welcome to Keen Software House Forums! Log in or Sign up to interact with the KSH community.
  1. You are currently browsing our forum as a guest. Create your own forum account to access all forum functionality.

System.Text.RegularExpressions performance in scripts

Discussion in 'Programming (In-game)' started by cheb, May 28, 2020.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. cheb Trainee Engineer

    Messages:
    12
    I profiled my freezing script and found that ~400 Regex.IsMatch executions on compiled regexp last 0.2 seconds!
    On my local machine in pure C# the exact same regexps(even heavier) executing 200(!) times faster!

    I changed regex to String.IndexOf and now script working fast.

    But, why regex'es so slow in ingame scripts?

    Regex example:
    DisplayRegex":{"Pattern":"(this will never mathch everything|Components|Ore)","Options":9}
     
  2. DreadedEntity Trainee Engineer

    Messages:
    2
    because regex is slow

    EDIT: To be more clear, regex is like using a sledgehammer to hammer in a nail, when all you really probably needed was a regular hammer. But not just a normal sledgehammer, a sledgehammer that is 10x larger than normal
     
  3. cheb Trainee Engineer

    Messages:
    12
    No, regex'es is fast. They are slow in game for some reason.
    How you will parse ini-style config without regex? I need this in my script.
    Now i have to parse it once and cache result.
     
  4. Malware Master Engineer

    Messages:
    9,860
    @cheb By using MyIni.
    https://github.com/malware-dev/MDK-SE/wiki/Handling-configuration-and-storage

    I'm sorry, but regexes are not fast. Especially not the .NET variety. They also allocate a lot causing late performance it as the GC needs to clean up

    Allocation is bad in the PB because it runs in a hot context. Gotta think like a game programmer when writing PB scripts, unfortunately. Not app programmer.
     
    • Agree Agree x 1
  5. cheb Trainee Engineer

    Messages:
    12
    Thank you for this link. I just realized that you are the author of this wiki. Great work! I constantly have it in ~20 tabs in browser while coding :)

    I tested now. Simple regex almost as fast as String.IndexOf. Probably PB have same game specific.
     
  6. Malware Master Engineer

    Messages:
    9,860
    No, there is no game specific. It's the same regex.


    Why do you think you have to specify the full namespace name, the very same as the one in any other .NET program, to use it?


    We weren't sure that we wanted to include it at all because of its potential impact. Which is why we never auto-included the namespace.
     
  7. cheb Trainee Engineer

    Messages:
    12
    Ok, code is the same. Why then are they slower 200 times in game? Simple IsMatch calls. Simple static compiled regex'es. Maybe compiler specific?
     
  8. Malware Master Engineer

    Messages:
    9,860
    I already suggested why they might be slower in the other thread.

    Whatever compiler chosen for the scripts themselves wouldn't affect the regex instances, they're already compiled and linked up with whatever compiler they use internally. There's no way to replace the internals of it.

    There is no difference.
     
  9. cheb Trainee Engineer

    Messages:
    12
    But regex class compiles regex'es itself in regex objects.
     
  10. Malware Master Engineer

    Messages:
    9,860
    Yes, but it uses a prelinked compiler. The old .NET IL emitter one, most likely, given the age of the regex objects. It certainly doesn't magically figure out what compiler we choose to use for the scripts :p

    Which is the modern Roslyn one, btw.

    Keep in mind, the regexes compile in runtime. Long after the scripts themselves have been compiled.

    They also only actually compile if you explicitly tell them to with the flag.
     
Thread Status:
This last post in this thread was made more than 31 days old.