For small lengths (1, 2, 4 and 8 bytes) use shorter code paths, this
increases performance of function by around 40%.
Signed-off-by: Piotr Dałek <piotr.dalek@ts.fujitsu.com>
unsigned buffer::ptr::raw_length() const { assert(_raw); return _raw->len; }
int buffer::ptr::raw_nref() const { assert(_raw); return _raw->nref.read(); }
+ void buffer::ptr::copy_out(unsigned o, unsigned l, char *dest) const {
+ assert(_raw);
+ if (o+l > _len)
+ throw end_of_buffer();
+ char* src = _raw->data + _off + o;
+ if (l > 8) {
+ memcpy(dest, src, l);
+ return;
+ }
+ switch (l) {
+ case 8:
+ *((uint64_t*)(dest)) = *((uint64_t*)(src));
+ return;
+ case 4:
+ *((uint32_t*)(dest)) = *((uint32_t*)(src));
+ return;
+ case 3:
+ *((uint16_t*)(dest)) = *((uint16_t*)(src));
+ *((uint8_t*)(dest+2)) = *((uint8_t*)(src+2));
+ return;
+ case 2:
+ *((uint16_t*)(dest)) = *((uint16_t*)(src));
+ return;
+ case 1:
+ *((uint8_t*)(dest)) = *((uint8_t*)(src));
+ return;
+ default:
+ memcpy(dest, src, l);
+ return;
+ }
+ }
+
unsigned buffer::ptr::wasted()
{
assert(_raw);
unsigned raw_length() const;
int raw_nref() const;
- void copy_out(unsigned o, unsigned l, char *dest) const {
- assert(_raw);
- if (!((o <= _len) && (o+l <= _len)))
- throw end_of_buffer();
- memcpy(dest, c_str()+o, l);
- }
+ void copy_out(unsigned o, unsigned l, char *dest) const;
bool can_zero_copy() const;
int zero_copy_to_fd(int fd, int64_t *offset) const;