~bigbes/tarantool

tarantool-protobuf

ref: d4dbd2f36353f3a03fd68fcfab563b81c6520a6b tarantool-protobuf/bench/c_accel/person_codec.c -rw-r--r-- 9.4 KiB
d4dbd2f3 — Eugene Blikh bench: C-acceleration spike — measure four Lua↔C boundaries 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
 * person_codec.c -- hand-written C codec for hello.Person.
 *
 * Strategy 4 of tarantool-protobuf-04c: upper-bound measurement.
 *
 * Scope: ONLY the fields exercised by bench/bench.lua's Person payloads:
 *   name (string, 1), age (int32, 2), emails (repeated string, 3),
 *   address (Address message, 5), lucky_numbers (packed int32, 7).
 *
 * Other Person fields (status, friends, avatar, user_id, balance,
 * weight_kg, maps) are intentionally absent. The spike measures the
 * upper bound of C boundary perf for the bench shapes, not full
 * codec coverage.
 */

#include <module.h>
#include <lauxlib.h>

#include <stdint.h>
#include <stdlib.h>
#include <string.h>

/* ---------------------------------------------------------------- *
 *  Growable byte buffer with a 4 KiB stack-backed initial region.  *
 * ---------------------------------------------------------------- */

typedef struct {
	uint8_t *data;
	size_t len;
	size_t cap;
	uint8_t stack[4096];
} buf_t;

static inline void
buf_init(buf_t *b)
{
	b->data = b->stack;
	b->len = 0;
	b->cap = sizeof(b->stack);
}

static inline void
buf_free(buf_t *b)
{
	if (b->data != b->stack)
		free(b->data);
}

static void
buf_grow(buf_t *b, size_t need)
{
	size_t nc = b->cap ? b->cap * 2 : 64;
	while (nc < b->len + need)
		nc *= 2;
	uint8_t *nd = (uint8_t *)malloc(nc);
	memcpy(nd, b->data, b->len);
	if (b->data != b->stack)
		free(b->data);
	b->data = nd;
	b->cap = nc;
}

static inline void
buf_reserve(buf_t *b, size_t need)
{
	if (b->len + need > b->cap)
		buf_grow(b, need);
}

/* ---------------------------------------------------------------- *
 *  Wire primitives.                                                *
 * ---------------------------------------------------------------- */

static inline void
write_varint(buf_t *b, uint64_t v)
{
	buf_reserve(b, 10);
	while (v >= 0x80) {
		b->data[b->len++] = (uint8_t)(v | 0x80);
		v >>= 7;
	}
	b->data[b->len++] = (uint8_t)v;
}

static inline void
write_bytes(buf_t *b, const void *src, size_t n)
{
	buf_reserve(b, n);
	memcpy(b->data + b->len, src, n);
	b->len += n;
}

static inline void
write_string_field(buf_t *b, uint32_t tag, const char *s, size_t n)
{
	write_varint(b, tag);
	write_varint(b, (uint64_t)n);
	write_bytes(b, s, n);
}

/* Read a varint. Returns new pointer on success, NULL on truncation. */
static const uint8_t *
read_varint(const uint8_t *p, const uint8_t *end, uint64_t *out)
{
	uint64_t v = 0;
	int shift = 0;
	while (p < end) {
		uint8_t c = *p++;
		v |= (uint64_t)(c & 0x7f) << shift;
		if (!(c & 0x80)) {
			*out = v;
			return p;
		}
		shift += 7;
		if (shift >= 64)
			return NULL;
	}
	return NULL;
}

/* ---------------------------------------------------------------- *
 *  Address encode/decode helpers (sub-message, fields used in       *
 *  bench payloads: street/1, city/2, zip/3).                        *
 * ---------------------------------------------------------------- */

static void
encode_address_body(buf_t *b, lua_State *L, int t)
{
	lua_getfield(L, t, "street");
	if (lua_type(L, -1) == LUA_TSTRING) {
		size_t n;
		const char *s = lua_tolstring(L, -1, &n);
		write_string_field(b, (1 << 3) | 2, s, n);
	}
	lua_pop(L, 1);

	lua_getfield(L, t, "city");
	if (lua_type(L, -1) == LUA_TSTRING) {
		size_t n;
		const char *s = lua_tolstring(L, -1, &n);
		write_string_field(b, (2 << 3) | 2, s, n);
	}
	lua_pop(L, 1);

	lua_getfield(L, t, "zip");
	if (lua_type(L, -1) == LUA_TNUMBER) {
		write_varint(b, (3 << 3) | 0);
		write_varint(b, (uint64_t)(int64_t)lua_tointeger(L, -1));
	}
	lua_pop(L, 1);
}

static const uint8_t *
decode_address(lua_State *L, const uint8_t *p, const uint8_t *end)
{
	lua_createtable(L, 0, 3);
	while (p < end) {
		uint64_t tag;
		p = read_varint(p, end, &tag);
		if (!p)
			return NULL;
		int field = (int)(tag >> 3);
		int wt = (int)(tag & 7);
		if (wt == 2) {
			uint64_t slen;
			p = read_varint(p, end, &slen);
			if (!p || (size_t)(end - p) < slen)
				return NULL;
			if (field == 1) {
				lua_pushlstring(L, (const char *)p, (size_t)slen);
				lua_setfield(L, -2, "street");
			} else if (field == 2) {
				lua_pushlstring(L, (const char *)p, (size_t)slen);
				lua_setfield(L, -2, "city");
			}
			p += slen;
		} else if (wt == 0) {
			uint64_t v;
			p = read_varint(p, end, &v);
			if (!p)
				return NULL;
			if (field == 3) {
				lua_pushinteger(L, (lua_Integer)(int32_t)v);
				lua_setfield(L, -2, "zip");
			}
		} else {
			/* Unknown wire types ignored in spike. */
			return NULL;
		}
	}
	return p;
}

/* ---------------------------------------------------------------- *
 *  Person_encode(tbl) -> string                                    *
 * ---------------------------------------------------------------- */

static int
Person_encode(lua_State *L)
{
	luaL_checktype(L, 1, LUA_TTABLE);
	const int t = 1;
	buf_t b;
	buf_init(&b);

	/* name (1, string) */
	lua_getfield(L, t, "name");
	if (lua_type(L, -1) == LUA_TSTRING) {
		size_t n;
		const char *s = lua_tolstring(L, -1, &n);
		write_string_field(&b, (1 << 3) | 2, s, n);
	}
	lua_pop(L, 1);

	/* age (2, int32) */
	lua_getfield(L, t, "age");
	if (lua_type(L, -1) == LUA_TNUMBER) {
		write_varint(&b, (2 << 3) | 0);
		write_varint(&b, (uint64_t)(int64_t)lua_tointeger(L, -1));
	}
	lua_pop(L, 1);

	/* emails (3, repeated string) */
	lua_getfield(L, t, "emails");
	if (lua_type(L, -1) == LUA_TTABLE) {
		int idx = lua_gettop(L);
		int n_emails = (int)lua_objlen(L, idx);
		for (int i = 1; i <= n_emails; i++) {
			lua_rawgeti(L, idx, i);
			if (lua_type(L, -1) == LUA_TSTRING) {
				size_t n;
				const char *s = lua_tolstring(L, -1, &n);
				write_string_field(&b, (3 << 3) | 2, s, n);
			}
			lua_pop(L, 1);
		}
	}
	lua_pop(L, 1);

	/* address (5, sub-message) */
	lua_getfield(L, t, "address");
	if (lua_type(L, -1) == LUA_TTABLE) {
		int addr_idx = lua_gettop(L);
		buf_t sub;
		buf_init(&sub);
		encode_address_body(&sub, L, addr_idx);
		write_varint(&b, (5 << 3) | 2);
		write_varint(&b, (uint64_t)sub.len);
		write_bytes(&b, sub.data, sub.len);
		buf_free(&sub);
	}
	lua_pop(L, 1);

	/* lucky_numbers (7, packed int32) */
	lua_getfield(L, t, "lucky_numbers");
	if (lua_type(L, -1) == LUA_TTABLE) {
		int idx = lua_gettop(L);
		int n = (int)lua_objlen(L, idx);
		buf_t sub;
		buf_init(&sub);
		for (int i = 1; i <= n; i++) {
			lua_rawgeti(L, idx, i);
			write_varint(&sub, (uint64_t)(int64_t)lua_tointeger(L, -1));
			lua_pop(L, 1);
		}
		write_varint(&b, (7 << 3) | 2);
		write_varint(&b, (uint64_t)sub.len);
		write_bytes(&b, sub.data, sub.len);
		buf_free(&sub);
	}
	lua_pop(L, 1);

	lua_pushlstring(L, (const char *)b.data, b.len);
	buf_free(&b);
	return 1;
}

/* ---------------------------------------------------------------- *
 *  Person_decode(string) -> tbl                                    *
 * ---------------------------------------------------------------- */

static int
Person_decode(lua_State *L)
{
	size_t len;
	const char *buf = luaL_checklstring(L, 1, &len);
	const uint8_t *p = (const uint8_t *)buf;
	const uint8_t *end = p + len;

	lua_createtable(L, 0, 5);
	const int result_idx = lua_gettop(L);

	/* Lazy arrays: stash stack index of the array table once created. */
	int emails_stkidx = 0;
	int n_emails = 0;
	int lucky_stkidx = 0;
	int n_lucky = 0;

	while (p < end) {
		uint64_t tag;
		p = read_varint(p, end, &tag);
		if (!p)
			break;
		int field = (int)(tag >> 3);
		int wt = (int)(tag & 7);

		if (wt == 2) {
			uint64_t slen;
			p = read_varint(p, end, &slen);
			if (!p || (size_t)(end - p) < slen)
				break;
			const uint8_t *fend = p + slen;

			switch (field) {
			case 1: /* name */
				lua_pushlstring(L, (const char *)p, (size_t)slen);
				lua_setfield(L, result_idx, "name");
				break;
			case 3: /* emails (repeated string) */
				if (emails_stkidx == 0) {
					lua_createtable(L, 4, 0);
					emails_stkidx = lua_gettop(L);
				}
				lua_pushlstring(L, (const char *)p, (size_t)slen);
				lua_rawseti(L, emails_stkidx, ++n_emails);
				break;
			case 5: /* address (sub-message) */
				if (decode_address(L, p, fend) == NULL)
					goto done;
				lua_setfield(L, result_idx, "address");
				break;
			case 7: { /* lucky_numbers (packed int32) */
				if (lucky_stkidx == 0) {
					lua_createtable(L, 8, 0);
					lucky_stkidx = lua_gettop(L);
				}
				const uint8_t *q = p;
				while (q < fend) {
					uint64_t v;
					q = read_varint(q, fend, &v);
					if (!q)
						break;
					lua_pushinteger(L,
					    (lua_Integer)(int32_t)v);
					lua_rawseti(L, lucky_stkidx,
					    ++n_lucky);
				}
				break;
			}
			default:
				/* Unknown LEN field -- skip silently. */
				break;
			}
			p = fend;
		} else if (wt == 0) {
			uint64_t v;
			p = read_varint(p, end, &v);
			if (!p)
				break;
			if (field == 2) { /* age */
				lua_pushinteger(L, (lua_Integer)(int32_t)v);
				lua_setfield(L, result_idx, "age");
			}
		} else {
			/* Other wire types not exercised by bench payloads. */
			break;
		}
	}
done:
	if (emails_stkidx) {
		lua_pushvalue(L, emails_stkidx);
		lua_setfield(L, result_idx, "emails");
	}
	if (lucky_stkidx) {
		lua_pushvalue(L, lucky_stkidx);
		lua_setfield(L, result_idx, "lucky_numbers");
	}
	lua_settop(L, result_idx);
	return 1;
}

/* ---------------------------------------------------------------- *
 *  Module entry.                                                   *
 * ---------------------------------------------------------------- */

static const struct luaL_Reg lib[] = {
	{"Person_encode", Person_encode},
	{"Person_decode", Person_decode},
	{NULL, NULL},
};

LUA_API int
luaopen_pb_c_person(lua_State *L)
{
	luaL_register(L, "pb_c_person", lib);
	return 1;
}