Improve serializing/string conversions + some other opportunities to optimize code
nayr7 opened this issue · comments
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 JsonNode
s
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 string
ing shenanigans :
- Using more
sink
andopenArray[char]
in the code when possible - Using procs that can accept
JsonNode
instead of converting them to string which is surely a way too costly operation for functions that run as often asnewMessage
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
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 🚀