193 points · by WithinReason · 4y ago · 75 comments
It’s hard to describe how nice the combination of ARC/ORC memory management with the TLSF allocator is for a ready-to-go real time code and systems programming.
This combo gets 90-95% the speed and size of manual memory management, while being much easier to program. Generally performance and real time metrics or latency is a matter of optimizing your code, not re-writing it. Since Nim v2 integrates the allocator it also makes it trivial to debug allocations, or check for leaks.
For a general review of what nim v2 will be about:
- Nim 2 video by Araq at https://nim-lang.org/nimconf2022/
- all the details about the goals of v2 are in the roadmap RFC "Cylons are here" https://github.com/nim-lang/RFCs/issues/437
Aside: I've tinkered with Nim a long time ago (when it was called Nimrod) and thought it was pleasant. I'd be interested to hear from anyone in the HN community that has used Nim in production. What did you use it for? How was the experience?
Some of the debug / error messages were frustrating red herrings which IIRC I was told was an area needing improvement at least at the time.
There are also compile-time superpowers like macros that just receive a parsed AST. That can be used to "re-parse" or "re-compile" external code as in https://github.com/c-blake/cligen. So, trade-offs like in all of life.
There is even a book called The Paradox Of Choice [1]. I think there is just a spectrum/distribution of human predisposition where some like to have things "standardized & packaged up for them" while others more like to invent their own rules..and enough variation within the population that people have to learn to agree to disagree more. (EDIT: and FWIW, I think this is context-modulated - the same person could be on one end of the spectrum about gardening and the other about software.)
I do feel like the syntax is far less chaotic than Perl.
https://en.wikipedia.org/wiki/Nim_(programming_language)#Inf...
All the productivity of a language like Python but with the performance of C.
I hope Jetbrains can prioritize the Clion plugin. It would help a lot.
Also basic usage of generics doesn't really work w/ the vs code plugin, which is the best one.
All Go had to do was say, “Built by Google” and people came running.
Corporate marketing budget and related astroturfing.
More pithy ways to put it are that "there are no speed limits" or "Nim responds to optimization effort about as well as other low level languages like C". You can deploy SIMD intrinsics, for example. In my experience, it's not that hard to "pay only for what you use".
As a more concrete thing, I have timed (yes, on one CPU, one test case, etc..many caveats) the tokenizer used in https://github.com/c-blake/bu/blob/main/rp.nim to be faster than that used by the Rust xsv.
Of course, you really shouldn't tokenize a lot if it's costly/data is big, but rather save a binary answer that does not need parsing (or perhaps more accurately is natively parsed by the CPU).
My experience has been with basically translating Python and shell scripts into Nim, and occasionally comparing them with my own feeble attempts at doing the same in C.
- https://github.com/status-im/nwaku
- https://github.com/status-im/nimbus-eth2
But I think the real killer feature to me is the javascript target. It seems like a real dream for a fullstack experience.
Agree, this is amazing because you can share code and data structures between front and backend (for example: https://github.com/karaxnim/karax).
Also, it's really nice having high level stuff like metaprogramming and static typing spanning both targets. Things like reading a spec file and generating statically checked APIs for server/client is straightforward, which opens up a lot of possibilities.
> "If your domain is not “hard” real-time but “soft” real-time on a conventional OS, you can “pin” a thread to particular core via system.pinToCpu"
This is probably one of the easiest/nicest API's I've seen for doing this in a language.pinToCPU (at least in Nim) just sets scheduling affinity, but does not hard-allocate like the Linux isolcpus boot parameter.."pin" makes it sound more powerful than it is. It is more like POSIX `nice` than POSIX `mlock`.
But also, `nice` exists for a reason. Your agenda might be to do something as much as possible on a Little core which is indeed potentially useful. pinToCPU takes a cpu number and you can usually know from that number whether it is Big or Little.
The real concern may be that the OS has a more global picture of system activity than any one program..and so discourage "second guessing" the scheduler. OTOH, the OS probably does not have a picture of how important timely completion of any particular thing is - honestly that could almost be end-user-provided data (much like there is a `nice` command).
A lot of the time people motivate affinity just as a "use the same L1/L2 cache" for "absolute max perf", and in that sense you may be getting better perf, but over fewer cores (only the big ones) or you may not do the ahead of time work to map out big-little CPU numbers.
Scheduling is also very OS-specific. So, reasoning about what Y you get when you ask for X can also be tricky in any OS-portable setting.
Sometimes things are "so complicated" that people "who know" just discourage those who do not. My inclination is instead to explain what it can/cannot/does/does not do better rather than discourage its use.
I'm surprised that hash maps can stray far enough from O(1) to become unusable in hard real-time. Is it because RT systems have to use lower-quality PRNGs?
But the categorization of real-time depends on the requirements of the problem, not how difficult it is to meet them. If your problem tolerates a few missed deadlines every now and then and your hardware is such that a naive implementation misses deadlines so frequently that it's useless, and so you need to optimize it thoroughly to get the misses under control, it's still soft real-time. It doesn't become soft real-time when technology advances enough that even an inefficient implementation will tolerably a deadline only occasionally.
The malicious version of this is hash DoS, where an attacker chooses the values inserted into the hash table so they have the same hash (or at least same bucket). Randomized hashing using a secure hash mitigates this somewhat, but the attack is still possible to some degree, especially if the hash secret is long lived.
Another issue is that inserting into hash tables is usually only amortized O(1), with the collection growing exponentially when it's full. So most insert operations are cheap, but occasionally you get an O(n) insertion. However it doesn't sound like this is what the article is talking about.
https://www.sciencedirect.com/science/article/abs/pii/019667748790040X
which is notably logarithmic - not unlike a B-Tree.When these expectations are exceeded you can at least detect a DoS attack. If you wait until such are seen, you can activate a "more random" mitigation on the fly at about the same cost as "the next resize/re-org/whatnot".
All you need to do is instrument your search to track the depth. There is some example such strategy in Nim at https://github.com/c-blake/adix for simple Robin-Hood Linear Probed tables and a formula based one from that above paper inside https://github.com/c-blake/suggest
This is all more about the "attack" scenario than the "truly hard" real-time scenario, of course.
And worst case is the only thing that matters in a hard real time system.
https://github.com/GULPF/tiny_sqlite/issues/3
Performance was hugely improved. There was another LRUCache implementation I played with, and when using that for the statement cache, performance was 25% faster than OrderedTable. That didn't make much sense to me for a 100-entry hash table, so I started running some tests comparing LRUCache and OrderedTable. What I discovered is that OrderedTable delete operations created an entirely new copy of the table, minus the entry being deleted, on every delete. That seemed pretty crazy, especially since it was already showing up as performance problems in a 100-entry table.
The tiny_sqlite developer switched to LRUCache, and I did some work on the OrderedTable implementation to make deletes O(1) as expected with hash table operations:
https://github.com/nim-lang/Nim/pull/14995
After spending a lot of time on this, I finally gave up. The problems were:
- the JSON implementation used OrderedTables and never did deletes. JSON benchmark performance was rather sacred, so changing OrderedTables to be slightly slower/larger (I used a doubly-linked list) was not desirable, even if it changed delete performance from O(n) to O(1)
- the Nim compiler also used OrderedTables and never did deletes
- Nim tables allowed multiple values for the same key (I did help get that deprecated).
- alternatives were proposed by others that maintained insertion order until a deleted occurred, but then it could become unordered. That made no sense to me.
The TLDR is, if you use Nim tables, don't use OrderedTable unless you can afford to make an copy of the table on every deleted.
Current Nim OrderedTable delete code: https://github.com/nim-lang/Nim/blob/15bffc20ed8da26e68c88bb...
Issue for db_sqlite not handling nulls, blobs, statement cache: https://github.com/nim-lang/Nim/issues/13559
nim recently implemented public analytics for its website and I think it is a fun thing and something I have not seen around much: https://nim-lang.org/blog/2022/11/01/this-month-with-nim.htm...
do you know of other open source projects which have public analytics?
what happened to that cool landing page you had?