News for hackers. Stuff that matters.
Re:Boehm-Demers-Weiser conservative C/C++ Garbage Collector by yxhuvud on Sunday January 21, 2024 @08:49AM (#39078396)
Another big reason is the lack of value types in languages like Java. Modern languages with a GC tend to have those, and where it makes sense to use them they help, a lot.
Re:Boehm-Demers-Weiser conservative C/C++ Garbage Collector by amelius on Sunday January 21, 2024 @11:34AM (#39079948)
What are "value types"?
Re:Boehm-Demers-Weiser conservative C/C++ Garbage Collector by jstimpfle on Sunday January 21, 2024 @11:59AM (#39080202)
If you nest an object within another object you often want to embed the child directly in its parent, sharing the memory and avoiding the indirection that you get if you nest with a reference as is the typical approach with GC languages. Also embedding makes it clear that it's a 1:1 relation, i.e. non-optional, fixed identity etc.

AFAICT few GC languages allow you to do this, basically you have to buy into indirection and end up with webs of objects.

This is quite cache unfriendly, but the bigger issue for me is that it makes the code less clear and requires additional complexities since you can't just jump from a child to its parent by subtracting an offset from its address.

Re:Boehm-Demers-Weiser conservative C/C++ Garbage Collector by ghusbands on Sunday January 21, 2024 @12:28PM (#39080473)
> makes the code less clear and requires additional complexities since you can't just jump from a child to its parent by subtracting an offset from its address.

Wait, so you're saying that code that subtracts an offset from a child node to get to a parent node is clearer and less complex than alternatives?

Re:Boehm-Demers-Weiser conservative C/C++ Garbage Collector by jstimpfle on Sunday January 21, 2024 @12:55PM (#39080772)
Not having the address relationship requires additional complexities. To get from the child to the parent, you need to spend an additional reference field, and need to keep it consistent with the other reference. If you embed directly, there's nothing in the relationship of the objects' identities that could ever get consistent. It's a strong guarantee.

How do you get from the child to the parent in, say Java? And how do you tell that a child is never swapped out?

How do you copy an object that holds, say 4 vec4, vec4 being a struct that itself 4 holds floats? With value types, say in C, it's 1 memcpy and you're done.

Re:Boehm-Demers-Weiser conservative C/C++ Garbage Collector by ghusbands on Monday January 22, 2024 @06:58AM (#39088538)
In a particularly constrained and archaic language like C, it may be clearer. In a modern language, in no way is subtraction of an offset clearer and simpler than almost any alternative. Even in C, as you've mentioned elsewhere, you've had to use macros to make it at all clear, and macros can do anything and are inherently opaque.

Many other languages also have newer/different approaches to solve some of these problems, be they subclassing, generics, macros, JITs, or other. They mostly cannot do this as efficiently, but that seems less like a clarity/complexity concern and more like a low-level-programming concern.

> How do you get from the child to the parent in, say Java?

In Java, you do indeed have to embed mutual pointers and just be careful. As a C user, you must be used to being careful. Even C#, which does allow structs, doesn't have references-to-members that can be turned into references-to-owners. However, there are patterns and language features that people use to handle similar things, like subclassing (for lists) and generics.

> How do you copy an object that holds, say 4 vec4, vec4 being a struct that itself 4 holds floats?

Copying structs as a block works fine in Rust, C++ and C#, along with many other languages. Again, Java is a little more limited in this regard, though I believe they are working on adding value types.

Re:Boehm-Demers-Weiser conservative C/C++ Garbage Collector by jstimpfle on Monday January 22, 2024 @02:22PM (#39094090)
I think we have quite different ideas what complex means. You seem to see a language (C or machine code), which, while based on simple principles, is complex in its behavior, especially if something goes wrong (which should not happen in the first place).

I see literally a subtraction operation versus a huge memory safe language framework set up over the same machine, plus a whole lot more user facing surface level source code, just to achieve the same thing.

Re:Boehm-Demers-Weiser conservative C/C++ Garbage Collector by ghusbands on Monday January 22, 2024 @06:12PM (#39096901)
Sure, C and machine code can be simple (or simplistic) but in a world in which 70% of severe security vulnerabilities are caused by memory safety issues, I'll generally prefer languages that are safe against them, even if I can't then use text macros to get from a child pointer to a parent pointer. But that's off-topic, really.

In the message I originally replied to, I guess it's only the "since you can't ..." part that I really object to, as it's never going to be clearer to use &(par->child) and CONTAINER_OF(ch, some_type, child) than to use par.child and ch.parent.

Re:Boehm-Demers-Weiser conservative C/C++ Garbage Collector by groestl on Sunday January 21, 2024 @12:49PM (#39080705)
> Wait, so you're saying that code that subtracts an offset from a child node to get to a parent node is clearer and less complex than alternatives?

Parent made me laugh out loud on this.

Re:Boehm-Demers-Weiser conservative C/C++ Garbage Collector by jstimpfle on Sunday January 21, 2024 @12:58PM (#39080805)
I hope you've stopped laughing and can read and understand my answer.
Re:Boehm-Demers-Weiser conservative C/C++ Garbage Collector by groestl on Sunday January 21, 2024 @01:14PM (#39080998)
Please, I did not want to offend you personally. It just took me by surprise. I understand your points, even though I'm with your direct reply, all things considered.
Re:Boehm-Demers-Weiser conservative C/C++ Garbage Collector by jstimpfle on Sunday January 21, 2024 @05:35PM (#39083790)
The address offset subtraction thing specifically is used quite a bit in the Linux kernel for example. It allows to combine abstract code with concrete data. For example they define a red-black tree as a graph of just plain nodes. These nodes are totally abstract, they contain only node pointers but no useful data. The code is totally abstract, no need to instanciate anything a dozen times with different offsets and such. You simply wrap the abstract nodes with whatever concrete useful data you fancy.

The same approach works for lists and a variety of other data structures.

You can get from the abstract link node to the containing struct with a macro: CONTAINER_OF(ptr, containertype, membername). It's easy to use and unlikely to get it wrong.

Re:Boehm-Demers-Weiser conservative C/C++ Garbage Collector by greesil on Sunday January 21, 2024 @11:51AM (#39080124)
In Java, primitives. int, boolean, float. Allocated on the stack, or the heap along with the object it is a part of.
Re:Boehm-Demers-Weiser conservative C/C++ Garbage Collector by pjmlp on Monday January 22, 2024 @03:46AM (#39087593)
Note about "modern languages with GC", this was already a thing in Common Lisp, Eiffel, Modula-2+, Modula-3, Mesa/Cedar, Oberon (and its linage),...

Java's mistake was ignoring them, as its was designed as set-top box programming language as Oak, pivoted to applets, and then pushed all over the place.

Re:Boehm-Demers-Weiser conservative C/C++ Garbage Collector by foldr on Monday January 22, 2024 @05:05AM (#39087963)
The poster is talking about the subset of modern languages that have GC. They're not suggesting that only modern languages have GC.
Re:Boehm-Demers-Weiser conservative C/C++ Garbage Collector by pjmlp on Monday January 22, 2024 @08:04AM (#39089060)
> "Another big reason is the lack of value types in languages like Java. Modern languages with a GC tend to have those, and where it makes sense to use them they help, a lot."

Might be interpreted as older languages with GC did not had value types, which is bluntly false, going back to 1970's.

My remark was to make clear this isn't something new and modern, rather a design mistake in languages like Java, that is now trying really hard to fix it, while others designed after it (like C#), thankfully didn't follow the same trap.

How modern a 2001 language (C#), using ideas from Mesa/Cedar (1985), is an exercise in the semantics of "modern".

Re:Boehm-Demers-Weiser conservative C/C++ Garbage Collector by foldr on Monday January 22, 2024 @11:00AM (#39090889)
>Might be interpreted as older languages with GC did not had value types, which is bluntly false, going back to 1970's.

Ok, but you included Common Lisp in your list, which doesn't really have value types. (E.g. you cannot make an unboxed array of structs.)

Re:Boehm-Demers-Weiser conservative C/C++ Garbage Collector by pjmlp on Monday January 22, 2024 @11:16AM (#39091189)
No, but you can have an array of primitive values, instead of an array of pointers.
Re:Boehm-Demers-Weiser conservative C/C++ Garbage Collector by foldr on Monday January 22, 2024 @11:21AM (#39091258)
You can do that in Java too!
Re:Boehm-Demers-Weiser conservative C/C++ Garbage Collector by pjmlp on Monday January 22, 2024 @11:25AM (#39091319)
Take your brownies, I guess, that wasn't my point.

As if me mis-remembering Common Lisp capabilities, invalidates the rest regarding "modern" GC languages.

Here is an array of structs in Oberon, 1987,

    TYPE
      Point = RECORD
        x, y : INTEGER
      END;

      Triagle = RECORD
        v1, v2, v3: Point
      END;

      Data = ARRAY 100 OF Triagle; (* static sized *)

      DynData  = ARRAY OF Triagle; (* dynamically sized *)

    VAR
       dynData : DynData; (* Allocated via NEW(dynData, elems)*)
Re:Boehm-Demers-Weiser conservative C/C++ Garbage Collector by foldr on Monday January 22, 2024 @11:42AM (#39091564)
No, it doesn't invalidate the rest of your comment. It just made it harder to figure out what point you were making in your original post (i.e. what the this referred to).

There's a little bit of irony here in how you react to people picking at details in your own posts, when your original post is precisely the same kind of pedantry!