Skip to content

Commit faee5bb

Browse files
committed
perf(ubus): Reduce duplicate code
Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
1 parent 318b74b commit faee5bb

File tree

1 file changed

+51
-70
lines changed

1 file changed

+51
-70
lines changed

ubus.c

+51-70
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,54 @@ struct eco_ubus_object {
3535

3636
static const char *obj_registry = "eco.ubus{obj}";
3737

38-
static void lua_push_ubus_ctx(lua_State *L, struct eco_ubus_context *ctx)
38+
/* ctx_env[ptr] = userdata */
39+
static int lua_save_obj_to_ubus_ctx(lua_State *L, void *obj)
40+
{
41+
lua_getuservalue(L, 1);
42+
43+
lua_pushlightuserdata(L, obj);
44+
lua_pushvalue(L, -3);
45+
lua_rawset(L, -3);
46+
47+
lua_settop(L, -2);
48+
49+
return 1;
50+
}
51+
52+
static void lua_get_obj_env_from_ubus_ctx(lua_State *L, struct eco_ubus_context *ctx, void *obj)
3953
{
4054
lua_rawgetp(L, LUA_REGISTRYINDEX, &obj_registry);
4155
lua_rawgetp(L, -1, ctx);
42-
lua_remove(L, -2);
56+
lua_getuservalue(L, -1); /* ..., reg, ctx, ctx env */
57+
58+
lua_pushlightuserdata(L, obj); /* ..., reg, ctx, ctx env, ptr */
59+
lua_rawget(L, -2); /* ..., reg, ctx, ctx env, handler */
60+
lua_getuservalue(L, -1); /* ..., reg, ctx, ctx env, handler, handler env */
61+
62+
lua_replace(L, -5); /* ..., handler env, ctx, ctx env, handler */
63+
lua_pop(L, 3);
64+
}
65+
66+
static void lua_get_obj_cb_from_ubus_ctx_by_idx(lua_State *L, struct eco_ubus_context *ctx,
67+
void *obj, int idx)
68+
{
69+
lua_pushnil(L);
70+
lua_get_obj_env_from_ubus_ctx(L, ctx, obj); /* ..., nil, handler env */
71+
lua_rawgeti(L, -1, idx); /* ..., nil, handler env, func */
72+
73+
lua_replace(L, -3); /* ..., func, handler env */
74+
lua_pop(L, 1); /* ..., func */
75+
}
76+
77+
static void lua_get_obj_cb_from_ubus_ctx_by_str(lua_State *L, struct eco_ubus_context *ctx,
78+
void *obj, const char *name)
79+
{
80+
lua_pushnil(L);
81+
lua_get_obj_env_from_ubus_ctx(L, ctx, obj); /* ..., nil, handler env */
82+
lua_getfield(L, -1, name); /* ..., nil, handler env, func */
83+
84+
lua_replace(L, -3); /* ..., func, handler env */
85+
lua_pop(L, 1); /* ..., func */
4386
}
4487

4588
static void lua_table_to_blob(lua_State *L, int index, struct blob_buf *b, bool is_array)
@@ -392,21 +435,7 @@ static void lua_ubus_event_handler(struct ubus_context *ctx, struct ubus_event_h
392435
struct eco_ubus_context *c = container_of(ctx, struct eco_ubus_context, ctx);
393436
lua_State *L = c->eco->L;
394437

395-
lua_pushnil(L);
396-
397-
lua_push_ubus_ctx(L, c);
398-
lua_getuservalue(L, -1); /* ..., nil, ctx, ctx env */
399-
400-
lua_pushlightuserdata(L, ev); /* ..., nil, ctx, ctx env, ptr */
401-
lua_rawget(L, -2); /* ..., nil, ctx, ctx env, handler */
402-
403-
lua_getuservalue(L, -1); /* ..., nil, ctx, ctx env, handler, handler env */
404-
405-
lua_rawgeti(L, -1, 1); /* ..., nil, ctx, ctx env, handler, handler env, func */
406-
407-
lua_replace(L, -6); /* ..., func, ctx, ctx env, handler, handler env */
408-
409-
lua_settop(L, -5); /* ..., func */
438+
lua_get_obj_cb_from_ubus_ctx_by_idx(L, c, ev, 1);
410439

411440
lua_pushstring(L, type);
412441

@@ -441,13 +470,7 @@ static int lua_ubus_listen(lua_State *L)
441470
return 2;
442471
}
443472

444-
lua_getuservalue(L, 1); /* 1: ctx 2: event name 3: func 4: ev handler 5: ctx env */
445-
446-
lua_pushlightuserdata(L, e); /* 1: ctx 2: event name 3: func 4: handler 5: ctx env, 6: handler ptr */
447-
lua_pushvalue(L, -3); /* 1: ctx 2: event name 3: func 4: handler 5: ctx env, 6: handler ptr, 7: handler */
448-
lua_rawset(L, -3); /* ctx_env[ptr] = handler */
449-
450-
lua_settop(L, -2); /* 1: ctx 2: event name 3: func 4: handler */
473+
lua_save_obj_to_ubus_ctx(L, e);
451474

452475
return 1;
453476
}
@@ -468,19 +491,7 @@ static int ubus_method_handler(struct ubus_context *ctx, struct ubus_object *obj
468491

469492
lua_settop(L, 0);
470493

471-
lua_pushnil(L);
472-
473-
lua_push_ubus_ctx(L, c);
474-
lua_getuservalue(L, -1);
475-
476-
lua_pushlightuserdata(L, o);
477-
lua_rawget(L, -2);
478-
479-
lua_getuservalue(L, -1);
480-
lua_getfield(L, -1, method);
481-
482-
lua_replace(L, -6);
483-
lua_settop(L, -5);
494+
lua_get_obj_cb_from_ubus_ctx_by_str(L, c, o, method);
484495

485496
ubus_defer_request(ctx, req, dreq);
486497

@@ -621,15 +632,7 @@ static int lua_ubus_add(lua_State *L)
621632
return 2;
622633
}
623634

624-
lua_getuservalue(L, 1);
625-
626-
lua_pushlightuserdata(L, o);
627-
lua_pushvalue(L, -3);
628-
lua_rawset(L, -3);
629-
630-
lua_settop(L, -2);
631-
632-
return 1;
635+
return lua_save_obj_to_ubus_ctx(L, o);
633636
}
634637

635638
static int lua_ubus_reply(lua_State *L)
@@ -670,21 +673,7 @@ static int ubus_subscriber_cb(struct ubus_context *ctx, struct ubus_object *obj,
670673
struct ubus_subscriber *s = container_of(obj, struct ubus_subscriber, obj);
671674
lua_State *L = c->eco->L;
672675

673-
lua_pushnil(L);
674-
675-
lua_push_ubus_ctx(L, c);
676-
lua_getuservalue(L, -1);
677-
678-
lua_pushlightuserdata(L, s);
679-
lua_rawget(L, -2);
680-
681-
lua_getuservalue(L, -1);
682-
683-
lua_rawgeti(L, -1, 1);
684-
685-
lua_replace(L, -6);
686-
687-
lua_settop(L, -5);
676+
lua_get_obj_cb_from_ubus_ctx_by_idx(L, c, s, 1);
688677

689678
lua_pushstring(L, method);
690679

@@ -733,15 +722,7 @@ static int lua_ubus_subscribe(lua_State *L)
733722
return 2;
734723
}
735724

736-
lua_getuservalue(L, 1);
737-
738-
lua_pushlightuserdata(L, sub);
739-
lua_pushvalue(L, -3);
740-
lua_rawset(L, -3);
741-
742-
lua_settop(L, -2);
743-
744-
return 0;
725+
return lua_save_obj_to_ubus_ctx(L, sub);
745726
}
746727

747728
static int lua_ubus_notify(lua_State *L)

0 commit comments

Comments
 (0)