rohc team mailing list archive
-
rohc team
-
Mailing list archive
-
Message #00524
Optimisation: less strlen() calls
Hello,
Functions printf(), sprintf(), vsprintf() return the number of
characters copied into the buffer. So, it is not useful to call strlen()
after calling one of these functions.
So, I replace code like:
sprintf(buffer,"sjmlsdj %x sdfsj",ddfff);
buffer += strlen(buffer);
with:
buffer += sprintf(buffer,"sjmlsdj %x sdfsj",ddfff);
Attached is a file patch for current branch.
Best regards,
FWX.
=== modified file 'src/comp/rohc_comp.c'
--- src/comp/rohc_comp.c 2012-05-26 15:17:53 +0000
+++ src/comp/rohc_comp.c 2012-06-13 14:57:06 +0000
@@ -753,23 +753,18 @@
save = buffer;
buffer += strlen(buffer);
- sprintf(buffer, "<profiles>\n");
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "<profiles>\n");
for(i = 0; i < C_NUM_PROFILES; i++)
{
- sprintf(buffer, "\t<profile id=\"%d\" ", c_profiles[i]->id);
- buffer += strlen(buffer);
- sprintf(buffer, "name=\"%s\" ", c_profiles[i]->description);
- buffer += strlen(buffer);
- sprintf(buffer, "/>\n");
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "\t<profile id=\"%d\" ", c_profiles[i]->id);
+ buffer += sprintf(buffer, "name=\"%s\" ", c_profiles[i]->description);
+ buffer += sprintf(buffer, "/>\n");
}
- sprintf(buffer, "</profiles>\n");
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "</profiles>\n");
- return strlen(save);
+ return buffer - save;
}
@@ -807,19 +802,13 @@
save = buffer;
buffer += strlen(buffer);
- sprintf(buffer, "%s<instance>\n", prefix);
- buffer += strlen(buffer);
- sprintf(buffer, "%s\t<creator>%s</creator>\n", prefix,
+ buffer += sprintf(buffer, "%s<instance>\n", prefix);
+ buffer += sprintf(buffer, "%s\t<creator>%s</creator>\n", prefix,
PACKAGE_NAME " (" PACKAGE_URL ")");
- buffer += strlen(buffer);
- sprintf(buffer, "%s\t<version>%s</version>\n", prefix, PACKAGE_VERSION);
- buffer += strlen(buffer);
- sprintf(buffer, "%s\t<status>%s</status>\n", prefix, comp->enabled ? "enabled" : "disabled");
- buffer += strlen(buffer);
- sprintf(buffer, "%s\t<flows>%d</flows>\n", prefix, comp->num_contexts_used);
- buffer += strlen(buffer);
- sprintf(buffer, "%s\t<packets>%d</packets>\n", prefix, comp->num_packets);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t<version>%s</version>\n", prefix, PACKAGE_VERSION);
+ buffer += sprintf(buffer, "%s\t<status>%s</status>\n", prefix, comp->enabled ? "enabled" : "disabled");
+ buffer += sprintf(buffer, "%s\t<flows>%d</flows>\n", prefix, comp->num_contexts_used);
+ buffer += sprintf(buffer, "%s\t<packets>%d</packets>\n", prefix, comp->num_packets);
if(comp->total_uncompressed_size != 0)
{
@@ -829,40 +818,28 @@
{
v = 0;
}
- sprintf(buffer, "%s\t<compression_ratio>%d%%</compression_ratio>\n", prefix, v);
- buffer += strlen(buffer);
- sprintf(buffer, "%s\t<max_cid>%d</max_cid>\n", prefix, comp->medium.max_cid);
- buffer += strlen(buffer);
- sprintf(buffer, "%s\t<mrru>%d</mrru>\n", prefix, comp->mrru);
- buffer += strlen(buffer);
- sprintf(buffer, "%s\t<large_cid>%s</large_cid>\n", prefix,
- comp->medium.cid_type == ROHC_LARGE_CID ? "yes" : "no");
- buffer += strlen(buffer);
- sprintf(buffer, "%s\t<connection_type>%d</connection_type>\n", prefix, 3);
- buffer += strlen(buffer);
- sprintf(buffer, "%s\t<feedback_freq>%d</feedback_freq>\n\n", prefix, 7); // comp-> ??
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t<compression_ratio>%d%%</compression_ratio>\n", prefix, v);
+ buffer += sprintf(buffer, "%s\t<max_cid>%d</max_cid>\n", prefix, comp->medium.max_cid);
+ buffer += sprintf(buffer, "%s\t<mrru>%d</mrru>\n", prefix, comp->mrru);
+ buffer += sprintf(buffer, "%s\t<large_cid>%s</large_cid>\n", prefix,
+ comp->medium.cid_type == ROHC_LARGE_CID ? "yes" : "no");
+ buffer += sprintf(buffer, "%s\t<connection_type>%d</connection_type>\n", prefix, 3);
+ buffer += sprintf(buffer, "%s\t<feedback_freq>%d</feedback_freq>\n\n", prefix, 7); // comp-> ??
/* profiles part */
- sprintf(buffer, "%s\t<profiles>\n", prefix);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t<profiles>\n", prefix);
for(i = 0; i < C_NUM_PROFILES; i++)
{
p = c_profiles[i];
- sprintf(buffer, "%s\t\t<profile id=\"%d\" ", prefix, p->id);
- buffer += strlen(buffer);
- sprintf(buffer, "name=\"%s\" ", p->description);
- buffer += strlen(buffer);
- sprintf(buffer, "active=\"%s\" ", comp->profiles[i] ? "yes" : "no");
- buffer += strlen(buffer);
- sprintf(buffer, "/>\n");
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t\t<profile id=\"%d\" ", prefix, p->id);
+ buffer += sprintf(buffer, "name=\"%s\" ", p->description);
+ buffer += sprintf(buffer, "active=\"%s\" ", comp->profiles[i] ? "yes" : "no");
+ buffer += sprintf(buffer, "/>\n");
}
- sprintf(buffer, "%s\t</profiles>\n", prefix);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t</profiles>\n", prefix);
/* contexts part */
i = 0;
@@ -872,13 +849,12 @@
}
buffer += strlen(buffer);
- sprintf(buffer, "%s</instance>\n\n", prefix);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s</instance>\n\n", prefix);
/* clean the indent prefix */
zfree(prefix);
- return strlen(save);
+ return buffer - save;
}
@@ -927,22 +903,16 @@
save = buffer;
buffer += strlen(buffer);
- sprintf(buffer, "\n%s<context type=\"compressor\" cid=\"%d\">\n", prefix, c->cid);
- buffer += strlen(buffer);
- sprintf(buffer, "%s\t<cid_state>%s</cid_state>\n", prefix, c->used ? "USED" : "UNUSED");
- buffer += strlen(buffer);
- sprintf(buffer, "%s\t<state>%s</state>\n", prefix,
- rohc_comp_get_state_descr(c->state));
- buffer += strlen(buffer);
- sprintf(buffer, "%s\t<mode>%s</mode>\n", prefix,
- rohc_get_mode_descr(c->mode));
- buffer += strlen(buffer);
- sprintf(buffer, "%s\t<profile>%s</profile>\n", prefix, c->profile->description);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "\n%s<context type=\"compressor\" cid=\"%d\">\n", prefix, c->cid);
+ buffer += sprintf(buffer, "%s\t<cid_state>%s</cid_state>\n", prefix, c->used ? "USED" : "UNUSED");
+ buffer += sprintf(buffer, "%s\t<state>%s</state>\n", prefix,
+ rohc_comp_get_state_descr(c->state));
+ buffer += sprintf(buffer, "%s\t<mode>%s</mode>\n", prefix,
+ rohc_get_mode_descr(c->mode));
+ buffer += sprintf(buffer, "%s\t<profile>%s</profile>\n", prefix, c->profile->description);
/* compression ratio */
- sprintf(buffer, "%s\t<ratio>\n", prefix);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t<ratio>\n", prefix);
if(c->total_uncompressed_size != 0)
{
@@ -952,8 +922,7 @@
{
v = 0;
}
- sprintf(buffer, "%s\t\t<all_packets>%d%%</all_packets>\n", prefix, v);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t\t<all_packets>%d%%</all_packets>\n", prefix, v);
if(c->header_uncompressed_size != 0)
{
@@ -963,31 +932,26 @@
{
v = 0;
}
- sprintf(buffer, "%s\t\t<all_headers>%d%%</all_headers>\n", prefix, v);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t\t<all_headers>%d%%</all_headers>\n", prefix, v);
v = c_sum_wlsb(c->total_16_uncompressed);
if(v != 0)
{
v = (100 * c_sum_wlsb(c->total_16_compressed)) / v;
}
- sprintf(buffer, "%s\t\t<last_16_packets>%d%%</last_16_packets>\n", prefix, v);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t\t<last_16_packets>%d%%</last_16_packets>\n", prefix, v);
v = c_sum_wlsb(c->header_16_uncompressed);
if(v != 0)
{
v = (100 * c_sum_wlsb(c->header_16_compressed)) / v;
}
- sprintf(buffer, "%s\t\t<last_16_headers>%d%%</last_16_headers>\n", prefix, v);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t\t<last_16_headers>%d%%</last_16_headers>\n", prefix, v);
- sprintf(buffer, "%s\t</ratio>\n", prefix);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t</ratio>\n", prefix);
/* compression mean */
- sprintf(buffer, "%s\t<mean>\n", prefix);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t<mean>\n", prefix);
if(c->num_sent_packets != 0)
{
@@ -997,8 +961,7 @@
{
v = 0;
}
- sprintf(buffer, "%s\t\t<all_packets>%d</all_packets>\n", prefix, v);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t\t<all_packets>%d</all_packets>\n", prefix, v);
if(c->num_sent_packets != 0)
{
@@ -1008,43 +971,32 @@
{
v = 0;
}
- sprintf(buffer, "%s\t\t<all_headers>%d</all_headers>\n", prefix, v);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t\t<all_headers>%d</all_headers>\n", prefix, v);
v = c_mean_wlsb(c->total_16_compressed);
- sprintf(buffer, "%s\t\t<last_16_packets>%d</last_16_packets>\n", prefix, v);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t\t<last_16_packets>%d</last_16_packets>\n", prefix, v);
v = c_mean_wlsb(c->header_16_compressed);
- sprintf(buffer, "%s\t\t<last_16_headers>%d</last_16_headers>\n", prefix, v);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t\t<last_16_headers>%d</last_16_headers>\n", prefix, v);
- sprintf(buffer, "%s\t</mean>\n", prefix);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t</mean>\n", prefix);
/* times */
- sprintf(buffer, "%s\t<activation_time>%u</activation_time>\n",
- prefix, (get_milliseconds() - c->first_used) / 1000 );
- buffer += strlen(buffer);
- sprintf(buffer, "%s\t<idle_time>%u</idle_time>\n",
- prefix, (get_milliseconds() - c->latest_used) / 1000);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t<activation_time>%u</activation_time>\n",
+ prefix, (get_milliseconds() - c->first_used) / 1000 );
+ buffer += sprintf(buffer, "%s\t<idle_time>%u</idle_time>\n",
+ prefix, (get_milliseconds() - c->latest_used) / 1000);
/* packets */
- sprintf(buffer, "%s\t<packets sent_total=\"%d\" ", prefix, c->num_sent_packets);
- buffer += strlen(buffer);
- sprintf(buffer, "sent_ir=\"%d\" ", c->num_sent_ir);
- buffer += strlen(buffer);
- sprintf(buffer, "sent_irdyn=\"%d\" ", c->num_sent_ir_dyn);
- buffer += strlen(buffer);
- sprintf(buffer, "recv_feedback=\"%d\" />\n", c->num_recv_feedbacks);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t<packets sent_total=\"%d\" ", prefix, c->num_sent_packets);
+ buffer += sprintf(buffer, "sent_ir=\"%d\" ", c->num_sent_ir);
+ buffer += sprintf(buffer, "sent_irdyn=\"%d\" ", c->num_sent_ir_dyn);
+ buffer += sprintf(buffer, "recv_feedback=\"%d\" />\n", c->num_recv_feedbacks);
- sprintf(buffer, "%s</context>\n", prefix);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s</context>\n", prefix);
free(prefix);
- return strlen(save);
+ return buffer - save;
}
=== modified file 'src/decomp/rohc_decomp.c'
--- src/decomp/rohc_decomp.c 2012-05-26 20:01:08 +0000
+++ src/decomp/rohc_decomp.c 2012-06-13 15:04:03 +0000
@@ -1138,29 +1138,22 @@
save = buffer;
buffer += strlen(buffer);
- sprintf(buffer, "%s<instance>\n", prefix);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s<instance>\n", prefix);
/* add the profiles part */
- sprintf(buffer, "%s\t<profiles>\n", prefix);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t<profiles>\n", prefix);
for(i = 0; i < D_NUM_PROFILES; i++)
{
p = d_profiles[i];
- sprintf(buffer, "%s\t\t<profile ", prefix);
- buffer += strlen(buffer);
- sprintf(buffer, "id=\"%d\" ", p->id);
- buffer += strlen(buffer);
- sprintf(buffer, "name=\"%s\" ", p->description);
- buffer += strlen(buffer);
- sprintf(buffer, "active=\"yes\" />\n");
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t\t<profile ", prefix);
+ buffer += sprintf(buffer, "id=\"%d\" ", p->id);
+ buffer += sprintf(buffer, "name=\"%s\" ", p->description);
+ buffer += sprintf(buffer, "active=\"yes\" />\n");
}
- sprintf(buffer, "%s\t</profiles>\n", prefix);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t</profiles>\n", prefix);
/* add the contexts part */
i = 0;
@@ -1170,13 +1163,12 @@
}
buffer += strlen(buffer);
- sprintf(buffer, "%s</instance>\n\n", prefix);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s</instance>\n\n", prefix);
/* clean the indent prefix */
zfree(prefix);
- return strlen(save);
+ return buffer - save;
}
@@ -1226,20 +1218,15 @@
save = buffer;
buffer += strlen(buffer);
- sprintf(buffer, "\n%s<context type=\"decompressor\" cid=\"%d\">\n", prefix, index);
- buffer += strlen(buffer);
- sprintf(buffer, "%s\t<state>%s</state>\n", prefix,
- rohc_decomp_get_state_descr(c->state));
- buffer += strlen(buffer);
- sprintf(buffer, "%s\t<mode>%s</mode>\n", prefix,
- rohc_get_mode_descr(c->mode));
- buffer += strlen(buffer);
- sprintf(buffer, "%s\t<profile>%s</profile>\n", prefix, c->profile->description);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "\n%s<context type=\"decompressor\" cid=\"%d\">\n", prefix, index);
+ buffer += sprintf(buffer, "%s\t<state>%s</state>\n", prefix,
+ rohc_decomp_get_state_descr(c->state));
+ buffer += sprintf(buffer, "%s\t<mode>%s</mode>\n", prefix,
+ rohc_get_mode_descr(c->mode));
+ buffer += sprintf(buffer, "%s\t<profile>%s</profile>\n", prefix, c->profile->description);
/* compression ratio */
- sprintf(buffer, "%s\t<ratio>\n", prefix);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t<ratio>\n", prefix);
if(c->total_uncompressed_size != 0)
{
@@ -1249,8 +1236,7 @@
{
v = 0;
}
- sprintf(buffer, "%s\t\t<all_packets>%d%%</all_packets>\n", prefix, v);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t\t<all_packets>%d%%</all_packets>\n", prefix, v);
if(c->header_uncompressed_size != 0)
{
@@ -1260,84 +1246,63 @@
{
v = 0;
}
- sprintf(buffer, "%s\t\t<all_headers>%d%%</all_headers>\n", prefix, v);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t\t<all_headers>%d%%</all_headers>\n", prefix, v);
v = c_sum_wlsb(c->total_16_uncompressed);
if(v != 0)
{
v = (100 * c_sum_wlsb(c->total_16_compressed)) / v;
}
- sprintf(buffer, "%s\t\t<last_16_packets>%d%%</last_16_packets>\n", prefix, v);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t\t<last_16_packets>%d%%</last_16_packets>\n", prefix, v);
v = c_sum_wlsb(c->header_16_uncompressed);
if(v != 0)
{
v = (100 * c_sum_wlsb(c->header_16_compressed)) / v;
}
- sprintf(buffer, "%s\t\t<last_16_headers>%d%%</last_16_headers>\n", prefix, v);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t\t<last_16_headers>%d%%</last_16_headers>\n", prefix, v);
- sprintf(buffer, "%s\t</ratio>\n", prefix);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t</ratio>\n", prefix);
/* compression mean */
- sprintf(buffer, "%s\t<mean>\n", prefix);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t<mean>\n", prefix);
v = c->total_compressed_size / c->num_recv_packets;
- sprintf(buffer, "%s\t\t<all_packets>%d</all_packets>\n", prefix, v);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t\t<all_packets>%d</all_packets>\n", prefix, v);
v = c->header_compressed_size / c->num_recv_packets;
- sprintf(buffer, "%s\t\t<all_headers>%d</all_headers>\n", prefix, v);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t\t<all_headers>%d</all_headers>\n", prefix, v);
v = c_mean_wlsb(c->total_16_compressed);
- sprintf(buffer, "%s\t\t<last_16_packets>%d</last_16_packets>\n", prefix, v);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t\t<last_16_packets>%d</last_16_packets>\n", prefix, v);
v = c_mean_wlsb(c->header_16_compressed);
- sprintf(buffer, "%s\t\t<last_16_headers>%d</last_16_headers>\n", prefix, v);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t\t<last_16_headers>%d</last_16_headers>\n", prefix, v);
- sprintf(buffer, "%s\t</mean>\n", prefix);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t</mean>\n", prefix);
/* times */
- sprintf(buffer, "%s\t<activation_time>%u</activation_time>\n",
- prefix, (get_milliseconds() - c->first_used) / 1000 );
- buffer += strlen(buffer);
- sprintf(buffer, "%s\t<idle_time>%u</idle_time>\n",
- prefix, (get_milliseconds() - c->latest_used) / 1000);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t<activation_time>%u</activation_time>\n",
+ prefix, (get_milliseconds() - c->first_used) / 1000 );
+ buffer += sprintf(buffer, "%s\t<idle_time>%u</idle_time>\n",
+ prefix, (get_milliseconds() - c->latest_used) / 1000);
/* packets */
- sprintf(buffer, "%s\t<packets recv_total=\"%d\" ", prefix, c->num_recv_packets);
- buffer += strlen(buffer);
- sprintf(buffer, "recv_ir=\"%d\" ", c->num_recv_ir);
- buffer += strlen(buffer);
- sprintf(buffer, "recv_irdyn=\"%d\" ", c->num_recv_ir_dyn);
- buffer += strlen(buffer);
- sprintf(buffer, "sent_feedback=\"%d\" />\n", c->num_sent_feedbacks);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t<packets recv_total=\"%d\" ", prefix, c->num_recv_packets);
+ buffer += sprintf(buffer, "recv_ir=\"%d\" ", c->num_recv_ir);
+ buffer += sprintf(buffer, "recv_irdyn=\"%d\" ", c->num_recv_ir_dyn);
+ buffer += sprintf(buffer, "sent_feedback=\"%d\" />\n", c->num_sent_feedbacks);
/* failures/repairs */
- sprintf(buffer, "%s\t<decomp>\n", prefix);
- buffer += strlen(buffer);
- sprintf(buffer, "%s\t\t<failures>%d</failures>\n", prefix, c->num_decomp_failures);
- buffer += strlen(buffer);
- sprintf(buffer, "%s\t\t<repairs>%d</repairs>\n", prefix, c->num_decomp_repairs);
- buffer += strlen(buffer);
- sprintf(buffer, "%s\t</decomp>\n", prefix);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s\t<decomp>\n", prefix);
+ buffer += sprintf(buffer, "%s\t\t<failures>%d</failures>\n", prefix, c->num_decomp_failures);
+ buffer += sprintf(buffer, "%s\t\t<repairs>%d</repairs>\n", prefix, c->num_decomp_repairs);
+ buffer += sprintf(buffer, "%s\t</decomp>\n", prefix);
- sprintf(buffer, "%s</context>\n", prefix);
- buffer += strlen(buffer);
+ buffer += sprintf(buffer, "%s</context>\n", prefix);
free(prefix);
- return strlen(save);
+ return buffer - save;
}
Follow ups