Difference between revisions of "String Parsing Optimization"

From Flexible Survival
Jump to: navigation, search
m (Conditionals)
Line 51: Line 51:
 
[else] creates potential for redundancy if a coder is not paying attention. Here is a small before-and-after list of sentences, followed by clear instruction.
 
[else] creates potential for redundancy if a coder is not paying attention. Here is a small before-and-after list of sentences, followed by clear instruction.
  
<br>
 
----
 
<br>
 
  
 
::''[if time is winter]The large bear returned to his cave to hibernate.[else]The small fox jumped over the fence.[end if]
 
::''[if time is winter]The large bear returned to his cave to hibernate.[else]The small fox jumped over the fence.[end if]
Line 62: Line 59:
 
In the above example, repeated text has been removed from the [if ] statement itself and added as prefix and suffix to the code, respectively. This provides the exact same user-visible output with less characters and processing requirements.
 
In the above example, repeated text has been removed from the [if ] statement itself and added as prefix and suffix to the code, respectively. This provides the exact same user-visible output with less characters and processing requirements.
  
<br>
 
----
 
<br>
 
  
 
::''A woman in tight jeans and an unassuming blouse [if time is night]would normally [end if]man[if time is day]s[end if] the counter.[if time is day] She's twirling a pen in her fingers, seemingly disinterested in her body of work.[else] However, she doesn't appear to be here right now.[end if]
 
::''A woman in tight jeans and an unassuming blouse [if time is night]would normally [end if]man[if time is day]s[end if] the counter.[if time is day] She's twirling a pen in her fingers, seemingly disinterested in her body of work.[else] However, she doesn't appear to be here right now.[end if]
Line 79: Line 73:
 
Second step: Determine whether to simplify the overall parsing. In this case, two separate [if ][else][end if] statements can be merged into one.
 
Second step: Determine whether to simplify the overall parsing. In this case, two separate [if ][else][end if] statements can be merged into one.
  
<br>
 
 
----
 
----
<br>
 
  
 
Protip: Saving memory is more important with our as-of-writing server configuration (11.26.2013) than performance. If splitting up an [if ][else][end if] to two [if ][end if] statements results in any significant reduction of characters, feel free to do so.
 
Protip: Saving memory is more important with our as-of-writing server configuration (11.26.2013) than performance. If splitting up an [if ][else][end if] to two [if ][end if] statements results in any significant reduction of characters, feel free to do so.
<br>
 
 
  
 
=== [if ][else if ][else][end if] ===
 
=== [if ][else if ][else][end if] ===
Line 91: Line 81:
 
[else if ] introduces further potential for code slowdown. Please read the following steps carefully to avoid that.
 
[else if ] introduces further potential for code slowdown. Please read the following steps carefully to avoid that.
  
<br>
 
----
 
<br>
 
  
 
::''You [if [player] is male]try not to grow aroused by the Skunk Girl's sumptuous body, whining in protest.[else if [player] is female]easily ignore the Skunk Girl's attempts to seduce you.[else]easily ignore the Skunk Girl's attempts to seduce you. Neuters 4ever![end if]
 
::''You [if [player] is male]try not to grow aroused by the Skunk Girl's sumptuous body, whining in protest.[else if [player] is female]easily ignore the Skunk Girl's attempts to seduce you.[else]easily ignore the Skunk Girl's attempts to seduce you. Neuters 4ever![end if]
Line 108: Line 95:
 
Second step: Move the trailing periods in the [if ][else][end if] statement before the start of the neuter check.  
 
Second step: Move the trailing periods in the [if ][else][end if] statement before the start of the neuter check.  
  
<br>
 
----
 
<br>
 
  
 
::''The [if time is day]sun is [end if][if time is morning]rising, stirring all forms of life into motion[else if time is afternoon]high, baking the earth with its furious light[else if time is evening]starting to set, casting the land in long shadows[else]moon shines over the land[end if].
 
::''The [if time is day]sun is [end if][if time is morning]rising, stirring all forms of life into motion[else if time is afternoon]high, baking the earth with its furious light[else if time is evening]starting to set, casting the land in long shadows[else]moon shines over the land[end if].
Line 125: Line 109:
 
Step two: Determine the longest word in your [if ] and [else if ] statements, then move that to the [else]. In this case, [else if time is night] is shorter than [else if time is afternoon], allowing for a smidgen of better memory utilization.
 
Step two: Determine the longest word in your [if ] and [else if ] statements, then move that to the [else]. In this case, [else if time is night] is shorter than [else if time is afternoon], allowing for a smidgen of better memory utilization.
  
<br>
 
  
 
=== [case ] ===
 
=== [case ] ===
Line 131: Line 114:
 
Case-statements are useful where lengthy [if ][else if ][end if] statements would result in redundancy.
 
Case-statements are useful where lengthy [if ][else if ][end if] statements would result in redundancy.
  
<br>
 
----
 
<br>
 
  
 
::''[if stat blah/bluh of [player] == 1]A[else if stat blah/bluh of [target] == 2]B[else if stat blah/bluh of [target] == 3]C[else if stat blah/bluh of [target] == 4]D[else if stat blah/bluh of [target] == 5]E[end if]
 
::''[if stat blah/bluh of [player] == 1]A[else if stat blah/bluh of [target] == 2]B[else if stat blah/bluh of [target] == 3]C[else if stat blah/bluh of [target] == 4]D[else if stat blah/bluh of [target] == 5]E[end if]
Line 148: Line 128:
 
Step two: Move equivalent comparison functions to the [case (target)] space. In this example, the equivalent comparison function is '=='.
 
Step two: Move equivalent comparison functions to the [case (target)] space. In this example, the equivalent comparison function is '=='.
  
<br>
 
  
 
== Randomization ==
 
== Randomization ==
Line 158: Line 137:
 
The standard [one of][or][at random] statement is only prone to redundancy.
 
The standard [one of][or][at random] statement is only prone to redundancy.
  
<br>
 
----
 
<br>
 
  
 
::''Sometimes, a man's gotta man. [one of]Other times,[or]Sometimes,[at random] he doesn't.
 
::''Sometimes, a man's gotta man. [one of]Other times,[or]Sometimes,[at random] he doesn't.
Line 169: Line 145:
 
In the above example, redundancy is nixed by moving a string shared by all inputs after [at random].
 
In the above example, redundancy is nixed by moving a string shared by all inputs after [at random].
  
<br>
 
----
 
<br>
 
  
 
=== [random x to y] ===
 
=== [random x to y] ===
Line 177: Line 150:
 
This function is more powerful than [one of][or][at random], lending itself well to optimization. Note that it only accepts numerical input (negatives included) and that 'y' should be greater than 'x'.
 
This function is more powerful than [one of][or][at random], lending itself well to optimization. Note that it only accepts numerical input (negatives included) and that 'y' should be greater than 'x'.
  
<br>
 
----
 
<br>
 
  
 
::''[one of]1[or]2[or]3[or]4[or]5[or]6[at random]
 
::''[one of]1[or]2[or]3[or]4[or]5[or]6[at random]
Line 188: Line 158:
 
A complex [one of][or][at random] statement converted into a [random x to y] statement.
 
A complex [one of][or][at random] statement converted into a [random x to y] statement.
  
<br>
 
----
 
<br>
 
  
 
::''[one of]1[or]1[or]1[or]1[or]1[or]1[or]1[or]1[or]1[or]2[at random]
 
::''[one of]1[or]1[or]1[or]1[or]1[or]1[or]1[or]1[or]1[or]2[at random]
Line 199: Line 166:
 
The above example illustrates how to make one value fire more frequently than the other.
 
The above example illustrates how to make one value fire more frequently than the other.
  
<br>
 
----
 
<br>
 
  
 
::''[one of]1[or]1[or]1[or]1[or]1[or]2[or]2[or]2[or]2[or]3[or]3[or]3[or]4[or]4[or]5[at random]
 
::''[one of]1[or]1[or]1[or]1[or]1[or]2[or]2[or]2[or]2[or]3[or]3[or]3[or]4[or]4[or]5[at random]
Line 212: Line 176:
 
'''Do NOT''' use [if ][else if ][else][end if]. Each roll would be independent and result in skewed output probability.
 
'''Do NOT''' use [if ][else if ][else][end if]. Each roll would be independent and result in skewed output probability.
  
<br>
 
----
 
<br>
 
  
 
::''[one of]Hello sir[or]Hello sir[or]Hello sir[or]Hello sir[or]Hello sir[or]Hi there[or]Hi there[or]Hi there[or]Hi there[or]Ola Mister Cunningham[or]Ola Mister Cunningham[or]Ola Mister Cunningham[or]Heya Cunnin'[or]Heya Cunnin'[or]Good day Mr. Cunningham[at random]
 
::''[one of]Hello sir[or]Hello sir[or]Hello sir[or]Hello sir[or]Hello sir[or]Hi there[or]Hi there[or]Hi there[or]Hi there[or]Ola Mister Cunningham[or]Ola Mister Cunningham[or]Ola Mister Cunningham[or]Heya Cunnin'[or]Heya Cunnin'[or]Good day Mr. Cunningham[at random]

Revision as of 18:39, 30 November 2013


String Parsing is a powerful language for designing content on the Flexible Survival MUD. The language permits read functions, write functions, and allows users to create dynamic content based on a combination of those functions. Its semantics were originally designed by Damaged and later rewritten by Fauna for increased performance.

String Parsing, like other markup languages, does cause a hit in performance when inserted into plain text. This article exists to provide solutions for those seeking to squeeze the most performance out of their code. It will not tell you how to write clean or legible code, NOR will it teach you the fundamental markup of String Parsing. For information on those subjects, see the wiki article on String Parsing and review the in-game manual.


Performance: Why should I care?

Performance is important for a number of reasons.

Optimized code reduces the perceived latency of navigation in a MUD. Doing so should be enjoyable on even the most sluggish of connections.
Optimized code reduces the Flexible Survival server load, thus permitting us to provide more and more detailed user-visible content.
Optimized code reduces the memory cost of programs and #dbrefs. In the case of a fatal memory spike or other unforeseen circumstances, the chances of services ceasing function are reduced when baseline memory is kept to a reasonable minimum.

How does this relate to String Parsing?

String Parsing is a markup language found throughout many aspects of the game. From transformation messages, to sex scenes, to exploration, to NPC interaction, and more, String Parsing is the gold standard for ensuring high quality content with minimal perceived latency and maximum ease of deployment. Despite this, we must keep an eye on our code to ensure continued high quality submissions and end-user satisfaction.

Strictly speaking, a more important language to keep optimized is the MUF backend that Flexible Survival is based upon. However, since this guide is targeted at a general audience and not Mucker-level or higher staff, String Parsing tips and tricks will be the subject matter I'll focus on.

For the subsequent sections, please use the in-game @viewparse command to indent and colorize markup for heightened legibility.

Conditionals

Conditionals are the heart, brain, and skeletal structure of the String Parsing body.

[if ][end if]

A basic [if ] statement is prone to some syntactic errors, but little in terms of optimization woes.


Always use the template [if [target] is <gender>] and [if time is

[if [target] = <gender>]
[if time = <time of day/season>]
[if [target] is <gender>]
[if time is <time of day/season>]

Do not check for integers with the '=' sign. This is a function designed to check a string. Use '==' instead. Benchmarks have shown that checking directly for an integer is both faster and cleaner.

[if stat cocks of [target] = 1]
[if stat cocks of [target] == 1]

Avoid not statements when possible. For example, [if time is not night] is the same as [if time is day].

[if time is not night]
[if time is not day]
[if time is day]
[if time is night]


[if ][else][end if]

[else] creates potential for redundancy if a coder is not paying attention. Here is a small before-and-after list of sentences, followed by clear instruction.


[if time is winter]The large bear returned to his cave to hibernate.[else]The small fox jumped over the fence.[end if]
The [if time is winter]large bear returned to his cave to hibernat[else]small fox jumped over the fenc[end if]e.


In the above example, repeated text has been removed from the [if ] statement itself and added as prefix and suffix to the code, respectively. This provides the exact same user-visible output with less characters and processing requirements.


A woman in tight jeans and an unassuming blouse [if time is night]would normally [end if]man[if time is day]s[end if] the counter.[if time is day] She's twirling a pen in her fingers, seemingly disinterested in her body of work.[else] However, she doesn't appear to be here right now.[end if]
A woman in tight jeans and an unassuming blouse [if time is day]mans[else]would normally man[end if] the counter. [if time is day]She's twirling a pen in her fingers, seemingly disinterested in her body of work[else]However, she doesn't appear to be here right now[end if].
A woman in tight jeans and an unassuming blouse [if time is day]mans the counter. She's twirling a pen in her fingers, seemingly disinterested in her body of work[else]would normally man the counter. However, she doesn't appear to be here right now[end if].


In the above example, I've illustrated a two-step series of optimizations.

First step: Move a repeated space at the beginning of the final [if ][else][end if] statement. Move repeated periods in that same statement to the end. Merge two independent [if ][end if] statements into a singular [if ][else][end if] statement.

Second step: Determine whether to simplify the overall parsing. In this case, two separate [if ][else][end if] statements can be merged into one.


Protip: Saving memory is more important with our as-of-writing server configuration (11.26.2013) than performance. If splitting up an [if ][else][end if] to two [if ][end if] statements results in any significant reduction of characters, feel free to do so.

[if ][else if ][else][end if]

[else if ] introduces further potential for code slowdown. Please read the following steps carefully to avoid that.


You [if [player] is male]try not to grow aroused by the Skunk Girl's sumptuous body, whining in protest.[else if [player] is female]easily ignore the Skunk Girl's attempts to seduce you.[else]easily ignore the Skunk Girl's attempts to seduce you. Neuters 4ever![end if]
You [if [player] is male]try not to grow aroused by the Skunk Girl's sumptuous body, whining in protest.[else]easily ignore the Skunk Girl's attempts to seduce you.[end if][if [player] is neuter] Neuters 4ever![end if]
You [if [player] is male]try not to grow aroused by the Skunk Girl's sumptuous body, whining in protest[else]easily ignore the Skunk Girl's attempts to seduce you[end if].[if [player] is neuter] Neuters 4ever![end if]


In the above example, a lengthy [if ][else if ][else][end if] statement is deconstructed into [if ][else][end if][if ][end if].

First step: Converge the [else if ] and [else] statements into a single [else]. Move the neuter check into a separate [if ] statement at the end.

Second step: Move the trailing periods in the [if ][else][end if] statement before the start of the neuter check.


The [if time is day]sun is [end if][if time is morning]rising, stirring all forms of life into motion[else if time is afternoon]high, baking the earth with its furious light[else if time is evening]starting to set, casting the land in long shadows[else]moon shines over the land[end if].
The [if time is morning]sun is rising, stirring all forms of life into motion[else if time is afternoon]sun is high, baking the earth with its furious light[else if time is evening]sun is starting to set, casting the land in long shadows[else]moon shines over the land[end if].
The [if time is evening]sun is starting to set, casting the land in long shadows[else if time is night]moon shines over the land[else if time is morning]sun is rising, stirring all forms of life into motion[else]sun is high, baking the earth with its furious light[end if].


In the above example, an overly complicated set of statements is simplified, then reduced in length.

Step one: Always determine whether avoiding redundancy is actually worth doing so. Surrounding the redundant statement with an [if ][end if] clause does NOT save character spaces and requires more time to process. Remove this, then add the statement to all day-related [if ] and [else if ] statements.

Step two: Determine the longest word in your [if ] and [else if ] statements, then move that to the [else]. In this case, [else if time is night] is shorter than [else if time is afternoon], allowing for a smidgen of better memory utilization.


[case ]

Case-statements are useful where lengthy [if ][else if ][end if] statements would result in redundancy.


[if stat blah/bluh of [player] == 1]A[else if stat blah/bluh of [target] == 2]B[else if stat blah/bluh of [target] == 3]C[else if stat blah/bluh of [target] == 4]D[else if stat blah/bluh of [target] == 5]E[end if]
[case stat blah/bluh of [player]][when (target) == 1]A[end when][when (target) == 2]B[end when][when (target) == 3]C[end when][when (target) == 4]D[end when][when (target) == 5]E[end when][end case]
[case stat blah/bluh of [player] ==][when (target) 1]A[end when][when (target) 2]B[end when][when (target) 3]C[end when][when (target) 4]D[end when][when (target) 5]E[end when][end case]


In the above example, a complex [if ][else if ][end if] statement is optimized in a two-step process:

Step one: Convert the code to a basic case-statement.

Step two: Move equivalent comparison functions to the [case (target)] space. In this example, the equivalent comparison function is '=='.


Randomization

The following functions display content at random. Input is separated by [or].

[one of][or][at random]

The standard [one of][or][at random] statement is only prone to redundancy.


Sometimes, a man's gotta man. [one of]Other times,[or]Sometimes,[at random] he doesn't.
Sometimes, a man's gotta man. [one of]Other [or]Some[at random]times, he doesn't.


In the above example, redundancy is nixed by moving a string shared by all inputs after [at random].


[random x to y]

This function is more powerful than [one of][or][at random], lending itself well to optimization. Note that it only accepts numerical input (negatives included) and that 'y' should be greater than 'x'.


[one of]1[or]2[or]3[or]4[or]5[or]6[at random]
[random 1 to 6]


A complex [one of][or][at random] statement converted into a [random x to y] statement.


[one of]1[or]1[or]1[or]1[or]1[or]1[or]1[or]1[or]1[or]2[at random]
[if [random 1 to 10] < 10]1[else]2[end if]


The above example illustrates how to make one value fire more frequently than the other.


[one of]1[or]1[or]1[or]1[or]1[or]2[or]2[or]2[or]2[or]3[or]3[or]3[or]4[or]4[or]5[at random]
[case [random 1 to 15] <][when (target) 6]1[end when][when (target) 10]2[end when][when (target) 13]3[end when][when (target) 15]4[end when][when 1]5[end when][end case]


Do NOT try to be too fancy with [random x to y]. The above example illustrates how a case-statement can result in longer code.

Do NOT use [if ][else if ][else][end if]. Each roll would be independent and result in skewed output probability.


[one of]Hello sir[or]Hello sir[or]Hello sir[or]Hello sir[or]Hello sir[or]Hi there[or]Hi there[or]Hi there[or]Hi there[or]Ola Mister Cunningham[or]Ola Mister Cunningham[or]Ola Mister Cunningham[or]Heya Cunnin'[or]Heya Cunnin'[or]Good day Mr. Cunningham[at random]
[case [random 1 to 15] <][when (target) 6]Hello sir[end when][when (target) 10]Hi there[end when][when (target) 13]Ola Mister Cunningham[end when][when (target) 15]Heya Cunnin'[end when][when 1][end when]Good day Mr. Cunningham[end case]


The above example illustrates an overall reduction of characters using [case ] with longer input. A general rule of thumb is that the longer the (target) and input values are, the more likely [case ] is to reduce overall characters needed.






This document is a work in progress and is thus subject to change. All views and opinions expressed in here are solely Songbird's. Please only modify with either A), explicit permission to do so, or B), a fix for known factual inaccuracy / grammar errors.