Files
Bubberstation/code/modules/paperwork
Timberpoes 93025aca76 Fixes a major source of papercode lag and optimises paper for less (but still some) lag. (#73628)
## About The Pull Request

Doesn't quite fix but helps mitigate #71697 - The issue report is still
valid, just less so.

Marked.js is just... Slow when used in BYOND's web browsing environment.
New markdown elements **and HTML tags** add significant fractions of a
millisecond to parsing time on my local.

However, I suspect I messed up with using the custom extensions and
every parsing pass it may have be appending a new extension to the
extension list.

I'm not sure why this never manifested in my 6 minutes of testing gif
from the original PR, mind you. I checked out that commit and local and
everything worked the same as I remember. Perhaps a change or version
bump in the interim modified the behaviour.

That aside, on current master it's MASSIVELY obvious something is
fucked.

Spamming 1000 characters of
```
# Slow
## Slower
### Slowest
```
then saving it, then puting **another** 1000 characters of the same
thing for 2000 previewed characters total gave the following performance
metrics:

![image](https://user-images.githubusercontent.com/24975989/221262868-c98d9b1a-e9d2-4a41-89e1-5ca6fb244cc6.png)

The marked.js parse time was about 150-180ms for the saved text and
220+ms for the input box text.

Moreso, if you leave the paper open for for a few minutes...

![image](https://user-images.githubusercontent.com/24975989/221263091-ae021caf-912d-4424-a33f-a91591ddf47e.png)

Yeah. That's 500+ms per parse (with 2 per render - one from the paper's
saved text and one from the input box) and the game will basically
crash.

Similar results using this template provided to me for testing purposes:
```
<center><b>Department Psychological Evaluation Survey</b></center>
<center><i>Test Log 230126-01</i></center>

<b>Question 1</b>
What department do you work in?
[_____________________________________]

<b>Question 2</b>
Which department are you least likely to recommend to a friend that they should work in?
<i>Please only choose one</i>
* Command [__]
* Security [__]
* Service [__]
* Cargo [__]
* Medical [__]
* Science [__]
* Engineering [__]

<b>Question 3</b>
Which department are you most likely to recommend to a friend that they should work in?
<i>Please only choose one</i>
* Command [__]
* Security [__]
* Service [__]
* Cargo [__]
* Medical [__]
* Science [__]
* Engineering [__]

<b>Question 4</b>
What is your favorite animal?
[_____________________________________]

---
Please sign here to confirm you are completing this survey voluntarily:
[_____________________________________]
<i>All Nanotrasen brand pens have an automatic signature system. Please write % s to automatically sign it.</i>
```

On first input + save, render time is normal but you can see it clearly
rising each second...

![image](https://user-images.githubusercontent.com/24975989/221263763-a0974f94-a95d-472a-881a-99ebf435239c.png)

![image](https://user-images.githubusercontent.com/24975989/221263878-5ed49878-aac2-49d1-9658-a7ff019e0947.png)

You get the idea.

Fixing the problem by using the proper markdown method for accomplishing
the same thing:

Markdown # Slow # Slower # Slowest test:

![image](https://user-images.githubusercontent.com/24975989/221264079-c58ec945-c130-4d3a-9dd7-b271b79321a3.png)

HoPaperwork Form

![image](https://user-images.githubusercontent.com/24975989/221264012-da121a5f-e4a8-4514-ab07-fb314ba50454.png)

And these numbers don't increase over time. So I think I nailed an
interim fix.

Ultimately, marked.js in tgui/BYOND just seems slow as molasses. But it
can at least be **usable** for now and this should majorly mitigate or
even eliminate problems players have been having with paper (depending
on how loaded it is with HTML and markdown tags).

All the features we had before seem to still work after I feex. So
hopefully good for now?

![image](https://user-images.githubusercontent.com/24975989/221267657-03fb0536-63a5-43a2-8f2b-b09bf7686d75.png)

## Beeg edit
I decided I'd go a step further and implement some basic caching logic.

![image](https://user-images.githubusercontent.com/24975989/221320748-54d60715-f8b8-48f7-9690-34467b526df2.png)

This means that you don't pay a parsing cost from already saved text
while writing new text, allowing complex forms to be created in multiple
saved or one big copy-paste.

This means that reading paper and filling in input boxes in paper is
free after the first parse, using the cache afterwards.

This is a workaround for the fact a parse can take longer than we'd like
for complicated paper forms.

## Why It's Good For The Game

Paper good.
## Changelog
🆑
fix: Papercode has been significantly improved and trivially filled
paper forms should no longer lag or crash players' game clients.
/🆑
2023-03-04 11:44:00 -05:00
..