Live data from Hacker News

Stop using 'short' for line and allocation sizes (2013)

git.kernel.org

21–30 of 50 posts

Re: Stop using 'short' for line and allocation sizes (2013)

#21
post #14

Why not fixed-size ints - uint8_t, etc. from stdint.h? I've not been using C for a while, so I wonder what the viewpoints are regarding this.

Best of both worlds: uint_fast8_t / uint_least8_t gives you at least 256 discrete values and likely a native/optimal-register-width allocation.

Re: Stop using 'short' for line and allocation sizes (2013)

#23

Stupid question: why not unsigned int?

In my experience with languages that have both signed and unsigned ints, but have no overflow or underflow protection (which is pretty much all of them), you really want to use signed ints so that you can write assertions that numbers you expect to be positive are positive. You can't do that with unsigned ints since they are all by definition >= 0, so you always end up finding bugs where suddenly you've got MAX_INT - 34 floating around in your program horking things up with no easy ability to tell there's a problem early.

A uint that actually threw an exception or something if you tried to underflow it would be useful, but most unsigned ints nowadays aren't all that useful.

Re: Stop using 'short' for line and allocation sizes (2013)

#24
post #14

Why not fixed-size ints - uint8_t, etc. from stdint.h? I've not been using C for a while, so I wonder what the viewpoints are regarding this.

You typically want to use the native integer size for something like this, for the code to be more portable. The native integer size should really be int, but it's not defined as such in the standard. My preference at the moment: off_t for file offsets ptrdiff_t for memory offsets (vs. size_t for unsigned) int for return flags A problem with this is that printf does not have sizes to match these.

But then you get different behavior depending on the platform, which doesn't seem any more portable to me.

Re: Stop using 'short' for line and allocation sizes (2013)

#25
For structs where there are only a dozen or so in memory at any one time, this makes sense.

For structs where there are thousands, or millions, in memory using the smallest type required to get the job done will improve performance because you will fit more data into the cache.

Re: Stop using 'short' for line and allocation sizes (2013)

#26

For structs where there are only a dozen or so in memory at any one time, this makes sense. For structs where there are thousands, or millions, in memory using the smallest type required to get the job done will improve performance because you will fit more data into the cache.

Only if makes the overall structure actually becomes smaller. A bunch of these shorts were surrounded by fields whose alignment means that changing to int results in no increase in the overall size of the structure. In fact, I think some of them actually got smaller due to reordering the fields to be more alignment-friendly.

Re: Stop using 'short' for line and allocation sizes (2013)

#27
post #24

Earlier quoted context omitted.

You typically want to use the native integer size for something like this, for the code to be more portable. The native integer size should really be int, but it's not defined as such in the standard. My preference at the moment: off_t for file offsets ptrdiff_t for memory offsets (vs. size_t for unsigned) int for return flags A problem with this is that printf does not have sizes to match these.

But then you get different behavior depending on the platform, which doesn't seem any more portable to me.

I'm in the "never use fixed size integers unless needed"-camp. For example, if you write code that's supposed to run on an 8-bit computer (something embedded) as well as your 64-bit desktop, it makes no sense to limit yourself. For example, say that you know that your number will be between 1-200, you could use uint8_t. But why not use int? It will be the native width of the platform, and likely to be faster. Calculations on an 8-bit int can be slower than 64-bit ints on your 64-bit CPU, because the CPU might have to mask out the relevant part of the register to only operate on your 8 bits. And you don't gain anything by writing uint8_t, you still need to occupy a register on your CPU (which will be 64-bit). The behavior of your program will be the same on both platforms.

Re: Stop using 'short' for line and allocation sizes (2013)

#28
post #26

For structs where there are only a dozen or so in memory at any one time, this makes sense. For structs where there are thousands, or millions, in memory using the smallest type required to get the job done will improve performance because you will fit more data into the cache.

Only if makes the overall structure actually becomes smaller. A bunch of these shorts were surrounded by fields whose alignment means that changing to int results in no increase in the overall size of the structure. In fact, I think some of them actually got smaller due to reordering the fields to be more alignment-friendly.

Struct packing is something you can control at the compiler level. In gcc, you can add __attribute__((__packed__)) to your structs.

Re: Stop using 'short' for line and allocation sizes (2013)

#29
post #26

Earlier quoted context omitted.

Only if makes the overall structure actually becomes smaller. A bunch of these shorts were surrounded by fields whose alignment means that changing to int results in no increase in the overall size of the structure. In fact, I think some of them actually got smaller due to reordering the fields to be more alignment-friendly.

Struct packing is something you can control at the compiler level. In gcc, you can add __attribute__((__packed__)) to your structs.

That can add a performance penalty. If it didn't, compilers would always pack tightly, after all.

In any case, these particular structs don't have any special packing applied.

Re: Stop using 'short' for line and allocation sizes (2013)

#30
post #24

Earlier quoted context omitted.

But then you get different behavior depending on the platform, which doesn't seem any more portable to me.

I'm in the "never use fixed size integers unless needed"-camp. For example, if you write code that's supposed to run on an 8-bit computer (something embedded) as well as your 64-bit desktop, it makes no sense to limit yourself. For example, say that you know that your number will be between 1-200, you could use uint8_t. But why not use int? It will be the native width of the platform, and likely to be faster. Calcula…

In practice, how much of my code is going to be run on an 8-bit CPU? Zero. I'd rather have the clarity and consistent behavior of explicitly typed ints than pre-optimize for something that will almost certainly never happen.
Post reply on HN