the most important programming concept

Поділитися
Вставка
  • Опубліковано 21 лис 2024

КОМЕНТАРІ • 24

  • @Veeq7
    @Veeq7 Місяць тому +12

    Cache coherence is king, also neat to use linear/pool allocators whenever possible. I am somewhat fond of realizing I can do that in modern C++ anyways, while avoiding most of the oop nonesense.

    • @Veeq7
      @Veeq7 Місяць тому +1

      Also neat to sometimes leverage globals, const or not, for static storage. And stack too.

    • @Veeq7
      @Veeq7 Місяць тому

      Somehow I also became a huge fan of templates, something I once sought to destroy.

    • @DylanFalconer
      @DylanFalconer  Місяць тому

      It is really good for performance and I find it to be much easier to reason about

  • @BlainWeeks
    @BlainWeeks Місяць тому +5

    I know this is a toy example, but can't appending a node potentially cause all of the pointers to be invalidated if the dynamic array needs to allocate a bigger chunk of memory?

    • @frankhestvik9859
      @frankhestvik9859 Місяць тому +1

      Yes, unless Odin does something truly wild(!) behind the scenes like mmap a terabyte of virtual memory for every [dynamic] array or something. Even worse if the video author themselves didn't realize -- my guess is the author is mostly familiar with garbage collected environments, isn't used to working with pointers, and just want to encourage memory pooling or arena allocation in general and picked an unfortunate way to show it.
      Instead of returning pointers, one way to solve it would be to just return opaque IDs or something like that (indices into the array, for example) that act like Node pointers -- if the language provides the option to overload "dereferencing" (like in C++/Nim/Rust/etc., I don't know if Odin can do this).

    • @BlainWeeks
      @BlainWeeks Місяць тому

      @@frankhestvik9859 I imagine the video maker is very familiar with manual memory environments, but mapped an example that used an arena or bump allocator onto a dynamic array for simplicity of explanation.

    • @DylanFalconer
      @DylanFalconer  Місяць тому

      Yes, in that case, you can use ids rather than pointers, or make sure not to keep pointers across calls that may realloc, or use a larger preallocated pool, etc

  • @thepaulcraft957
    @thepaulcraft957 Місяць тому +3

    isn't what you showed basically a pool allocator? and what's the point of using a linked list when using vectors in the background anyways? (I'm just curious)

    • @tosemusername
      @tosemusername Місяць тому

      Maybe educational purposes, Idk.

    • @JTAArt
      @JTAArt Місяць тому +3

      You still keep the properties of why you want a linked list. It allows you to add and remove items faster, because you don't need to moving memory around. Arrays/Vector/dynamic arrays are just blocks of memory.

    • @theevilcottonball
      @theevilcottonball Місяць тому +2

      I am more of an array person myself, you often do not need to store pointers. But grouped lifetime thinking can also be applied to tree like structures (and if you are weird you can see a link list as a tree where every node only has at most one child). Linked lists have some advantages over array,s especially in the functional programming realm where they are a persistent data structure.

    • @DylanFalconer
      @DylanFalconer  Місяць тому

      Yeah it is basically a pool allocator
      The linked list example is just as an example - because that's something we learn very early and it's usually the individually allocated elements way
      That said, you can still back a linked list with a pool (probably different to this one so you don't get reallocations) and get decent cache locality while maintaining characteristics you want from the list (->next, ->prev)
      Why you'd want that just depends on what you are working on

    • @DylanFalconer
      @DylanFalconer  Місяць тому

      Same, in practice I usually use arrays for most things unless there's some good reason not to

  • @screamodev
    @screamodev Місяць тому +7

    you might be the only newsletter i subscribed to that i actually read. Amazing articles and video explanations

  • @ismbks
    @ismbks Місяць тому +1

    i have been playing with arena allocators in C for a month now, i even built some data structures on top of them like "vectors" but i am starting to feel like these types of allocators are mostly powerful for game development and not so much systems programming (which is what i mainly do)
    i say this because i only hear game developers talking about this, and from my little experience im not entirely sold on them right now, maybe i'm just using them badly but i feel like there is some mental overhead and rewriting with this paradigm

    • @DylanFalconer
      @DylanFalconer  Місяць тому +2

      It's interesting that you say that because I feel the exact opposite way
      Once I started thinking in groups, it became much easier to program
      What do you usually do in systems programming?

    • @ismbks
      @ismbks Місяць тому

      @@DylanFalconer i'm a student taking a class on low level programming in C in which we are asked to frequently write console applications for linux/unix systems, i just came back to this video because today i received an assignment in which grouped lifetime thinking is probably the best way to solve my problem efficiently.
      so i guess i was totally wrong because i actually did exactly what you described in your video, we had to implement a kind of stack that rotates and in which you had to pop and push elements sometimes. i used a memory pool with a free list to keep track of which elements were popped so i can reuse their slots and it worked wonderfully!

  • @hampuseden9402
    @hampuseden9402 Місяць тому +1

    What is this language?

  • @IgorStojkovic
    @IgorStojkovic Місяць тому +3

    This also explains why Rust's philosophy is not so great since it is focused on managing individual lifetimes.

    • @frankhestvik9859
      @frankhestvik9859 Місяць тому +7

      This seems nonsensical or a misunderstanding. The "individual" in "individual lifetimes" that Rust focuses on is a completely different concept than the "individual malloc()/free() calls can cause fragmentation"; that's like conflating the size of a for loop in the source with the number of iterations during run-time. In practice it might actually be the opposite: Rust's type system is such a pedantic pain in the ass that people end up "giving up" on their (usually bad) linked list/heap-node strategies and instead just go back to using a vector/ECS/data-oriented approach, e.g. backed by vector storage or an arena/pool allocator, which tend to be much simpler to do in Rust -- and which is exactly what this video seemingly would argue for... I mean this is why I don't like Rust either, but to say it doesn't encourage data-oriented design seems wrong.
      The main problem you would have in Rust with this code tho is that it would (correctly) tell you that what you're doing is bullshit because you're returning pointers into a seemingly dynamically allocated array that may move around on the heap (unless Odin does something truly wild like map every dynamic array to its own terabyte of virtual memory).

  • @GenericInternetter
    @GenericInternetter 28 днів тому

    i'm 4 minutes in and you still haven't got to the point