Skip to content

Commit

Permalink
misc: add compiler optimization opportunities
Browse files Browse the repository at this point in the history
Signed-off-by: He Xian <[email protected]>
  • Loading branch information
hexian000 committed Jan 22, 2025
1 parent ad3f318 commit dc0389b
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/api_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ static void send_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
const struct vbuffer *restrict cbuf = p->cbuf;
buf = cbuf->data + p->cpos;
len = cbuf->len - p->cpos;
err = socket_send(watcher->fd, buf, &len);
err = socket_send(fd, buf, &len);
if (err != 0) {
const char *msg = strerror(err);
api_client_finish(loop, ctx, msg, strlen(msg), NULL);
Expand Down
18 changes: 10 additions & 8 deletions src/dialer.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ dialaddr_set(struct dialaddr *addr, const char *host, const uint16_t port)
}

bool dialaddr_parse(
struct dialaddr *restrict addr, const char *s, const size_t len)
struct dialaddr *restrict addr, const char *restrict s,
const size_t len)
{
/* FQDN + ':' + port */
if (len > FQDN_MAX_LENGTH + 1 + 5) {
Expand Down Expand Up @@ -142,8 +143,8 @@ int dialaddr_format(
}

static bool proxy_set_credential(
struct proxyreq *restrict proxy, const char *username,
const char *password)
struct proxyreq *restrict proxy, const char *restrict username,
const char *restrict password)
{
const size_t ulen = (username != NULL) ? strlen(username) + 1 : 0;
const size_t plen = (password != NULL) ? strlen(password) + 1 : 0;
Expand All @@ -162,7 +163,7 @@ static bool proxy_set_credential(
}

bool dialreq_addproxy(
struct dialreq *restrict req, const char *proxy_uri,
struct dialreq *restrict req, const char *restrict proxy_uri,
const size_t urilen)
{
/* should be more than enough */
Expand Down Expand Up @@ -230,7 +231,8 @@ bool dialreq_addproxy(
#define DIALREQ_NEW(n) \
(malloc(sizeof(struct dialreq) + sizeof(struct proxyreq) * (n)))

struct dialreq *dialreq_parse(const char *addr, const char *csv)
struct dialreq *
dialreq_parse(const char *restrict addr, const char *restrict csv)
{
size_t len = 0, n = 0;
if (csv != NULL) {
Expand All @@ -244,7 +246,7 @@ struct dialreq *dialreq_parse(const char *addr, const char *csv)
}
}
}
struct dialreq *req = DIALREQ_NEW(n);
struct dialreq *restrict req = DIALREQ_NEW(n);
if (req == NULL) {
LOGOOM();
return NULL;
Expand Down Expand Up @@ -1318,8 +1320,8 @@ void dialer_init(struct dialer *restrict d, const struct event_cb *cb)
}

void dialer_do(
struct dialer *restrict d, struct ev_loop *restrict loop,
const struct dialreq *restrict req)
struct dialer *restrict d, struct ev_loop *loop,
const struct dialreq *req)
{
if (LOGLEVEL(VERBOSE)) {
char s[4096];
Expand Down
2 changes: 1 addition & 1 deletion src/http_proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ static void dialer_cb(struct ev_loop *loop, void *data)
ev_io_start(loop, &ctx->w_send);
}

static struct dialreq *make_dialreq(const char *addr_str)
static struct dialreq *make_dialreq(const char *restrict addr_str)
{
struct dialreq *req = dialreq_new(0);
if (req == NULL) {
Expand Down
6 changes: 3 additions & 3 deletions src/ruleset/await.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ struct await_resolve_userdata {

static int await_resolve_close(lua_State *restrict L)
{
struct await_resolve_userdata *ud = lua_touserdata(L, 1);
struct await_resolve_userdata *restrict ud = lua_touserdata(L, 1);
if (ud->query != NULL) {
resolve_cancel(ud->query);
ud->query = NULL;
Expand All @@ -189,9 +189,9 @@ static int await_resolve_close(lua_State *restrict L)

static void resolve_cb(
struct resolve_query *q, struct ev_loop *loop, void *data,
const struct sockaddr *sa)
const struct sockaddr *restrict sa)
{
struct await_resolve_userdata *ud = data;
struct await_resolve_userdata *restrict ud = data;
ASSERT(ud->query == q);
UNUSED(q);
ud->query = NULL;
Expand Down
18 changes: 9 additions & 9 deletions src/ruleset/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,18 @@ static int thread_main(lua_State *restrict L)
/* [-0, +1, v] */
lua_State *aux_getthread(lua_State *restrict L)
{
lua_pushnil(L);
aux_getregtable(L, RIDX_IDLE_THREAD);
lua_pushnil(L);
if (lua_next(L, -2)) {
lua_copy(L, -2, -4);
lua_pop(L, 1);
lua_pushvalue(L, -1);
lua_pushnil(L);
lua_rawset(L, -3);
lua_pop(L, 1);
/* lua stack: RIDX_IDLE_THREAD co co nil */
lua_rawset(L, -4);
lua_replace(L, -2);
return lua_tothread(L, -1);
}
lua_pop(L, 2);
lua_pop(L, 1);
lua_State *restrict co = lua_newthread(L);
lua_pushcfunction(co, thread_main);
const int status = aux_resume(co, NULL, 0);
Expand All @@ -124,7 +124,7 @@ lua_State *aux_getthread(lua_State *restrict L)
return co;
}

const char *aux_reader(lua_State *L, void *ud, size_t *restrict sz)
const char *aux_reader(lua_State *restrict L, void *ud, size_t *restrict sz)
{
UNUSED(L);
struct stream *s = ud;
Expand All @@ -144,7 +144,7 @@ const char *aux_reader(lua_State *L, void *ud, size_t *restrict sz)
int aux_format_addr(lua_State *restrict L)
{
/* lua stack: ... sa */
const struct sockaddr *sa = lua_touserdata(L, -1);
const struct sockaddr *restrict sa = lua_touserdata(L, -1);
if (sa == NULL) {
return 0;
}
Expand Down Expand Up @@ -196,7 +196,7 @@ bool aux_todialreq(lua_State *restrict L, const int n)
}
size_t len;
for (int i = 1; i < n; i++) {
const char *s = lua_tolstring(L, -i, &len);
const char *restrict s = lua_tolstring(L, -i, &len);
if (s == NULL) {
dialreq_free(req);
lua_pop(L, n);
Expand All @@ -208,7 +208,7 @@ bool aux_todialreq(lua_State *restrict L, const int n)
return false;
}
}
const char *s = lua_tolstring(L, -n, &len);
const char *restrict s = lua_tolstring(L, -n, &len);
if (s == NULL) {
dialreq_free(req);
lua_pop(L, n);
Expand Down
8 changes: 4 additions & 4 deletions src/ruleset/cfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ int cfunc_request(lua_State *restrict L)
{
check_memlimit(L);
ASSERT(lua_gettop(L) == 6);
struct ruleset_state **pstate = lua_touserdata(L, 1);
struct ruleset_state *restrict *restrict pstate = lua_touserdata(L, 1);
const char *func = lua_touserdata(L, 2);
const char *request = lua_touserdata(L, 3);
const char *username = lua_touserdata(L, 4);
const char *password = lua_touserdata(L, 5);
const struct ruleset_request_cb *in_cb = lua_touserdata(L, 6);
const struct ruleset_request_cb *restrict in_cb = lua_touserdata(L, 6);
lua_settop(L, 0);

struct ruleset_state *restrict state =
Expand Down Expand Up @@ -194,9 +194,9 @@ int cfunc_rpcall(lua_State *restrict L)
{
check_memlimit(L);
ASSERT(lua_gettop(L) == 3);
struct ruleset_state **pstate = lua_touserdata(L, 1);
struct ruleset_state *restrict *restrict pstate = lua_touserdata(L, 1);
struct stream *stream = lua_touserdata(L, 2);
const struct ruleset_rpcall_cb *in_cb = lua_touserdata(L, 3);
const struct ruleset_rpcall_cb *restrict in_cb = lua_touserdata(L, 3);
lua_settop(L, 0);

struct ruleset_state *restrict state =
Expand Down
17 changes: 11 additions & 6 deletions src/ruleset/marshal.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ static int marshal_buffer_gc(lua_State *restrict L)
}

/* [-0, +0, -] */
static void marshal_string(lua_State *L, struct vbuffer **pvbuf)
static void
marshal_string(lua_State *restrict L, struct vbuffer *restrict *restrict pvbuf)
{
const int idx = 1;
size_t len;
Expand Down Expand Up @@ -57,7 +58,8 @@ static void marshal_string(lua_State *L, struct vbuffer **pvbuf)
}

/* [-0, +0, -] */
static void marshal_number(lua_State *L, struct vbuffer **pvbuf)
static void
marshal_number(lua_State *restrict L, struct vbuffer *restrict *restrict pvbuf)
{
const int idx = 1;
static const char prefix[3] = "-0x";
Expand Down Expand Up @@ -159,7 +161,8 @@ static void marshal_number(lua_State *L, struct vbuffer **pvbuf)
#define IDX_MARSHAL (lua_upvalueindex(3))

/* [-0, +0, m] */
static void marshal_table(lua_State *L, struct vbuffer **pvbuf)
static void
marshal_table(lua_State *restrict L, struct vbuffer *restrict *restrict pvbuf)
{
const int idx = 1;
/* check visited */
Expand Down Expand Up @@ -198,10 +201,11 @@ static void marshal_table(lua_State *L, struct vbuffer **pvbuf)
}

/* [-0, +0, v] */
static int marshal_value(lua_State *L)
static int marshal_value(lua_State *restrict L)
{
const int idx = 1;
struct vbuffer **pvbuf = lua_touserdata(L, IDX_BUFFER);
struct vbuffer *restrict *restrict pvbuf =
lua_touserdata(L, IDX_BUFFER);
const int type = lua_type(L, idx);
switch (type) {
case LUA_TNIL:
Expand Down Expand Up @@ -252,7 +256,8 @@ int api_marshal(lua_State *restrict L)
lua_pushliteral(L, "");
return 1;
}
struct vbuffer **pvbuf = lua_newuserdata(L, sizeof(struct vbuffer *));
struct vbuffer *restrict *restrict pvbuf =
lua_newuserdata(L, sizeof(struct vbuffer *));
*pvbuf = VBUF_NEW(1024);
if (luaL_newmetatable(L, MT_MARSHAL_BUFFER)) {
lua_pushcfunction(L, marshal_buffer_gc);
Expand Down
2 changes: 1 addition & 1 deletion src/socks.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ static int socks4_req(struct socks_ctx *restrict ctx)
static int socks5_req(struct socks_ctx *restrict ctx)
{
ASSERT(ctx->state == STATE_HANDSHAKE3);
const unsigned char *hdr = ctx->next;
const unsigned char *restrict hdr = ctx->next;
const size_t len = ctx->rbuf.len - (ctx->next - ctx->rbuf.data);
size_t want = sizeof(struct socks5_hdr);
if (len < want) {
Expand Down
29 changes: 18 additions & 11 deletions src/sockutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,9 @@ void copy_sa(struct sockaddr *restrict dst, const struct sockaddr *restrict src)
FAILMSGF("unexpected af: %jd", (intmax_t)src->sa_family);
}

static int
format_sa_inet(char *s, const size_t maxlen, const struct sockaddr_in *sa)
static int format_sa_inet(
char *restrict s, const size_t maxlen,
const struct sockaddr_in *restrict sa)
{
char buf[INET_ADDRSTRLEN];
if (inet_ntop(AF_INET, &(sa->sin_addr), buf, sizeof(buf)) == NULL) {
Expand All @@ -205,8 +206,9 @@ format_sa_inet(char *s, const size_t maxlen, const struct sockaddr_in *sa)
return snprintf(s, maxlen, "%s:%" PRIu16, buf, port);
}

static int
format_sa_inet6(char *s, const size_t maxlen, const struct sockaddr_in6 *sa)
static int format_sa_inet6(
char *restrict s, const size_t maxlen,
const struct sockaddr_in6 *restrict sa)
{
char buf[INET6_ADDRSTRLEN];
if (inet_ntop(AF_INET6, &(sa->sin6_addr), buf, sizeof(buf)) == NULL) {
Expand All @@ -221,20 +223,25 @@ format_sa_inet6(char *s, const size_t maxlen, const struct sockaddr_in6 *sa)
s, maxlen, "[%s%%%" PRIu32 "]:%" PRIu16, buf, scope, port);
}

int format_sa(char *s, const size_t maxlen, const struct sockaddr *sa)
int format_sa(
char *restrict s, const size_t maxlen,
const struct sockaddr *restrict sa)
{
switch (sa->sa_family) {
case AF_INET:
return format_sa_inet(s, maxlen, (struct sockaddr_in *)sa);
return format_sa_inet(
s, maxlen, (struct sockaddr_in *restrict)sa);
case AF_INET6:
return format_sa_inet6(s, maxlen, (struct sockaddr_in6 *)sa);
return format_sa_inet6(
s, maxlen, (struct sockaddr_in6 *restrict)sa);
default:
break;
}
return snprintf(s, maxlen, "<af:%jd>", (intmax_t)sa->sa_family);
}

static bool find_addrinfo(union sockaddr_max *sa, const struct addrinfo *node)
static bool find_addrinfo(
union sockaddr_max *restrict sa, const struct addrinfo *restrict node)
{
for (const struct addrinfo *it = node; it != NULL; it = it->ai_next) {
#define EXPECT_ADDRLEN(p, expected) \
Expand Down Expand Up @@ -265,7 +272,7 @@ static bool find_addrinfo(union sockaddr_max *sa, const struct addrinfo *node)
return false;
}

bool parse_bindaddr(union sockaddr_max *sa, const char *s)
bool parse_bindaddr(union sockaddr_max *restrict sa, const char *restrict s)
{
const size_t addrlen = strlen(s);
char buf[FQDN_MAX_LENGTH + 1 + 5 + 1];
Expand Down Expand Up @@ -299,8 +306,8 @@ bool parse_bindaddr(union sockaddr_max *sa, const char *s)
}

bool resolve_addr(
union sockaddr_max *sa, const char *name, const char *service,
const int family)
union sockaddr_max *restrict sa, const char *restrict name,
const char *restrict service, const int family)
{
struct addrinfo hints = {
.ai_family = family,
Expand Down

0 comments on commit dc0389b

Please sign in to comment.