Live data from Hacker News

Generics in C without void* or macros – enabled by psychec

github.com

31–40 of 74 posts

Re: Generics in C without void* or macros – enabled by psychec

#31

I don't really understand what the point of all this is, and why anyone would consider using this in a real-world application they're developing. There's already a C-like language which lets you write type-safe generic programs: it's called C++! And if for some reason you really want or need to restrict yourself to using C language features (almost) exclusively, you can disable RTTI, exceptions, and use a technique t…

No, I’m not going to drag in C++ and all of its baggage if all I need is some form of generics. It’s really not as simple as, “Just use C++!” If I truly need type-safe generics and C11 is an option, I’d rather go with that any day!

Re: Generics in C without void* or macros – enabled by psychec

#32

Earlier quoted context omitted.

...my compile times rarely take more than a minute, so I can't really relate to that, but exceptions will slow down execution and add an overhead to the binary's file-size.

Exceptions don't generally slow down execution -- they usually have zero runtime cost if they're not thrown making them better than error returns for performance. However, the exchange is, as you said, that they do add a lot of overhead to the file-size.

Something that generates extra code that ends up in the instruction cache during execution is not zero cost.

There's cost to transfer it through memory and cache hierarchies and there's cost when it causes the CPU to drop some other L1C cache line.

Re: Generics in C without void* or macros – enabled by psychec

#33
post #2

You should never typedef a pointer to an object. That’s common knowledge amongst experienced C developers and an immediate red flag, even for myself normally welcoming of anything built with Haskell.

I don’t see why not, as long as you make it clear what it is. E.g., if a C API is handing me an opaque handle that I need to pass around to the rest of the API, I don’t need to know that it’s a pointer (I’m not the one dereferencing it) and I’d rather not have to worry about that if I’m using the API properly.

Re: Generics in C without void* or macros – enabled by psychec

#35
post #32

Earlier quoted context omitted.

Exceptions don't generally slow down execution -- they usually have zero runtime cost if they're not thrown making them better than error returns for performance. However, the exchange is, as you said, that they do add a lot of overhead to the file-size.

Something that generates extra code that ends up in the instruction cache during execution is not zero cost. There's cost to transfer it through memory and cache hierarchies and there's cost when it causes the CPU to drop some other L1C cache line.

Well, if the exception is not thrown, no code related to the exceptional path goes into the instruction cache (not even checking for exception propagation). Assuming of course a non-completely dumb compiler that puts exceptional path code and unwinds tables in separate pages than hot code.

Re: Generics in C without void* or macros – enabled by psychec

#36

Earlier quoted context omitted.

Yes, that was confusing. "Look, you don't need to use these C-features if you use another language than C."

C++ 14, Haskell and Python. One of the reasons I'm actually going for pure C in this modern day and age, is that I can just write a small program, without having any other dependency. I'm sure the same could be said for C++, too, but for some reason, with my setup (GCC 5.1.0 with MinGW on Windows 7), I'm averaging around 800KB on C++ vs 80KB on C, for the (mostly) same code. EDIT: It appears that exception-handling i…

This needs to be made clearer in the readme: C++ and Haskell are requirements for building the type inference engine; Python is necessary for invoking the frontend (or the binaries can be downloaded).

Re: Generics in C without void* or macros – enabled by psychec

#37
post #28

Earlier quoted context omitted.

C++ 14, Haskell and Python. One of the reasons I'm actually going for pure C in this modern day and age, is that I can just write a small program, without having any other dependency. I'm sure the same could be said for C++, too, but for some reason, with my setup (GCC 5.1.0 with MinGW on Windows 7), I'm averaging around 800KB on C++ vs 80KB on C, for the (mostly) same code. EDIT: It appears that exception-handling i…

Are you sure it isn't debugging information that bloats the executable? Try a strip --strip-all your.exe and see if it is still that big.

> Try a strip and see if it is still that big.

...I guarantee that it is nothing short of humongous xD

Joking aside, maybe, I don't know, it's been a while since I used some C++ code, but I'll have a look into it the next time around.

...though it's probably unlikely, I use Code::Blocks and exclusively use the "Release" candidate, and uncheck the "Debug" version, and I'm quite confident the CB authors know what they are doing.

Re: Generics in C without void* or macros – enabled by psychec

#38

No void* or macros, but requires a special compiler frontend.

Sorry, it wasn't my intention to be tricky, that's why I added "enabled by psychec" to the subject line. I hope that, once landing at the project page, it should be obvious that such functionality is provided by a tool.

Re: Generics in C without void* or macros – enabled by psychec

#39
post #32

Earlier quoted context omitted.

Exceptions don't generally slow down execution -- they usually have zero runtime cost if they're not thrown making them better than error returns for performance. However, the exchange is, as you said, that they do add a lot of overhead to the file-size.

Something that generates extra code that ends up in the instruction cache during execution is not zero cost. There's cost to transfer it through memory and cache hierarchies and there's cost when it causes the CPU to drop some other L1C cache line.

Exceptions store the data to unwind the stack and run the handlers "out of band". Actually triggering the exception itself is expensive but that's an acceptable trade off as exceptions should only be used for exceptional situations.

Re: Generics in C without void* or macros – enabled by psychec

#40

Interesting link, but if anybody is interested in making real practical use of generics in a C-style language, then it's best to take a look at Zig.

Zig is not able to output c code that could be consumed by an alternative [optimized|verified/proofed] C compiler like Intel along with other questionable decisions such as "Zig does not support RAII or operator overloading because both make it very difficult to tell where function calls happen just by looking at a function body. Zig tends to avoid syntactic sugar except where it would have a significant effect on the semantics of a program. Zig does not have a C-style for loop, because you can just use the more general-purpose while loop instead with only a little bit of extra code. [1] The compiler is free to inline non-extern functions, change their parameters, and otherwise !!do whatever it wants!!, since they are internal functions." [2]

[1] https://ziglang.org/download/0.1.1/release-notes.html [2] https://andrewkelley.me/post/intro-to-zig.html

Post reply on HN