Skip to content

Commit 3d28793

Browse files
committed
Embed unpacker_stack inside unpacker
Now that there's no longer a concept of child stacks, we can embed the struct and save a malloc/free pair.
1 parent 8289177 commit 3d28793

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

ext/msgpack/unpacker.c

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,9 @@ void msgpack_unpacker_static_destroy(void)
7979

8080
#define HEAD_BYTE_REQUIRED 0xc1
8181

82-
static inline msgpack_unpacker_stack_t* _msgpack_unpacker_new_stack(void) {
83-
msgpack_unpacker_stack_t *stack = ZALLOC(msgpack_unpacker_stack_t);
82+
static inline void _msgpack_unpacker_stack_init(msgpack_unpacker_stack_t *stack) {
8483
stack->capacity = MSGPACK_UNPACKER_STACK_CAPACITY;
8584
stack->data = msgpack_rmem_alloc(&s_stack_rmem);
86-
/*memset(uk->stack, 0, MSGPACK_UNPACKER_STACK_CAPACITY);*/
87-
return stack;
8885
}
8986

9087
void _msgpack_unpacker_init(msgpack_unpacker_t* uk)
@@ -96,25 +93,26 @@ void _msgpack_unpacker_init(msgpack_unpacker_t* uk)
9693
uk->last_object = Qnil;
9794
uk->reading_raw = Qnil;
9895

99-
uk->stack = _msgpack_unpacker_new_stack();
96+
_msgpack_unpacker_stack_init(&uk->stack);
10097
}
10198

10299
static inline void _msgpack_unpacker_free_stack(msgpack_unpacker_stack_t* stack) {
103100
if (!msgpack_rmem_free(&s_stack_rmem, stack->data)) {
104101
rb_bug("Failed to free an rmem pointer, memory leak?");
105102
}
106-
xfree(stack);
103+
stack->data = NULL;
104+
stack->depth = 0;
107105
}
108106

109107
void _msgpack_unpacker_destroy(msgpack_unpacker_t* uk)
110108
{
111-
_msgpack_unpacker_free_stack(uk->stack);
109+
_msgpack_unpacker_free_stack(&uk->stack);
112110
msgpack_buffer_destroy(UNPACKER_BUFFER_(uk));
113111
}
114112

115113
void msgpack_unpacker_mark_stack(msgpack_unpacker_stack_t* stack)
116114
{
117-
if (stack) {
115+
if (stack->data) {
118116
msgpack_unpacker_stack_entry_t* s = stack->data;
119117
msgpack_unpacker_stack_entry_t* send = stack->data + stack->depth;
120118
for(; s < send; s++) {
@@ -128,7 +126,7 @@ void msgpack_unpacker_mark(msgpack_unpacker_t* uk)
128126
{
129127
rb_gc_mark(uk->last_object);
130128
rb_gc_mark(uk->reading_raw);
131-
msgpack_unpacker_mark_stack(uk->stack);
129+
msgpack_unpacker_mark_stack(&uk->stack);
132130
/* See MessagePack_Buffer_wrap */
133131
/* msgpack_buffer_mark(UNPACKER_BUFFER_(uk)); */
134132
rb_gc_mark(uk->buffer_ref);
@@ -141,8 +139,8 @@ void _msgpack_unpacker_reset(msgpack_unpacker_t* uk)
141139

142140
uk->head_byte = HEAD_BYTE_REQUIRED;
143141

144-
/*memset(uk->stack, 0, sizeof(msgpack_unpacker_t) * uk->stack->depth);*/
145-
uk->stack->depth = 0;
142+
/*memset(uk->stack, 0, sizeof(msgpack_unpacker_t) * uk->stack.depth);*/
143+
uk->stack.depth = 0;
146144
uk->last_object = Qnil;
147145
uk->reading_raw = Qnil;
148146
uk->reading_raw_remaining = 0;
@@ -226,35 +224,35 @@ static inline int object_complete_ext(msgpack_unpacker_t* uk, int ext_type, VALU
226224
/* stack funcs */
227225
static inline msgpack_unpacker_stack_entry_t* _msgpack_unpacker_stack_entry_top(msgpack_unpacker_t* uk)
228226
{
229-
return &uk->stack->data[uk->stack->depth-1];
227+
return &uk->stack.data[uk->stack.depth-1];
230228
}
231229

232230
static inline int _msgpack_unpacker_stack_push(msgpack_unpacker_t* uk, enum stack_type_t type, size_t count, VALUE object)
233231
{
234232
reset_head_byte(uk);
235233

236-
if(uk->stack->capacity - uk->stack->depth <= 0) {
234+
if(uk->stack.capacity - uk->stack.depth <= 0) {
237235
return PRIMITIVE_STACK_TOO_DEEP;
238236
}
239237

240-
msgpack_unpacker_stack_entry_t* next = &uk->stack->data[uk->stack->depth];
238+
msgpack_unpacker_stack_entry_t* next = &uk->stack.data[uk->stack.depth];
241239
next->count = count;
242240
next->type = type;
243241
next->object = object;
244242
next->key = Qnil;
245243

246-
uk->stack->depth++;
244+
uk->stack.depth++;
247245
return PRIMITIVE_CONTAINER_START;
248246
}
249247

250248
static inline VALUE msgpack_unpacker_stack_pop(msgpack_unpacker_t* uk)
251249
{
252-
return --uk->stack->depth;
250+
return --uk->stack.depth;
253251
}
254252

255253
static inline bool msgpack_unpacker_stack_is_empty(msgpack_unpacker_t* uk)
256254
{
257-
return uk->stack->depth == 0;
255+
return uk->stack.depth == 0;
258256
}
259257

260258
#ifdef USE_CASE_RANGE
@@ -282,7 +280,7 @@ static inline bool msgpack_unpacker_stack_is_empty(msgpack_unpacker_t* uk)
282280

283281
static inline bool is_reading_map_key(msgpack_unpacker_t* uk)
284282
{
285-
if(uk->stack->depth > 0) {
283+
if(uk->stack.depth > 0) {
286284
msgpack_unpacker_stack_entry_t* top = _msgpack_unpacker_stack_entry_top(uk);
287285
if(top->type == STACK_TYPE_MAP_KEY) {
288286
return true;

ext/msgpack/unpacker.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ struct msgpack_unpacker_stack_t {
4949

5050
struct msgpack_unpacker_t {
5151
msgpack_buffer_t buffer;
52-
msgpack_unpacker_stack_t *stack;
52+
msgpack_unpacker_stack_t stack;
5353
unsigned int head_byte;
5454

5555
VALUE self;

ext/msgpack/unpacker_class.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ static size_t Unpacker_memsize(const void *ptr)
6666
total_size += sizeof(msgpack_unpacker_ext_registry_t) / (uk->ext_registry->borrow_count + 1);
6767
}
6868

69-
if (uk->stack) {
70-
total_size += (uk->stack->depth + 1) * sizeof(msgpack_unpacker_stack_t);
69+
if (uk->stack.data) {
70+
total_size += (uk->stack.depth + 1) * sizeof(msgpack_unpacker_stack_t);
7171
}
7272

7373
return total_size + msgpack_buffer_memsize(&uk->buffer);
@@ -161,7 +161,7 @@ static VALUE Unpacker_allow_unknown_ext_p(VALUE self)
161161

162162
NORETURN(static void raise_unpacker_error(msgpack_unpacker_t *uk, int r))
163163
{
164-
uk->stack->depth = 0;
164+
uk->stack.depth = 0;
165165
switch(r) {
166166
case PRIMITIVE_EOF:
167167
rb_raise(rb_eEOFError, "end of buffer reached");

0 commit comments

Comments
 (0)