-
-
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
Excessive if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
generation
#23311
Comments
To add a quick note, I tried |
If tlv_get_addr shows up in your profile it indicates that your platform has a suboptimal thread local storage implementation. clang can optimize out these accesses but I don't know when it does it. We should investigate. :-) As for your questions: Nim is pessimistic with |
@Araq thanks for the reply. I was not aware of the Does I am very happy be able to have some control over when the error checking is generated! I can now experiment with what (if any) cost various levels of checking has instead of just reaching a wall. Now that I understand why the checks are generated (pessimistic as explained makes sense), I am fully satisfied by having the option to simply do a little more work to get the results I want in the specific cases it matters. As for |
Well no but "omitting error checking" can lead to code that is tricky to get right when you use exception handling for control flow. Been there, done that. ;-) |
I think the main thing Guzba and I are trying to achieve is the ability to profile our code to identify hotspots. We think a lot about hotspots and rewrite them in a more "C-like" manner, where we really consider allocations, branch predictions, cache evictions, and maybe sprinkle in some SIMD. We know everything that can happen to this code because we've thought about it and have tests, fuzzing tests, and benchmarks. We just want to be able to mark some parts as "optimized code here: super unsafe but highly tested." We want Nim to generate plain C without any smart features. Does setting |
To expand on what Araq said, the Nim compiler doesn't track where The
If you want exceptions to continue to work as they normally do, but still get rid of the pessimistic unwind handling, you can use When panics are enabled, all defects raised by compiler-inserted run-time checks immediately terminate the program with |
Yes. |
Thanks for the helpful replies here! |
Description
I recently did some performance-related work and noticed that a lot of code is generated related to exceptions when looking at the C. This excessive checking seems unavoidable as a compiler user and is generated even in what appears to me to be no-point-situations.
Compiled with
nim c --mm:arc -d:release -rf
Example code:
Generates:
So these
inline
andraises: []
procs still resulted in theif (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
check. Maybe this check is unavoidable in all cases?This is just a simplified example. The number of checks is very significant as the the amount of Nim grows.
This is relevant because when doing this performance work I found that ~25% of my program's runtime was at
tlv_get_addr
according to macOS Instruments (noticed this when working on Mac). Perhaps these things are not related, butextern NIM_THREADVAR NIM_BOOL nimInErrorMode__system_u4315;
is the only threadvar in the generated C code and then follows:and
Am I looking in the wrong place? Is this checking unavoidable and
raises: []
can't help? Should these checks be getting optimized out by the C compiler? Thanks for your time.Nim Version
Nim Compiler Version 2.0.2 [Windows: amd64]
Compiled at 2023-12-15
Copyright (c) 2006-2023 by Andreas Rumpf
active boot switches: -d:release
Current Output
No response
Expected Output
No response
Possible Solution
No response
Additional Information
No response
The text was updated successfully, but these errors were encountered: