Skip to content

Commit

Permalink
refactor stumpless_get_prival_string
Browse files Browse the repository at this point in the history
Removes a call to snprintf, replacing it with faster memcpy calls.
  • Loading branch information
Caselhos authored Aug 3, 2024
1 parent 4162d1d commit fb0d2e0
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/prival.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,19 @@ stumpless_get_prival_string( int prival ) {

severity = stumpless_get_severity_string( get_severity( prival ) );
facility = stumpless_get_facility_string( get_facility( prival ) );

size_t len_severity = strlen(severity); // to not call strlen() mutiples times, unsure about impact
size_t len_facility = strlen(facility);

// +3 for formatting, +1 for termination
prival_string_size = ( strlen( severity ) + strlen( facility ) + 4);
prival_string_size = ( len_severity + len_facility + 4);
prival_string = alloc_mem( prival_string_size );

snprintf( prival_string, prival_string_size, "%s | %s", severity, facility );


memcpy( prival_string, severity , len_severity);
memcpy( prival_string + len_severity, " | ", 3); // 3 is the size of " | "
memcpy( prival_string + len_severity + 3, facility, len_facility);
memcpy( prival_string + len_severity + 3 + len_facility, "\0", 1);

return prival_string;
}

Expand Down

0 comments on commit fb0d2e0

Please sign in to comment.