Major Forge performance increase


I wrongly assumed that The Forge was “just slow”. I have spent a few days fine tuning the way requests are done and have improved asset loading by over 15x! This means the average request used to take about a half second to resolve. The majority now resolve in under 40 milliseconds.

The data

Here are samples of requests taken from before and then after my Forge optimization updates. The size is the size of the asset and the time is the total time of the request and download time. Each row is a separate request sent for various Terminal assets.

version 2.5.0 (average 528ms)

version 2.5.2 (average 32ms)

How is this possible? I started by looking at the network tab I noticed many requests were actually just redirects to a similar URL. To dig a bit deeper I attempted making requests directly to the URL that was being redirected to. I was able to get the full asset with no issue. So, why even go make requests to the first URL? I don’t have a great answer for that but after talking with a Forge dev. They recommend to avoid the common approach of a relative path. They recommend instead to use an assets path which can benefit from caching. Skipping that first redirect request you generally save about a quarter of second. Sometimes much more, in rare cases the first request would stall out leaving Terminal as an unstyled skeleton for seconds.

initial 215ms request is just a redirect. The actual assets request takes 2.52s

If you are on The Forge your assets which were using the redirect URL are now automatically using the significantly faster direct URL.

Let’s make it even faster

Next I looked at the headers for the request and noticed that The Forge uses CloudFlare. I use CloudFlare for other projects and am familiar that they will somewhat aggressively cache things for you (which is generally a good thing). I was able to confirm this with the presence of two relevant headers Cf-Cache-Status and Age.

Cache-Status is usually in one of a few states:

  • MISS
  • HIT
  • REVALIDATED

These are referring to if an asset was present in the edge network cache. Data in these networks usually only last a few hours and ends up having a significant impact on speeds.

MISS

For example, let’s say you request to get image.jpg. You haven’t requested this asset today and you get a MISS on cache. Meaning the source server will need to serve this request. While this happens CloudFlare places that image.jpg in its edge network near you. It also makes note of when it was stored for determining its Age. Getting a MISS will result in a slow request that sometimes take a second or more to resolve.

HIT

Then let’s say 5 minutes later you again request image.jpg. In some cases your browser is smart enough to know this asset likely hasn’t changed and will serve you the same asset. If not then it sends out the request. Here is where you get a HIT on the cache. This is the real power of edge networks. This request will resolve extremely quick. As you can see in the screenshot of my data I downloaded a large 1.5Mb video in 59ms. For web development purposes this might as well be instant speed.

REVALIDATED

Last case if the Age of the asset reaches, in the case of Forge, 500 seconds (or 8.3 minutes) old. Then the asset is considered stale. You can still use the cache but there will be a revalidation action performed to check if the asset needs to be renewed. Revalidation comes with a 50ms penalty, meaning requests will resolve ~90ms usually. This is still very fast.

Optimization

Since all that is needed to vastly improve speeds is to preemptively perform the MISS request. I have now implemented for The Forge, prefetching. This comes in two modes.

Forge Default

By default (on The Forge) I am requesting all assets for enabled Terminals for all clients when they first load a scene. This ensures that all requests in the next hour for this scene can be expected to be 15x faster. When a new scene is switched to it will again prefetch all enabled Terminal assets. You can open up the Developer Console and watch the logs for how many assets were prefetched.

Cache Warming Setting

But why keep these wins only for Forge. I have now added optional periodic prefetches for enabled Terminals in the current scene. If enabled this happens every 6 minutes. This is off by default since this should only be used if you know your server is behind a cache network!

You can find this setting in your game settings under “Keep Cache Warm”. Please keep in mind if you have several Terminals enabled in a scene and several players a lot of prefetch requests will be sent out.

Enabling this setting will also give you the previous Forge Default behavior where changing to a scene prefetches all enabled Terminals.

Forge GMs can also enable this setting for extra prefetches in addition to the previously mentioned Forge Default ones.

To check if your server is behind a cache network you can look for the Cf-Cache-Status response headers in your browser’s developer console.

Enjoy your fast Terminal speeds

Get Foundry Terminal

Buy Now$4.99 USD or more

Leave a comment

Log in with itch.io to leave a comment.