4 ms·
Huh? Which languages are you referring to? Javascript, Lua, Python and ruby are all GC based languages with (as far as I know) no special syntax for short lived
by josephg 5y ago
Huh? Which languages are you referring to? Javascript, Lua, Python and ruby are all GC based languages with (as far as I know) no special syntax for short lived objects. Does Go have syntax like that? The only GC language I can think of with special stack variables is C#. Are there others? Does Java do this now?
And on the other side, I’ve still never seen a production C or C++ codebase which used a garbage collector. The closest I’ve seen is refcounting - but that’s a very different beast compared to what V8, .net, Go, etc do internally to garbage collect objects.
The only languages I’ve seen which mix GC and non-GC code are D, Nim (I think), with an honourable mention for Obj-C / Swift’s ARC. But again, I’m not sure ARC belongs in the same category as the generational GCs we see in the JVM and others.
With a few obscure exceptions, it seems to me that the gc/ non-gc line is still pretty firm for most programming languages.
- veltas 5y agoLua for instance has specific syntax added for it: https://www.lua.org/manual/5.4/manual.html#3.3.8 https://www.lua.org/manual/5.4/manual.html#3.3.8 Languages tend to have workarounds or conventions for this kind of code where there isn't special syntax for it: https://stackoverflow.com/a/865272 https://stackoverflow.com/a/865272 (Python) Also just because C or C++ codebases don't use GC doesn't mean it's not useful. I've never used it either, but I can think of situations where I would use it. Like I've not used most of the algorithms I learned at uni but I can still think of where I might use them.
- p_l 5y agoI can think of only Common Lisp, which has optional optimization hint that specific object won't get passed outside of current stack (declaring something DYNAMIC-EXTENT, to be specific). Mind you, it's only a hint, and there were specific techniques for writing "nonconsing" code (in Lisp lingo, "non-allocating", thus not creating garbage)
- hayley-patton 5y agoNote that "short lived objects" and "stack allocated objects" aren't exactly the same set. Common Lisp has a dynamic-extent declaration, which can be used to stack allocate [1]. Go and Java implementations perform escape analysis. There can have some wins if stack allocatability is tracked at runtime, rather than compile-time [2]; Cliff Click reported roughly doubling the number of stack-allocated objects by using escape detection rather than analysis [3]. At least the Inkscape vector editor uses the Boehm garbage collector [4]. There are several programming language implementations which use Boehm and C, but one wonders if it is still a "C codebase" then. [1] http://www.lispworks.com/documentation/HyperSpec/Body/d_dynami.htm http://www.lispworks.com/documentation/HyperSpec/Body/d_dyna... [2] Henry Baker, CONS should not CONS its arguments https://www.cs.tufts.edu/~nr/cs257/archive/henry-baker/cons-lazy-alloc.pdf https://www.cs.tufts.edu/~nr/cs257/archive/henry-baker/cons-... [3] https://youtu.be/5uljtqyBLxI?t=791 https://youtu.be/5uljtqyBLxI?t=791 [4] http://inkscape.gitlab.io/inkscape/doxygen/namespaceInkscape_1_1GC.html http://inkscape.gitlab.io/inkscape/doxygen/namespaceInkscape...
- josephg 5y agoThankyou. I didn’t know about Inkscape, or anything about Common Lisp. Javascript (in V8) also has special detection & processing for very short lived objects. I appreciate the links! But I was specifically responding to / confused by this claim in the GP comment: > GC-based languages all ended up adding special syntax to add that level of control for objects when the programmer wants fine-grained control over the destructor. (Emphasis mine) And as you say, Java, V8 and Go do this with heuristics, not syntax. And I’m not sure if Ruby or Python do this sort of escape / generational analysis at all. On the flip side, it looks like Lua has added something like this, and C# has stack-allocated structs. Lua + C# + CommonLisp falls very short of the “all GC languages” claim.
- masklinn 5y ago> And I’m not sure if Ruby or Python do this sort of escape / generational analysis at all. It would almost certainly be implementation specific (as it is with js really, v8 is not a langage). I would assume pypy does escape analysis, I’m almost certain cpython does not.
- veltas 5y agoOkay I have edited the post so it does not say "all".
- steerablesafe 5y agoIf you replace "short-lived objects" with "short-lived resources" then Go has defer, Python has context managers.
- josephg 5y agoIs Go’s defer used to optimize the GC? I thought it was just syntax sugar to make sure people wouldn’t forget to close file handles and things like that..?
- steerablesafe 5y agoIt is for deterministically release resources based on scope (although Go's defer is tied to function-scope for reasons I can't understand). python's context manager is similar. Much like destructors in C++. You can probably say the same about heap allocated memory, but GC'd languages make the choice the non-deterministic release of heap allocations are fine. But there are resources where you can't play the same game (open files, connections, locks) and you need a way to deterministically close them.
- jatone 5y agodefer is tied to function scope because that is the usecase it was designed for. go also has finalizers executing right before deallocation.
- pjmlp 5y ago> The only GC language I can think of with special stack variables is C#. Are there others? Does Java do this now? There are tons of languages that allow that, even if many of them failed to gain mainstream adoption. - Mesa/Cedar (https://yahnd.com/theater/r/youtube/z_dt7NG38V4/ https://yahnd.com/theater/r/youtube/z_dt7NG38V4/) - CLU - Modula-2+ - Modula-3 - Oberon - Oberon-2 - Oberon-07 - Active Oberon - Component Pascal - Oberon-V - D - Nim - Swift (RC is a GC algorithm, chapter 5, https://gchandbook.org/ https://gchandbook.org/) - Eiffel - C++/CLI - Unreal C++ - Linear Haskell (getting there still WIP) - Pony I kept Nim and Swift for completion as you already mentioned it before.