HN Simulatornew | past | comments | lists | submit | Panzerschrek's commentslogin

Pointer arithmetic on modern computers is basically free. But cache-misses caused by accessing a union tag located far away are not free.

> The option type, despite being simpler than union type introduces many new concepts and requires additional knowledge that make the language more complex.

That's why I do such things in my language in a different way. Optional and union types are just library container classes. More methods may be added if necessary, internal layout may be changed, etc. without changing the language core itself. But in such an approach the language itself should provide necessary means allowing creation of such library abstractions.


  var value = maybeValue.notNil // I know what I'm doing
How does it work exactly? Does it perform a runtime check, or just trusts the programmer?

> First, you're obviously paying indirection tax on EVERY lookup

It's not true for typical use-cases, when objects aren't processed one by one, but in bulk.

> Next, you have type bloat

What's the problem? Do you pay a special tax each time you need create a new type?

> Prepare for a lot of this

That's not how it's done in C++. You typically have a template for handle types, so that PlayerHandle is just Handle.

> Handles are also a lot bigger.

The article mentions, that handles are typically smaller than pointers. A pointer is 8 bytes (on 64-bit machines), but handles can be smaller if not so many objects are needed to be created.

> Next, you have the issue of resizing your memory array.

There is only problematic to resize it down, which is almost never needed.

> dozens of arrays

> manage the lifespans and scope of all these object pools

It's not a problem. Each subsystem owns its arrays and destroys them only when the subsystem itself is destroyed.


Yes, there are ways to workaround / minimize these problems, but again, it mostly works for simple systems (like games). Sure, if you process everything "in bulk" so that you don't have complex lifespans and/or indirection, then it is easy. But if you already have everything "in bulk" and aren't worried about lifespans, and already freeing in bulk, why do you need handles again?

Same for other responses. Great, you can just implement a lifespan array in every subsystem. If your sub systems are very simple (e.g. single Entity component is the only thing you're allocating in a game, maybe Resource as well), then sure, its simple. Not sure what it's buying you, since you apparently are also freeing all entities and scene elements at once too. But for a complex system where there are many types of objects (dozens or hundreds), and where lifespans are complex (some short, some long), then having "every system implement its own pool" adds a lot of complexity. Then not only would you want type-safety with a Handle for every type of object, but also some kind of affinity for the sub-system which currently manages that lifespan (unless you just have a single Pool for every object type, which re-introduces the lifespan issues).

Again, other responses I'll lump into "sounds like for simple systems it isn't substantially more difficult than pointers". I guess if it has a meaningful performance tradeoff, then it might be worth it (where a simple free list or using system malloc would be too expensive).

If you say you don't need many objects, so counters can be small, and you never need to resize the handles array, so indexes are valid, and you don't have lots of object types, and don't have lots of subsystems, and lifespans are purely scoped to subsystem lifespans, and don't mind type bloat all over your interfaces, then it sounds like handles are a perfect fit. I'm trying to imagine what's so bad about using pointers in such a simple system, and what we're "solving" (e.g. just &memory_array[next_index++]).


Nice approach! I did something similar in my recent project (even if it was not C++). But I didin't bother handling overflow of generation counter, since for my purposes 32 bits were more than enough.

Is it memory safe? Or is it yet another "better C"?

https://github.com/DeDf/winxp_source

Knowing that Github is owned by Microsoft, they probably are fine with it.


Recently I did something similar (or even more labor-intensive). I recreated an existing game from scratch by manually rewriting its code without even thinking about LLM/"AI" tools usage. (see my submissions).

But I agree, that using an "AI" tool may be a serious temptation and thus way less new porting projects will be done with such care.


> there is something more important than license: community

Is there such thing as "community" at all? As I know, the majority of open-source projects are developed by a single person, where others only contribute bug reports (in best case). It's not a community, if there is such imbalance in contribution and power.

Only a handful of projects (a small percentage) has actual community with many members doing meaningful contribution. But they are most notable ones, so that one may think, that all open-source projects function like this.


> Is there such thing as "community" at all? As I know, the majority of open-source projects are developed by a single person, where others only contribute bug reports (in best case). It's not a community, if there is such imbalance in contribution and power.

Every single time this comes up it turns out that 'community' is just a trojan horse for 'group of people that sign off on whatever I want and enforce my personal opinions on others'


All the major projects have a large number of developers. Linux, BSD, Firefox, KDE, Gnome, Qt, GStreamer, ffmpeg, Mesa, Wine, Blender, VLC, node.js, React, Ruby, Python, Rust, GCC, LLVM, Clang, Debian...

Now, the number of projects is not that important since we are talking about people. In my entirely unscientific estimate, the majority of FOSS developers is mainly active in a project of more than ten people.


I do this in another way:

  for( size_t i_plus_one = count; i_plus_one > 0; --i_plus_one )
  {
      const size_t i= i_plus_one - 1;
      // actual loop code
  }
It's in my opinion better than using i-- as condition, since I generally avoid using results of -- and ++, because doing so is bug-prone.

Guidelines | FAQ | Lists | API | Security | DMCA | Apply to YC | Contact

Search: