From 679a0c6442762aff0c831e5887e7cddea453185c Mon Sep 17 00:00:00 2001 From: Fluffy <65877598+FluffyGhoster@users.noreply.github.com> Date: Thu, 4 Apr 2024 12:45:26 +0200 Subject: [PATCH] Optimized some procs (#18766) Optimized some procs, ported from Daedalus Dock --- code/__HELPERS/text.dm | 40 +++++++---------- code/modules/mob/mob_helpers.dm | 44 ++++++++----------- .../fluffyghost-someprocoptimizations.yml | 41 +++++++++++++++++ 3 files changed, 74 insertions(+), 51 deletions(-) create mode 100644 html/changelogs/fluffyghost-someprocoptimizations.yml diff --git a/code/__HELPERS/text.dm b/code/__HELPERS/text.dm index 7a400e884f6..ab47363a992 100644 --- a/code/__HELPERS/text.dm +++ b/code/__HELPERS/text.dm @@ -392,22 +392,21 @@ //is in the other string at the same spot (assuming it is not a replace char). //This is used for fingerprints /proc/stringmerge(text,compare,replace = "*") - var/newtext = text if(length(text) != length(compare)) - return 0 - for(var/i = 1, i < length(text), i++) - var/a = copytext(text,i,i+1) - var/b = copytext(compare,i,i+1) - //if it isn't both the same letter, or if they are both the replacement character - //(no way to know what it was supposed to be) - if(a != b) - if(a == replace) //if A is the replacement char - newtext = copytext(newtext,1,i) + b + copytext(newtext, i+1) - else if(b == replace) //if B is the replacement char - newtext = copytext(newtext,1,i) + a + copytext(newtext, i+1) - else //The lists disagree, Uh-oh! - return 0 - return newtext + CRASH("Stringmerge received strings of differing lengths") + + var/list/text_chars = splittext_char(text, "") + var/list/compare_chars = splittext_char(compare, "") + var/text_char + var/compare_char + for(var/i in 1 to length(text_chars)) + text_char = text_chars[i] + compare_char = compare_chars[i] + if(text_char == compare_char) + continue + if(text_char == replace) + text_chars[i] = compare_char + return jointext(text_chars, "") /** @@ -439,16 +438,7 @@ * * character - The character you want to know how many are in the string */ /proc/charcount(text, character = "*") - if(!text || !character) - return 0 - var/count = 0 - var/lentext = length(text) - var/a = "" - for(var/i = 1, i <= lentext, i += length(a)) - a = text[i] - if(a == character) - count++ - return count + return length(splittext_char(text, character)) - 1 /proc/reverse_text(text = "") diff --git a/code/modules/mob/mob_helpers.dm b/code/modules/mob/mob_helpers.dm index abf7cf20e51..cb6229479e8 100644 --- a/code/modules/mob/mob_helpers.dm +++ b/code/modules/mob/mob_helpers.dm @@ -353,32 +353,24 @@ var/list/global/organ_rel_size = list( return pick(base_miss_chance) return zone -/proc/stars(n, pr) - if (pr == null) - pr = 25 - if (pr <= 0) - return null - else - if (pr >= 100) - return n - var/te = n - var/t = "" - n = length(n) - var/p = null - p = 1 - var/intag = 0 - while(p <= n) - var/char = copytext_char(te, p, p + 1) - if (char == "<") //let's try to not break tags - intag = !intag - if (intag || char == " " || prob(pr)) - t = text("[][]", t, char) - else - t = text("[]*", t) - if (char == ">") - intag = !intag - p++ - return t +/** + * Convert random parts of a passed in message to stars + * + * * phrase - the string to convert + * * probability - probability any character gets changed + * + * This proc is dangerously laggy, avoid it or die + */ +/proc/stars(phrase, probability = 25) + if(length(phrase) == 0) + return + + var/list/chars = splittext_char(html_decode(phrase), "") + for(var/i in 1 to length(chars)) + if(chars[i] == " " || !prob(probability)) + continue + chars[i] = "*" + return sanitize(jointext(chars, "")) /proc/slur(phrase, strength = 100) phrase = html_decode(phrase) diff --git a/html/changelogs/fluffyghost-someprocoptimizations.yml b/html/changelogs/fluffyghost-someprocoptimizations.yml new file mode 100644 index 00000000000..5233c203343 --- /dev/null +++ b/html/changelogs/fluffyghost-someprocoptimizations.yml @@ -0,0 +1,41 @@ +################################ +# Example Changelog File +# +# Note: This file, and files beginning with ".", and files that don't end in ".yml" will not be read. If you change this file, you will look really dumb. +# +# Your changelog will be merged with a master changelog. (New stuff added only, and only on the date entry for the day it was merged.) +# When it is, any changes listed below will disappear. +# +# Valid Prefixes: +# bugfix +# wip (For works in progress) +# tweak +# soundadd +# sounddel +# rscadd (general adding of nice things) +# rscdel (general deleting of nice things) +# imageadd +# imagedel +# maptweak +# spellcheck (typo fixes) +# experiment +# balance +# admin +# backend +# security +# refactor +################################# + +# Your name. +author: FluffyGhost + +# Optional: Remove this file after generating master changelog. Useful for PR changelogs that won't get used again. +delete-after: True + +# Any changes you've made. See valid prefix list above. +# INDENT WITH TWO SPACES. NOT TABS. SPACES. +# SCREW THIS UP AND IT WON'T WORK. +# Also, all entries are changed into a single [] after a master changelog generation. Just remove the brackets when you add new entries. +# Please surround your changes in double quotes ("), as certain characters otherwise screws up compiling. The quotes will not show up in the changelog. +changes: + - backend: "Optimized some procs, ported from Daedalus Dock."