Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pktbuf: optimize size overhead in packet buffer #1959

Merged
merged 1 commit into from
Nov 7, 2014

Conversation

miri64
Copy link
Member

@miri64 miri64 commented Nov 7, 2014

As long as PKTBUF_SIZE remains under a certain value, the bit-size of the size parameter of packets can be smaller and thus saving a few byte of loss in the packet buffer due to overhead.

On most platforms every packet now needs at least 2 byte less of pktbuf space, reducing the overhead from 9 byte to 7.

@miri64 miri64 added the Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation label Nov 7, 2014
@miri64 miri64 added this to the Release NEXT MAJOR milestone Nov 7, 2014
@Kijewski
Copy link
Contributor

Kijewski commented Nov 7, 2014

If you want to safe bits:

#if PKTBUF_SIZE < (1ull << 7)
typedef uint8_t _pktsize_t;
#elif PKTBUF_SIZE < (1ull << 15)
typedef uint16_t _pktsize_t;
#elif PKTBUF_SIZE < (1ull << 31)
typedef uint32_t _pktsize_t;
#elif PKTBUF_SIZE < (1ull << 63)
typedef uint64_t _pktsize_t;
#endif

typedef struct __attribute__((packed)) _packet {
     volatile struct _packet *next;
     _pktsize_t _size;
} _packet_t;

static inline int packet_get_processing(const _packet_t *p) {
     return p->_size & 1;
}

static inline _pktsize_t packet_get_size(const _packet_t *p) {
     return p->_size >> 1;
}

static inline void packet_set_size(_packet_t *p, _pktsize_t size) {
     p->_size = (size << 1) | (p->_size & 1);
}

static inline void packet_set_processing(_packet_t *p, int processing) {
     p->_size = (p->_size >> 1 << 1) | !!processing;
}

static inline void packet_set_size_and_processing(_packet_t *p, _pktsize_t size, int processing) {
     p->_size = (size << 1) | !!processing;
}

@miri64
Copy link
Member Author

miri64 commented Nov 7, 2014

First of all: uint64_t for size just isn't reasonable, second of all: yes, but that would require also some heavy rewriting of the whole module, for what I currently do not have the nerves ;-)

@miri64
Copy link
Member Author

miri64 commented Nov 7, 2014

Furthermore, I still don't know if it might be a good idea to make size public (but not processing), having the model in mind

@miri64
Copy link
Member Author

miri64 commented Nov 7, 2014

Furthermore 2, your code implies, that processing is boolean, which it isn't. It's a [edit]semaphoric[/edit] counter.

@OlegHahm
Copy link
Member

OlegHahm commented Nov 7, 2014

ACK - 2 bytes are better than nothing.

miri64 added a commit that referenced this pull request Nov 7, 2014
pktbuf: optimize size overhead in packet buffer
@miri64 miri64 merged commit 5f81de3 into RIOT-OS:master Nov 7, 2014
@miri64 miri64 deleted the pktbuf-opt branch November 7, 2014 23:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants