g_paste_util_replace: use g_string_replace instead of g_regex_replace_literal #445
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When using gpaste with storage backup, either selecting a history object for usage or saving a new one will cause the whole history to be converted to xml and stored back to the filesystem.
This conversion is usually fast, but if the user keeps a lot of history saved up, it can take up a couple seconds to convert / save.
When selecting a history object, though, this delay can be noticed by the user, since it takes that much time for the selected object to be available in the clipboard.
When verifying with perf, a lot of time is being taken by g_paste_util_xml_encode, moslty by running g_regex_replace_literal in g_paste_util_replace.
g_paste_util_replace, on the other hand, is only used to replace simple strings. Also, it calls g_regex_escape_string() to make sure all possible regex is not enabled in the replacing pattern, effectively causing it to just replace one string by the other.
In this case, why not replace it directly using g_string_replace instead? It's much faster, considering a history of 27000 itens:
With g_string_replace(), this patch's suggestion:
g_paste_storage_backend_write_history: took 364 miliseconds to run g_paste_storage_backend_write_history: took 364 miliseconds to run g_paste_storage_backend_write_history: took 364 miliseconds to run g_paste_storage_backend_write_history: took 365 miliseconds to run g_paste_storage_backend_write_history: took 362 miliseconds to run
With g_regex_replace_literal():
g_paste_storage_backend_write_history: took 1132 miliseconds to run g_paste_storage_backend_write_history: took 1135 miliseconds to run g_paste_storage_backend_write_history: took 1138 miliseconds to run g_paste_storage_backend_write_history: took 1128 miliseconds to run g_paste_storage_backend_write_history: took 1133 miliseconds to run
Saving almost 70% of cpu usage, and making it much more confortable to the user.