You don't need Generics in C
Vložit
- čas přidán 2. 06. 2024
- Previous Episodes: • Music Visualizer
References:
- Tsoding - Musializer - github.com/tsoding/musializer
- github.com/nothings/stb/blob/...
- nu11 - CZcams - / @nu11_ft
- nu11 - WIP Works 2016-2022 - / nu11-wip-works-2016-2022
- pilotredsun - Achievement (full album) - • pilotredsun - Achievem...
Support:
- BTC: bc1qj820dmeazpeq5pjn89mlh9lhws7ghs9v34x9v9
- Servers: zap-hosting.com/en/shop/donat...
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 - Věda a technologie
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.
Actually the simpler implementation of linear search is faster until at least 20 elements in most modern CPUs, because of the size of the cache line.
You wouldn't believe it. (not from theory, but do any sort of testing, this is a science after all)
Computer science teachers live in the 1990s , and they teach things like you're programming a pentium100.
Alas, all modern implementation of associative arrays are actually linear search until they grow big enough.
No one is going to create a freaking red-black tree to store less than a 100 elements , heap dispatching causes cache misses .
An array with linear search is faster for 100 elements than any tree, it loses to binary search and hash tables, but it's still unbelievably fast for small number of elements, you can literally get away with murder if you murder less than 100 elements (considering they're smaller structs, like 4 words)
I get heated by that.
@@monad_tcp It's strange to hate science 😅. But yeah, you're absolutely right, mate! By the way, there's a difference when you take an engineering approach that's tightly connected to practice rather than just applying theory like it's dogma. Sadly, I see this trend with some developers who are not flexible in their solutions. They only apply best practices..
@@malekith6522 "It's strange to hate science"
Say that to the theoretical programmers that don't actually do science. I love the science part, I also love the mathematics part. "but in theory" yeah, I know theory, it applies when you have 10.000 elements, when you have less than 1000 elements, hardware optimization and practical limits apply more.
Software engineering is a field that has a feet on mathematical theory and the other on practical science.
People that only do one or the other trip and fall.
Engineering is a field where you learn theory then you go practice to learn where to apply it.
Novice programmers and student out of the universities take all theory as gospel and apply it everywhere and make a lot of mess like little kids in a puddle of dirty
water.
I just laugh at programmers with 3 years in the field that think they're senior because HR gave them the "senior title", that doesn't means you know shit yet.
First go program for 5000h then come back. That will take 5 years of full time programming, but you are just out of the uni, you programmed for less than 300h and earned the senior title after 1 year because they just gave those automatically if you don't just quit after 1 year.
And I know you didn't do your programming assignments and just played games, don't lie.
I went to the uni and I know how it is.
@@monad_tcp This also concludes my observation. Seriously, I wouldn't dare to label myself as a senior software engineer with only 3 years of experience; I'd consider myself more of a mid-level professional.
I noticed this misalignment a lot during university, especially when studying DSP, Digital electronics or any other EE stuff. There was a noticeable difference between theoretical knowledge in the lab and the practical application when we attempted to filter real signals and obtain results and that how I started to think more like engenier.
gotta love turning c into ghetto c++ with macros
cool but kinda mess, i don't see a point why just not use c++ if you can
@@c7rsed118 Because C++ is not C with classes and generics
@@c7rsed118c++ cool but kinda mess, i don't see a point why just not use javascript if you can
"ghetto c++" got me lmao
@@guitarcat01 same
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, }, 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.
The 'proper' way to get generic support in C is to implement some kind of "metaprogramming" in your codebase, it essentially just does what languages that natively support generics do. However, it adds a ton of complexity to a codebase so is only really recommended for larger projects where the benefits outweigh the overhead
It wouldn't be too out of the question for a compiler to do that if it is inlining the function no?
#define DEFINE_ELEMENT(T) struct Element__##T { T t; }
#define ELEMENT(T) struct Element__##T
C does generic function selection via the standard _Generic macro. It just doesn’t do source code generation without a lot more macro metaprogamming work.
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.
linear lookup is actually faster than hash tables for low number of elements
Cache locality ftw
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 ;)
C only has 3 types : void, pointer and int
Just like python. Python has Nil, PyObject and double
>Also void* clearly makes C a dynamic language ;)
No, but this statement certainly ensures you're retarded.
It's like saying HTML is a programming language because it can be Turing Complete when used with CSS. HTML is not programming language simply because it was never made as a programming language.
Likewise, just because you can do some dynamic language stuff in C with void* doesn't mean C is a dynamic language. Next what? C++ is a functional programming language because of lambdas? No. Its main focus isn't lambda calc. Stop spreading bullshit everywhere.
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:)
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*).
But then you don't get 200 different specializations exploding your exe size
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.
chad void* user vs avg malding generic/template rust user
Came here for yet another AoC 2023. Not disappointed.
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.
In university we were always taught about how linked lists are often superior to dynamic arrays because ‘Big-O bro’. We ended up testing the two and found that in almost every single realistic benchmark (including prepending items to the front!), dynamic arrays won out. lol
linked list have a very niche use case. If for some reason you need to do a lot of insertion in the middle of the list and doing it sequentially or on elements that you have references to (remember, insertion in the middle of a list in a linked list is actually O(n) because you need to do a lookup first before inserting)
@@soniablanche5672 Insertion in the middle isn’t even better with a linked list than a dynamic array in most realistic situations though. We benchmarked this at my university and the dynamic array *still* beat the linked list in all cases unless the input sizes were massive
@@soniablanche5672 linked lists are a great data structure to combine with others, e.g. a linked list of arrays for very large arrays so that changing elements in the middle is much cheaper, but you still get most of the memory locality benefits by filling up entire cache lines between the indirections.
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.
i used to do this a lot when writing buffer encoders/decoders in Turbo Pascal
what about _Generic introduced in C11?
offsetof + sizeof macros are to the rescue when you do search in memory and do not know how struct is padded or something
C has reflection confirmed, where is your C++ now ?
I'm kidding
(Unrelated to the video) Are you going to be doing Advent of Code this year Tsoding?
i aint, bro it is too hard
iirc he said on one stream like 3 weeks ago that is probably not gonna do it.
What is advent of code
@@samuraijosh1595one programming challenge every day for the entirety of December. just google advent of code and check out its website
@@samuraijosh1595 Advent of Code is a yearly event where every day from December 1st to December 25th you can do a programming challenge
Lol that thumbnail matches perfectly with the title
Would You make a video to show us how to use emacs like You do, You are great !! And your content is amazing thanks
I haven't made the leap yet from C# to C but the similarity of Raylib to Monogame really helps me feel like I can
I started really getting into C two years ago after 6 years of C#. It's super worth it to learn C because of the fundamentals. When you go back to C# you will really start to question what it's doing under the hood. Questions like that will make you write way more efficient code.
C is sincerely a very simple and easy language. The C book is like 100 pages.
Amazing, just a thing:
1:15:22 you do have LoadTexture in raylib
Tsoding, what about intrusive hashtables?
I implement generics by using 'hacky' macros for types which gives me type safety in C. Using void* is my last resort.
This reminds me of "those aren't angle brackets, they're characters from the Canadian Aboriginal Syllabics block"
@@vytahsure, I can do generics
**runs sed**
Same. I have some single-header libraries I made for myself that implement macros to generate type-safe data structures like vectors, sets, hashmaps, etc. Really handy
c11 has generic keyword, which makes some parts slightly better
I have done the same using the mlib approach. It just ain't worth it though, to much hassle not to mention compiler error and static analyzers will give you harder to debug info
in the end, isn't a hashtable just like using a enum who's integer corresponding values you can use as index keys in a linear array? asking for a friend
Yes kind of, but it also handles the case where two keys hash to the same index.
always love the webdev bashing xD
What editor are u using ? Is this VIM or Emacs?
Imagine compile C sources with -std=C23....
How can you do hot reloading in java I thought about using the dynamicClassLoader but the GC doesn't seem to happen correctly sometimes.
V interesting.
Question, since i dont know much C: does endianness matter when trying to hop over the key string to get to the value? Or is that abstracted behind pointer arithmetic?
Basically is it guaranteed that the first 8 bytes will be the key?
The first 8 bytes will always be the key since it is the first field and 8 bytes. Endianness might affect the order of those 8 bytes.
Endianness affects the order of bytes in a field, not the order of fields in a struct
what compiler are you using
I was just wondering why you don't upgrade your computer. Why do you still use the old laptop.
Curious to know.
Using a slow computer keeps you based.
You might equally ask why upgrade, answer probably is "if it aint broke..."
He's poor
You appear to be a consumer. Do you have any *real* objections to his style or semantics?
"You don't generic in C"
Me when create data structures: make sense
Should you do aoc in porth ? Would be fun to see :D
!!! Enterprise warning !!!
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."
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.
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.
Same when people complain about endianness in serialization… name me one big endian machine
@@mattmurphy7030 network
I literally laughed out loud at the title + thumbnail combo
sure you can make it work in C with void* but it will be less performant bc of indirection and you have no compiler type safety to prevent you from shooting yourself in the foot
I don't think it's less performant because you can't pass a generic by value ( even if the language syntax says otherwise )
You have to copy a chunk of data, which requires indirection anyway, so you actually gained nothing.
This doesn't apply only to generics but to all structures ( that don't fit inside a register ).
You can also just use a tagged union with an enum, that way you don't lose whatever type safety that C does offer ( and if you use -Wall it will even warn you about not handling all possible enum values in a switch statement )
@@U20E0 you can pass generics by value in compiled languages because the compiler can generate the specific implementations without indirection at compile time for every time they were invoked with different types.
@@julians.2597 As i said, generocs don't even matter. Passing a structure by value and passing a copy of it on the stack by reference literally compiles to the exact same instructions. Instructions which closer resemble the latter
When Jai comes out publicly, do you think you'll fully switch to Jai or continue to work in C?
“When” jai comes out 😂😂😂
@@mattmurphy7030 I have faith!
When you are in doubt, use void*.
UX designers on suicide watch
Have you worked with large pages yet? Most programs don't know they exist, but most modern CPUs have a separate TLB for large pages, so its practically exclusive for any app using them(though java can use them). This mem accesses to them a bit faster. Also pretty sure you'd need a separate pagefile for them, otherwise they are basically locked to memory, which can be useful.
The 6 level associative TLB is a marvel to behold. It's hashtables all the way down.
const char *key = *(const char**)((char*)items + i*item_size);
Excuse my galactic ignorance, but what's the code editor you're using?
Emacs i think
@@plushrei5926 Thanks!
You meant ( 50:12 ) the Matt Godot Game Engine?
I only know Gal Gadot from the Wonder Woman Compiler Explorer!
Hehehe, maximale Verwirrung *g*
the editor you are using looks really nice, what is its name
Emacs
@@ruslansmirnov9006 Vim is better
it's called pico. I reccomend "boku no pico" fork.
50:40 as a native german speaker, i could resist myself from laughing lol
17:31 I'm sorry man, T_T
You should use the _Generic keyword instead of the macro for each function
c++ turning complete generics are bad obviously, but I really don't understand the issue with first-order generics? Just nicer syntax for a macro e.g.
#define VECTOR_DECL(T) \
struct Vector_##T { \
... \
};
vs if C had first-order generics
struct Vector {
....
};
much nicer syntax. It's C so you could still have the compiler generate uninitialised memory instead of using RAII or whatever. And still just write
Vector vector_new(int capacity) {
Vector v;
v.data = malloc(capacity * sizeof(T));
return v;
}
This is pretty much how I program C++ anyway, and I don't understand why it's an issue. void* indirection is not simpler imo.
Even simple generics in c++ cause it's name that is used by linker to get "mangeled", which means further incompatabilities between compilers and harder asembly interconnect.
@@ukyoize I get your point, but this is a design choice of the language and a fundamental limitation of linked libraries. Generics and dylibs are not compatible. Maybe a compiler that could monomorphize for binaries and transpile to void* for libs would be cool?
C++ didn’t actually need name mangling to do name overloading. It *could* have instead just generated new function names and then used something like the way _Generic works in C11 and maintained complete binary compatibility with C.
Name mangling was simply a poor design choice that would be hard to undo now. It’s not a good reason for not having first-order generics.
can you share discord server here please
discord link is on the twitch channel
I do
hello hello zozzing session
holy shit gf2 is cool
Bro I need oxygen mask to breathe
Will you ever make a course for newbeginners
i eat q-tips
void* goes brrrr
What is the point to have a stable build of rust if they force you to use nightly?
how exactly is c hot reloadable? cant find anything about this online and it does not seem to work in my testing
Check older streams
C is not inherently hot-reloadable, but most OSes allow you to dynamically load code using shared libraries. They are *.so on Linux.
And I don’t believe that you can’t find anything because when I search C hot reloading I immediately come to CZcams videos about it and a library that implements it
Hello!
where is the CLITERAL located?
In raylib.h
Lmao
Lol no generics
If you're going to bother with the performance, wouldn't it make sense to
- cut the common prefix of the path out of the keys (like ./resources/ is being compared for everything and you don't necessarily need support for resources outside of ./resources/, so they keys can omit it and then you can prepend ./resources/ to the keys if you need to load it)
- maybe use a trie instead of a hash table, since a trie only requires you to read the string as far as until it's distinct from all other strings in the catalog (rather than the whole thing for hashing). You lose out a bit of performance in terms of it being less cache friendly than just reading the string, since you need to chase a pointer for each character read, but it seems likely to be faster, or at least worth trying. Definitely much less of a pain than setting up perfect hashing.
I mean, real generics require name mangling
What about AI generation of code for each type you need? From some template in the comments or existing implementation for some specific type. Can it become the best way of doing generics in C?
Ok zoomer
how to become good problem solver in programming?
Practice. Solve 100 Leetcode problems and then you’ll know the answer to your question
so much echo
Porn folder 6 GiB too small????
C is too much work compared to C++.
C IS EVERYTHING
It's not my mother, nor your аss.
Data has types, not variables!
The amount of cope from stdlib reinventers in the comments is off the charts.
void* is a crime.
CS student for 5 years, spent 1000€ for my entire scholarship *laughs in french*
why are you so triggered
I'm a C lover don't get it wrong
but generics/templates are a gift from god
the complexity added by OOP/C++ is nearly satanic tho ;)
"but generics/templates are a gift from god"
It's just crap.
@@IBelieveInCode if seriously think it is a good idea, maybe we didn't find the right way do it now
Что это
a void pointer
музыкальный визуалайзер - музиалайзер
This content is not for Greeks.
@@samuraijosh1595 i am not greek
I don't think your videos are bad, I just wish you would trim them down to a more appropriate length. You lose most people watching after a minute or two at most. Personally, I watched until 15:00 and found that I was still wondering when you were going to get to the point. Best of luck on your channel.