You don't need Generics in C

preview_player
Показать описание

References:

Support:
- BTC: bc1qj820dmeazpeq5pjn89mlh9lhws7ghs9v34x9v9

Chapters:
- 0:00:00 - Announcement
- 0:00:55 - Demo
- 0:02:04 - Hotreloading
- 0:06:26 - Asset Manager
- 0:12:19 - struct Assets
- 0:14:50 - struct Images
- 0:17:31 - CS Students are Shaking and Crying
- 0:20:17 - CS Education
- 0:23:00 - images_find()
- 0:23:59 - Generalization
- 0:25:26 - Generics in C
- 0:28:27 - Padding
- 0:30:54 - assoc_find()
- 0:36:27 - Rust and C++ are Faster than C
- 0:37:12 - But C could be as fast as Rust and C++
- 0:39:50 - It's not About the Speed
- 0:40:45 - assoc.c
- 0:48:48 - decoupling from nob
- 0:50:14 - Godbolt
- 0:52:03 - Debugger
- 0:55:10 - Summary
- 0:57:24 - stb_ds.h
- 0:58:45 - We are just having fun
- 0:59:24 - offsetof that accepts values
- 1:00:27 - Macro Arguments
- 1:01:41 - Computing Offset Manually
- 1:03:03 - typeof
- 1:03:20 - C23
- 1:04:30 - AI Help
- 1:04:53 - Final assoc_find()
- 1:07:12 - Duck Typey Structures
- 1:09:32 - Integrating assoc_find() back to plug.c
- 1:11:26 - assets_image()
- 1:15:02 - assets_texture()
- 1:17:58 - assets_fonts()
- 1:18:59 - Integrating the Assets Manager
- 1:23:10 - Loading New Texture
- 1:26:22 - assets_unload_everything()
- 1:29:19 - Updating Already Loaded Texture
- 1:32:35 - Hotreloading Icons
- 1:33:43 - Summary
- 1:34:44 - Limitations
- 1:35:32 - Reloading Textures One More Time
- 1:36:19 - C is all you need
- 1:37:17 - Outro
Рекомендации по теме
Комментарии
Автор

I'm not a computer science student but a former electrical engineering student. In university, our professors, as you mentioned, presented it as a faster algorithm, claiming it's better than linear, etc. They emphasized learning different approaches, like bubble sort, to understand various methods. However, only at work did I learn that bubble sort or linear algo is not entirely useless. It can be used for certain projects, especially in specific conditions and for small input sizes. This is due to its simpler implementation, making it suitable for non-critical roles. If a more efficient approach is needed in the future, we can always make that change. Nonetheless, using simpler algorithms in certain scenarios can result in faster development and better code readability. By the way, this argument always can trigger some CS full-stack engineers :D.

malekith
Автор

gotta love turning c into ghetto c++ with macros

not_herobrine
Автор

Another reason that generics can be more performant is that they don't require indirection. Doesn't apply here, because you're dealing with arrays anyways. But where in a language with generics you can have a type Element<T> { t: T, }, in C it'd have to be Element { void *ptr_to_t; } that you need to dereference (which I'd be surprised if even the most aggressive compiler could optimize that away). The upshot is that you get automatic dynamic dispatch.

isaactfa
Автор

I have to commend you for the educational way your are teaching. There is not a lot of people with the amount knowledge that you posses that could teach it in such a straight forward manner. For that I amend you. Great work.

todsalomonssonsegerby
Автор

assoc_find demonstrates the reason that in C++ when storing mixed objects in one array you use pointers to the objects. By using pointers, sizeof(void*) == sizeof(anyobject*).

mechaelbrun-kestler
Автор

51:00 - C compiler actually unrolled loop, and within it every iteration it gets qword (long) pointer at +8 and then dword (int) pointer at +16 (long pointer + size of qword which is 8) but next element starts at +14 for some reason (maybe padding of 4 bytes idk)

GhostVlVin
Автор

There's a difference between "slow" and "not the fastest", and if its fast enough for a given purpose, "not the fastest" is just irrelevant, especially if there are other upsides (ex-python user here).

Also void* clearly makes C a dynamic language ;)

johanngambolputty
Автор

linear lookup is actually faster than hash tables for low number of elements

soniablanche
Автор

Great stuff, thank you for the video Tsoder!:)
I usually do this kind of stuff as a virtual filesystem. The gzipped(or else) resource pack file (no Zoomer here! hehe) is loaded, the archive file table is created. Later you can lazy-load/request a file/resource, that is also cached for subsequent requests. Additionally the physical filesystem (executable + something like "resource" folder structure, "images", "fonts", etc. you get it) is also mapped onto the virtual filesystem and has preference over the virtual filesystem. This is very convenient when developing, as you can edit and test new resources in-place under the folder tree structure without having to rebuild your resource archive. And to hit two birds with one stone, this may also be used later as a basis for a plugin system or additions and resources that the user may provide. Even patches or updates are deployable this way.

Oh and because I am a coward, this lady here programs all the mentioned stuff in C++:P So I am very excited to see what you are brewing in your C-witchcraft-kitchen, hehehe:)

dieSpinnt
Автор

CS student here: I trust my professor not at all (I trust no one generally -- personal experience is a much better teacher than "that nerd told me so, " mostly because nerds are horrendous at communication), but he knows and teaches that arrays are faster than linked lists almost no matter what. Benefit of a small school, the webdev prof is the same guy as the DSA prof, and he happens to be a big fan of measuring real-world performance. So when he makes his DSA students implement a radix sort (in C++, no less!), you can bet that he'll make them implement it on both a linked list and a linear array and MEASURE the difference over a few million elements on their own machine. Doing that sort of thing disabuses starry-eyed students of the notion that linked lists are faster than arrays for almost any reason, and keeps him sane in webdev class. I've _never_ heard him make the first mention of performance in webdev -- and if I asked about it, he'd probably tell me that I'm in the wrong class to be asking about that sort of thing.

mage
Автор

30:12 you could explicitly set padding values to zero in memory allocation. then even if you include the padding in value comparison, it wouldn't matter because both item would have padding with value of 0.

sinasalahshour
Автор

There is theshold when hash maps/dictionaries become faster than linear arrays, but your data set must be thousands of items or maybe even slightly more. If your data set has maybe 100 items linear arrays will always be faster, because they benefit from CPU cache locality.

Better yet, implement this yourself and mesure which method is faster for your particular case. Language doesn't matter.

wiktorwektor
Автор

I implement generics by using 'hacky' macros for types which gives me type safety in C. Using void* is my last resort.

ronaldomorillo
Автор

chad void* user vs avg malding generic/template rust user

anon-fzbo
Автор

1:08:32 it's inheritance, you invented C++, now you just add a pointer to methods and you have a VTable.

One can even say it's "structurally typed" to make TS people mad.

monad_tcp
Автор

29:43 it's gonna be fine.
You don't need to support processors that need padding between a single 8 byte pointer and don't align on 4 byte boundary.
No one uses any IBM mainframe here.

monad_tcp
Автор

19:15 I just burst out laughing. Psychological therapy for High Languages Programmers. "It is fine. It is fine." And I also see an astronaut in a 50's hard sci-fim novel flying through space with earphones: "It is fine. It is fine."

hadeseye
Автор

offsetof + sizeof macros are to the rescue when you do search in memory and do not know how struct is padded or something

rogo
Автор

34:11 pointer arithmetics<-> templates

SphereofTime
Автор

Came here for yet another AoC 2023. Not disappointed.

jaumeguimeramarquez