krisppurg / dimscord

A Discord Bot & REST Library for Nim.
https://krisppurg.github.io/dimscord/
MIT License
222 stars 22 forks source link

Improve serializing/string conversions + some other opportunities to optimize code #102

Open nayr7 opened 1 year ago

nayr7 commented 1 year ago

Consider the following snippets:

Example 1:

## objects.nim

proc newMessage*(data: JsonNode): Message =
    result = data.`$`.fromJson(Message)

# or ...

proc `[]=`(obj: ref object, fld: string, val: JsonNode) =
    for name, field in obj[].fieldPairs:
        if name == fld:
            field = ($val).fromJson(typeof(field))

# etc ...

and just about anything that use fromJson but cannot take in JsonNodes

Example 2:

# it's cool that we can do something clean like this in Nim:
        let row = newActionRow @[
            newButton(label = "Lose ?", idOrUrl = "lost", style = Danger),
            newButton(label = "Win ?", idOrUrl = "won", style = Primary)
        ]

... but we can do even better:

# notice the lack of `@`, we're using arrays instead of seq == statically allocated array == more speed !
        let row = newActionRow [
            newButton(label = "Lose ?", idOrUrl = "lost", style = Danger),
            newButton(label = "Win ?", idOrUrl = "won", style = Primary)
        ]

... which brings us to

openArray and other stringing shenanigans :

profiling as we go

The main trap of optimizing stuff is that we think we are making things faster when in fact we are making them slower, every change must come with microbenchmark to ensure we are not shooting ourselves in the foot

ire4ever1190 commented 1 year ago

Issue with openArray is that it cannot be a parameter for async procs (Since it cant be guaranteed that the array/seq it is pointing to will still be alive). Could use something like an Iterable concept that kinda achieves the same effect (Although it would increase the binary size if used with lots of different array sizes).

The reason for the string conversion is because of the use of jsony, could switch to jsonutils. While it isn't faster in benchmarks, not needing to convert to/from strings could make it faster (Another benefit, jsonutils has better error messages).

Has with most API clients, biggest slowdown will be network. Would be good to analyse refactoring requester.nim to use a pool of clients instead of creating a new one everytime to see if it gives lower latency

Rest of the suggestions seem good though :rocket: