-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
up to 20x faster jsonutils deserialization #18183
Conversation
lib/std/jsonutils.nim
Outdated
# just pick 1 exception type for simplicity; other choices would be: | ||
# JsonError, JsonParser, JsonKindError | ||
raise newException(ValueError, msg) | ||
proc raiseJsonException(condStr: string, msg: string) = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
proc raiseJsonException(condStr: string, msg: string) = | |
proc raiseJsonException(condStr: string, msg: string) {.noinline.} = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done, but can't the C compiler figure out on its own (and without PGO) that it's not worth inlining because it's a noreturn ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With --exceptions:goto it's not a noreturn.
The lesson seems to me "split code into hot/cold sections", it was a template and thus inlined before. |
9688751
to
42e943d
Compare
actually both my original conclusion and yours are off-track, IMO the real explanation here is that template calls allow for lazy parameter evaluation (in D, the analog is checkJson ok, $(json.len, num, numMatched, $T, json) before PR, after PR, This also explains why This is applicable in other scenarios, and in particular means that it's usually ok [1] to have verbose asserts with potentially complex code when a condition fails, so long the assert/enforce is a template, like [1] instruction cache size being a second-order effect |
Which I care about though... |
that's essentially what |
* up to 20x faster jsonutils deserialization * noinline
condStr
is now used instead of ignored (unrelated bugfix)nim r -d:danger main
(taking best of 5 runs)
before PR
0.27
after PR
0.012
example directly adapted from https://forum.nim-lang.org/t/8074
performance lesson learned
avoid function calls in hotspots (even{.inline.}
or--passc:-flto
would not help here)actually the real explanation is instead explained here: #18183 (comment), which is that templates, unlike procs, allow lazy parameter evaluation eg: