4 ms·
Leveraging Rust to speed Node.js is cool and all, but why the author didn't attempt to optimize the JS code first? If too much intermediate objects negatively i
by Klaster_1 3y ago
Leveraging Rust to speed Node.js is cool and all, but why the author didn't attempt to optimize the JS code first? If too much intermediate objects negatively impact memory consumption, surely re-using the same structure or variables to hold the parsed result would have improved the performance? The benchmark tests not only the speedup brought up by Rust, but an optimization too, so it's unclear which impacts the performance the most.
- pkolaczk 3y agoThat approach may work in short term but in long term you'd likely end up with unreadable and brittle code that is often still not as fast as it would be after the rewrite. You'd introduce additional complexity into your product to overcome limitations of the platform. And tricks like reusing objects can turn into really nasty bugs, especially in languages that don't come with any tools to control aliasing.
- beezlewax 3y agoWhat does aliasing mean in this context?
- pkolaczk 3y agoIf you want to reuse an object to avoid allocation, you better not have a reference to that object from somewhere else. In JS AFAIK there is no way to detect such situation automatically.
- Etheryte 3y agoYes there is, it's called scope and it's super easy to keep track of? In fact, you have to go out of your way to share a reference if you scope it.
- pkolaczk 3y agoIn languages like JS, Java, Python is trivial to accidentally make an object escape the scope by storing a reference in a field of another object that outlives the scope. And it can be another developer 3 months later that can introduce a bug, not knowing about your clever object reuse.
- Cthulhu_ 3y agoI mean I hear what you mean, but counterpoints; The hot path of code is allowed to be more involved than a naive implementation. You mention "unreadable", "brittle" and "complex" as if those are inevitable; I don't believe it, and again, it's worth the tradeoff, like dropping down to ASM in the hot path of C applications. Another counterpoint: By switching to Rust, another language was added to the company's technology stack. This means that they now need to hire for Rust as well, if the author decides to leave. The simple graphics on https://boringtechnology.club/ https://boringtechnology.club/ are pretty interesting in that regard. I don't know anything about this company, but the general advice I'd give is: be careful with adding languages, it adds an exponent to the complexity of your organization and hiring.
- pkolaczk 3y agoThat's a fair point, but we really don't know if Rust was added to the company or only to this project. Big companies often use multiple tech stacks anyway. In my experience the language was never a bottleneck at onboarding new people to the project. It is always the complexity of the project itself, all the non-standard in-house built things that engineers invented over previous X years and often didn't even take time to document properly. I'd personally prefer a boring / standard solution in a relatively modern tech stack I have to learn from scratch than a pile of cleverness in something I know well (let's say Java). In the former case I can learn it quite quickly from the publicly available materials. In the latter case I need to reverse-engineer all the cleverness.
- tacone 3y agoNot 100% sure but just defining those variables outside the loop might have solved.
- r-spaghetti 3y agoOr delete them or set to null inside the loop?
- pkolaczk 3y agoSetting to null doesn't release the memory. It only erases the reference. You still need to wait for the GC to complete.
- baq 3y agoSide note: it might work in a refcount GC like CPython. That isn’t node though. (It only might because it doesn’t guarantee that memory is actually reused.)
- TheFragenTaken 3y agoYou couldn't write a blog post that hit the top of HN if you did that :)
- nerdponx 3y agoIn this case, it's probably easier to just write the Rust version.
- IshKebab 3y agoYou'd end up writing your own Rust-style split implementation and `&str` class and so on. You'd basically be implemented a crap version of Rust in JS. Surely it's better to just use Rust?