Skip to content

Commit 809ce8b

Browse files
committed
Remove cast_block from msgpack_buffer
It's only used by the unpacking loop, and can perfectly be allocated on the stack, no reason to add an extra 8B in every buffer struct for it.
1 parent 9bac145 commit 809ce8b

File tree

2 files changed

+52
-63
lines changed

2 files changed

+52
-63
lines changed

ext/msgpack/buffer.h

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,6 @@ struct msgpack_buffer_chunk_t {
8181
bool rmem;
8282
};
8383

84-
union msgpack_buffer_cast_block_t {
85-
char buffer[8];
86-
uint8_t u8;
87-
uint16_t u16;
88-
uint32_t u32;
89-
uint64_t u64;
90-
int8_t i8;
91-
int16_t i16;
92-
int32_t i32;
93-
int64_t i64;
94-
float f;
95-
double d;
96-
};
97-
9884
struct msgpack_buffer_t {
9985
char* read_buffer;
10086
char* tail_buffer_end;
@@ -107,8 +93,6 @@ struct msgpack_buffer_t {
10793
char* rmem_end;
10894
void** rmem_owner;
10995

110-
union msgpack_buffer_cast_block_t cast_block;
111-
11296
VALUE io;
11397
VALUE io_buffer;
11498
ID io_write_all_method;
@@ -383,14 +367,6 @@ static inline size_t msgpack_buffer_skip_nonblock(msgpack_buffer_t* b, size_t le
383367
return length;
384368
}
385369

386-
static inline union msgpack_buffer_cast_block_t* msgpack_buffer_read_cast_block(msgpack_buffer_t* b, size_t n)
387-
{
388-
if(!msgpack_buffer_read_all(b, b->cast_block.buffer, n)) {
389-
return NULL;
390-
}
391-
return &b->cast_block;
392-
}
393-
394370
size_t msgpack_buffer_read_to_string_nonblock(msgpack_buffer_t* b, VALUE string, size_t length);
395371

396372
static inline size_t msgpack_buffer_read_to_string(msgpack_buffer_t* b, VALUE string, size_t length)

ext/msgpack/unpacker.c

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,23 @@ static inline bool msgpack_unpacker_stack_is_empty(msgpack_unpacker_t* uk)
279279

280280
#endif
281281

282+
union msgpack_buffer_cast_block_t {
283+
char buffer[8];
284+
uint8_t u8;
285+
uint16_t u16;
286+
uint32_t u32;
287+
uint64_t u64;
288+
int8_t i8;
289+
int16_t i16;
290+
int32_t i32;
291+
int64_t i64;
292+
float f;
293+
double d;
294+
};
282295

283296
#define READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, n) \
284-
union msgpack_buffer_cast_block_t* cb = msgpack_buffer_read_cast_block(UNPACKER_BUFFER_(uk), n); \
285-
if(cb == NULL) { \
297+
union msgpack_buffer_cast_block_t cb; \
298+
if (!msgpack_buffer_read_all(UNPACKER_BUFFER_(uk), (char *)&cb.buffer, n)) { \
286299
return PRIMITIVE_EOF; \
287300
}
288301

@@ -443,8 +456,8 @@ static int read_primitive(msgpack_unpacker_t* uk)
443456
case 0xc7: // ext 8
444457
{
445458
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 2);
446-
uint8_t length = cb->u8;
447-
int ext_type = (signed char) cb->buffer[1];
459+
uint8_t length = cb.u8;
460+
int ext_type = (signed char) cb.buffer[1];
448461
if(length == 0) {
449462
return object_complete_ext(uk, ext_type, Qnil);
450463
}
@@ -455,8 +468,8 @@ static int read_primitive(msgpack_unpacker_t* uk)
455468
case 0xc8: // ext 16
456469
{
457470
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 3);
458-
uint16_t length = _msgpack_be16(cb->u16);
459-
int ext_type = (signed char) cb->buffer[2];
471+
uint16_t length = _msgpack_be16(cb.u16);
472+
int ext_type = (signed char) cb.buffer[2];
460473
if(length == 0) {
461474
return object_complete_ext(uk, ext_type, Qnil);
462475
}
@@ -467,8 +480,8 @@ static int read_primitive(msgpack_unpacker_t* uk)
467480
case 0xc9: // ext 32
468481
{
469482
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 5);
470-
uint32_t length = _msgpack_be32(cb->u32);
471-
int ext_type = (signed char) cb->buffer[4];
483+
uint32_t length = _msgpack_be32(cb.u32);
484+
int ext_type = (signed char) cb.buffer[4];
472485
if(length == 0) {
473486
return object_complete_ext(uk, ext_type, Qnil);
474487
}
@@ -479,109 +492,109 @@ static int read_primitive(msgpack_unpacker_t* uk)
479492
case 0xca: // float
480493
{
481494
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 4);
482-
cb->u32 = _msgpack_be_float(cb->u32);
483-
return object_complete(uk, rb_float_new(cb->f));
495+
cb.u32 = _msgpack_be_float(cb.u32);
496+
return object_complete(uk, rb_float_new(cb.f));
484497
}
485498

486499
case 0xcb: // double
487500
{
488501
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 8);
489-
cb->u64 = _msgpack_be_double(cb->u64);
490-
return object_complete(uk, rb_float_new(cb->d));
502+
cb.u64 = _msgpack_be_double(cb.u64);
503+
return object_complete(uk, rb_float_new(cb.d));
491504
}
492505

493506
case 0xcc: // unsigned int 8
494507
{
495508
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 1);
496-
uint8_t u8 = cb->u8;
509+
uint8_t u8 = cb.u8;
497510
return object_complete(uk, INT2NUM((int)u8));
498511
}
499512

500513
case 0xcd: // unsigned int 16
501514
{
502515
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 2);
503-
uint16_t u16 = _msgpack_be16(cb->u16);
516+
uint16_t u16 = _msgpack_be16(cb.u16);
504517
return object_complete(uk, INT2NUM((int)u16));
505518
}
506519

507520
case 0xce: // unsigned int 32
508521
{
509522
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 4);
510-
uint32_t u32 = _msgpack_be32(cb->u32);
523+
uint32_t u32 = _msgpack_be32(cb.u32);
511524
return object_complete(uk, ULONG2NUM(u32)); // long at least 32 bits
512525
}
513526

514527
case 0xcf: // unsigned int 64
515528
{
516529
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 8);
517-
uint64_t u64 = _msgpack_be64(cb->u64);
530+
uint64_t u64 = _msgpack_be64(cb.u64);
518531
return object_complete(uk, rb_ull2inum(u64));
519532
}
520533

521534
case 0xd0: // signed int 8
522535
{
523536
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 1);
524-
int8_t i8 = cb->i8;
537+
int8_t i8 = cb.i8;
525538
return object_complete(uk, INT2NUM((int)i8));
526539
}
527540

528541
case 0xd1: // signed int 16
529542
{
530543
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 2);
531-
int16_t i16 = _msgpack_be16(cb->i16);
544+
int16_t i16 = _msgpack_be16(cb.i16);
532545
return object_complete(uk, INT2NUM((int)i16));
533546
}
534547

535548
case 0xd2: // signed int 32
536549
{
537550
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 4);
538-
int32_t i32 = _msgpack_be32(cb->i32);
551+
int32_t i32 = _msgpack_be32(cb.i32);
539552
return object_complete(uk, LONG2NUM(i32)); // long at least 32 bits
540553
}
541554

542555
case 0xd3: // signed int 64
543556
{
544557
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 8);
545-
int64_t i64 = _msgpack_be64(cb->i64);
558+
int64_t i64 = _msgpack_be64(cb.i64);
546559
return object_complete(uk, rb_ll2inum(i64));
547560
}
548561

549562
case 0xd4: // fixext 1
550563
{
551564
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 1);
552-
int ext_type = cb->i8;
565+
int ext_type = cb.i8;
553566
uk->reading_raw_remaining = 1;
554567
return read_raw_body_begin(uk, ext_type);
555568
}
556569

557570
case 0xd5: // fixext 2
558571
{
559572
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 1);
560-
int ext_type = cb->i8;
573+
int ext_type = cb.i8;
561574
uk->reading_raw_remaining = 2;
562575
return read_raw_body_begin(uk, ext_type);
563576
}
564577

565578
case 0xd6: // fixext 4
566579
{
567580
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 1);
568-
int ext_type = cb->i8;
581+
int ext_type = cb.i8;
569582
uk->reading_raw_remaining = 4;
570583
return read_raw_body_begin(uk, ext_type);
571584
}
572585

573586
case 0xd7: // fixext 8
574587
{
575588
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 1);
576-
int ext_type = cb->i8;
589+
int ext_type = cb.i8;
577590
uk->reading_raw_remaining = 8;
578591
return read_raw_body_begin(uk, ext_type);
579592
}
580593

581594
case 0xd8: // fixext 16
582595
{
583596
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 1);
584-
int ext_type = cb->i8;
597+
int ext_type = cb.i8;
585598
uk->reading_raw_remaining = 16;
586599
return read_raw_body_begin(uk, ext_type);
587600
}
@@ -590,7 +603,7 @@ static int read_primitive(msgpack_unpacker_t* uk)
590603
case 0xd9: // raw 8 / str 8
591604
{
592605
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 1);
593-
uint8_t count = cb->u8;
606+
uint8_t count = cb.u8;
594607
/* read_raw_body_begin sets uk->reading_raw */
595608
uk->reading_raw_remaining = count;
596609
return read_raw_body_begin(uk, RAW_TYPE_STRING);
@@ -599,7 +612,7 @@ static int read_primitive(msgpack_unpacker_t* uk)
599612
case 0xda: // raw 16 / str 16
600613
{
601614
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 2);
602-
uint16_t count = _msgpack_be16(cb->u16);
615+
uint16_t count = _msgpack_be16(cb.u16);
603616
/* read_raw_body_begin sets uk->reading_raw */
604617
uk->reading_raw_remaining = count;
605618
return read_raw_body_begin(uk, RAW_TYPE_STRING);
@@ -608,7 +621,7 @@ static int read_primitive(msgpack_unpacker_t* uk)
608621
case 0xdb: // raw 32 / str 32
609622
{
610623
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 4);
611-
uint32_t count = _msgpack_be32(cb->u32);
624+
uint32_t count = _msgpack_be32(cb.u32);
612625
/* read_raw_body_begin sets uk->reading_raw */
613626
uk->reading_raw_remaining = count;
614627
return read_raw_body_begin(uk, RAW_TYPE_STRING);
@@ -617,7 +630,7 @@ static int read_primitive(msgpack_unpacker_t* uk)
617630
case 0xc4: // bin 8
618631
{
619632
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 1);
620-
uint8_t count = cb->u8;
633+
uint8_t count = cb.u8;
621634
/* read_raw_body_begin sets uk->reading_raw */
622635
uk->reading_raw_remaining = count;
623636
return read_raw_body_begin(uk, RAW_TYPE_BINARY);
@@ -626,7 +639,7 @@ static int read_primitive(msgpack_unpacker_t* uk)
626639
case 0xc5: // bin 16
627640
{
628641
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 2);
629-
uint16_t count = _msgpack_be16(cb->u16);
642+
uint16_t count = _msgpack_be16(cb.u16);
630643
/* read_raw_body_begin sets uk->reading_raw */
631644
uk->reading_raw_remaining = count;
632645
return read_raw_body_begin(uk, RAW_TYPE_BINARY);
@@ -635,7 +648,7 @@ static int read_primitive(msgpack_unpacker_t* uk)
635648
case 0xc6: // bin 32
636649
{
637650
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 4);
638-
uint32_t count = _msgpack_be32(cb->u32);
651+
uint32_t count = _msgpack_be32(cb.u32);
639652
/* read_raw_body_begin sets uk->reading_raw */
640653
uk->reading_raw_remaining = count;
641654
return read_raw_body_begin(uk, RAW_TYPE_BINARY);
@@ -644,7 +657,7 @@ static int read_primitive(msgpack_unpacker_t* uk)
644657
case 0xdc: // array 16
645658
{
646659
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 2);
647-
uint16_t count = _msgpack_be16(cb->u16);
660+
uint16_t count = _msgpack_be16(cb.u16);
648661
if(count == 0) {
649662
return object_complete(uk, rb_ary_new());
650663
}
@@ -654,7 +667,7 @@ static int read_primitive(msgpack_unpacker_t* uk)
654667
case 0xdd: // array 32
655668
{
656669
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 4);
657-
uint32_t count = _msgpack_be32(cb->u32);
670+
uint32_t count = _msgpack_be32(cb.u32);
658671
if(count == 0) {
659672
return object_complete(uk, rb_ary_new());
660673
}
@@ -664,7 +677,7 @@ static int read_primitive(msgpack_unpacker_t* uk)
664677
case 0xde: // map 16
665678
{
666679
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 2);
667-
uint16_t count = _msgpack_be16(cb->u16);
680+
uint16_t count = _msgpack_be16(cb.u16);
668681
if(count == 0) {
669682
return object_complete(uk, rb_hash_new());
670683
}
@@ -674,7 +687,7 @@ static int read_primitive(msgpack_unpacker_t* uk)
674687
case 0xdf: // map 32
675688
{
676689
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 4);
677-
uint32_t count = _msgpack_be32(cb->u32);
690+
uint32_t count = _msgpack_be32(cb.u32);
678691
if(count == 0) {
679692
return object_complete(uk, rb_hash_new());
680693
}
@@ -704,12 +717,12 @@ int msgpack_unpacker_read_array_header(msgpack_unpacker_t* uk, uint32_t* result_
704717
} else if(b == 0xdc) {
705718
/* array 16 */
706719
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 2);
707-
*result_size = _msgpack_be16(cb->u16);
720+
*result_size = _msgpack_be16(cb.u16);
708721

709722
} else if(b == 0xdd) {
710723
/* array 32 */
711724
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 4);
712-
*result_size = _msgpack_be32(cb->u32);
725+
*result_size = _msgpack_be32(cb.u32);
713726

714727
} else {
715728
return PRIMITIVE_UNEXPECTED_TYPE;
@@ -732,12 +745,12 @@ int msgpack_unpacker_read_map_header(msgpack_unpacker_t* uk, uint32_t* result_si
732745
} else if(b == 0xde) {
733746
/* map 16 */
734747
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 2);
735-
*result_size = _msgpack_be16(cb->u16);
748+
*result_size = _msgpack_be16(cb.u16);
736749

737750
} else if(b == 0xdf) {
738751
/* map 32 */
739752
READ_CAST_BLOCK_OR_RETURN_EOF(cb, uk, 4);
740-
*result_size = _msgpack_be32(cb->u32);
753+
*result_size = _msgpack_be32(cb.u32);
741754

742755
} else {
743756
return PRIMITIVE_UNEXPECTED_TYPE;

0 commit comments

Comments
 (0)