Skip to content

Commit e4b2e9d

Browse files
string micro optimisations
1 parent d65cfae commit e4b2e9d

File tree

5 files changed

+15
-30
lines changed

5 files changed

+15
-30
lines changed

src/basic/sockets.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void sockread_statement(struct basic_ctx* ctx)
7777

7878
input[rv] = 0; // Null-terminate string
7979

80-
switch (var[strlen(var) - 1]) {
80+
switch (var[var_length - 1]) {
8181
case '$':
8282
basic_set_string_variable(var, input, ctx, false, false);
8383
break;
@@ -327,11 +327,12 @@ void udpwrite_statement(struct basic_ctx* ctx) {
327327
if (source_port > 65535 || source_port < 0 || dest_port > 65535 || dest_port < 0) {
328328
tokenizer_error_print(ctx, "Invalid UDP port number");
329329
}
330-
if (strlen(data) == 0 || strlen(data) > 65530) {
330+
size_t len = strlen(data);
331+
if (len == 0 || len > 65530) {
331332
tokenizer_error_print(ctx, "Invalid UDP packet length");
332333
}
333334
uint32_t dest = htonl(str_to_ip(dest_ip));
334-
udp_send_packet((uint8_t*)&dest, source_port, dest_port, (void*)data, strlen(data) + 1); // including the NULL terminator
335+
udp_send_packet((uint8_t*)&dest, source_port, dest_port, (void*)data, len + 1); // including the NULL terminator
335336
}
336337

337338
void udpbind_statement(struct basic_ctx* ctx) {

src/basic/variable.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,12 @@ void local_statement(struct basic_ctx* ctx) {
8282
void newline_statement(struct basic_ctx* ctx) {
8383
}
8484

85-
void assignment_statement(struct basic_ctx* ctx, bool global, bool local)
86-
{
85+
void assignment_statement(struct basic_ctx* ctx, bool global, bool local) {
8786
const char* var;
8887
const char* _expr;
8988
double f_expr = 0;
9089
size_t var_length;
9190

92-
basic_debug("LET statement\n");
93-
9491
var = tokenizer_variable_name(ctx, &var_length);
9592

9693
if (varname_is_int_array_access(ctx, var)) {
@@ -151,12 +148,11 @@ void assignment_statement(struct basic_ctx* ctx, bool global, bool local)
151148
accept_or_return(NEWLINE, ctx);
152149
}
153150

154-
bool valid_suffix_var(const char* name, char suffix)
155-
{
151+
bool valid_suffix_var(const char* name, char suffix) {
156152
const char* i;
157-
unsigned int varLength = strlen(name);
153+
unsigned int var_length = strlen(name);
158154
if (suffix != '\0') {
159-
if (varLength < 2 || name[varLength - 1] != suffix) {
155+
if (var_length < 2 || name[var_length - 1] != suffix) {
160156
return false;
161157
}
162158
size_t offset = 0;
@@ -177,7 +173,7 @@ bool valid_suffix_var(const char* name, char suffix)
177173
}
178174
return true;
179175
}
180-
if (varLength < 1) {
176+
if (var_length < 1) {
181177
return false;
182178
}
183179
size_t offset = 0;

src/block/ahci/sense.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ bool atapi_request_sense6(ahci_hba_port_t* port, ahci_hba_mem_t* abar, uint8_t*
2323

2424
memset(cmdtbl->atapi_command, 0, sizeof(cmdtbl->atapi_command));
2525
scsi_cdb_request_sense6* cdb = (scsi_cdb_request_sense6*)cmdtbl->atapi_command;
26-
cdb->opcode = SCSI_OP_REQUEST_SENSE_6; /* assumed declared in your scsi.h */
26+
cdb->opcode = SCSI_OP_REQUEST_SENSE_6;
2727
cdb->alloc_len = out_len;
2828

2929
if (!issue_and_wait(port, slot, "request sense")) {

src/net/tcp.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,6 @@ bool tcp_state_listen(ip_packet_t *encap_packet, tcp_segment_t *segment, tcp_con
612612
child.snd_wnd = TCP_WINDOW_SIZE;
613613
child.rcv_wnd = TCP_WINDOW_SIZE;
614614

615-
/* House defaults (mirror your other inits). */
616615
child.fd = -1;
617616
child.segment_list = NULL;
618617
child.recv_eof_pos = -1;

src/string.c

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ int strcmp(const char* s1, const char* s2)
3636
return (*(const unsigned char *)s1 - *(const unsigned char *)(s2 - 1));
3737
}
3838

39-
int stricmp(const char* s1, const char* s2)
40-
{
39+
int stricmp(const char* s1, const char* s2) {
4140
if (!s1 || !s2) {
4241
return (int)s1 - (int)s2;
4342
}
@@ -47,8 +46,7 @@ int stricmp(const char* s1, const char* s2)
4746
return (*(const unsigned char *)s1 - *(const unsigned char *)(s2 - 1));
4847
}
4948

50-
char* strchr(const char *s, int c)
51-
{
49+
char* strchr(const char *s, int c) {
5250
if (!s) {
5351
return NULL;
5452
}
@@ -88,35 +86,26 @@ int strnicmp(const char* s1, const char* s2, uint32_t n)
8886

8987
#include <stdint.h>
9088

91-
__attribute__((hot)) int strncmp(const char *s1, const char *s2, uint32_t n)
92-
{
89+
__attribute__((hot)) int strncmp(const char *s1, const char *s2, uint32_t n) {
9390
if (!s1 || !s2) {
94-
return (int)s1 - (int)s2; /* preserve your NULL behaviour */
91+
return (int)s1 - (int)s2;
9592
}
96-
9793
if (s1 == s2 || n == 0) {
9894
return 0;
9995
}
100-
10196
const unsigned char *p1 = (const unsigned char *)s1;
10297
const unsigned char *p2 = (const unsigned char *)s2;
103-
10498
while (n--) {
10599
unsigned char a = *p1++;
106100
unsigned char b = *p2++;
107-
108-
/* Common case: bytes equal and not NUL → continue. */
109101
if (__builtin_expect(a == b, 1)) {
110102
if (__builtin_expect(a != 0u, 1)) {
111103
continue;
112104
}
113-
return 0; /* both NUL at same position */
105+
return 0;
114106
}
115-
116-
/* First difference decides the result. */
117107
return (int)a - (int)b;
118108
}
119-
120109
return 0;
121110
}
122111

0 commit comments

Comments
 (0)