Skip to content

Commit

Permalink
Cache models
Browse files Browse the repository at this point in the history
This improves startup performance significantly, in my case reducing it
from 15 seconds to 2. However…

This is naïve in several ways, and has various serious failure modes and
as such should not be used as-is in a release version. It does however
lend itself to a much faster development cycle.

This does not take any changes to model-related files into account:

 - If models.txt is extended, it will likely crash
 - If models are reordered, the models displayed in-game will be wrong
   in potentially hilarious ways.
 - If model file references are changed, the changes will not be taken into
   account and the files used at cache generation time will be used.
 - If model files are modified, the changes will not be taken into
   account.

All of these issues can be remedied by removing the model.cache file
generated in the user data directory when any model files are altered.
As a hack for development purposes, the models.cache file could be
created and set to be neither readable nor writable by the user running
the game in order to prevent it from being used.

In addition, the file format is dumb and an incorrect models.cache file
will likely crash the game. It may also introduce security
vulnerabilities, so models.cache should never be loaded from an
untrusted source.

To improve on this, it would probably make sense to use a hash-based
data structure to map the contents of an input model file to its polygon
representation consistently. This would allow handling changes to the
models transparently while still providing a significant performance
boost. Adding compression may also be worthwhile, as the cache file ends
up being around 44MB with the data from the current release.
  • Loading branch information
lheckemann committed Oct 18, 2017
1 parent 662bed6 commit cc8c83b
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -681,14 +681,27 @@ void generatePolyModels(int start, int end)
polyquad_t* quad1, *quad2;
Uint32 numquads;
list_t quads;
FILE *model_cache;
bool generateAll = start == 0 && end == nummodels;

quads.first = NULL;
quads.last = NULL;

printlog("generating poly models...\n");
if ( start == 0 && end == nummodels )
if ( generateAll )
{
polymodels = (polymodel_t*) malloc(sizeof(polymodel_t) * nummodels);
model_cache = openUserFile("models.cache", "rb");
if (model_cache) {
for (size_t model_index = 0; model_index < nummodels; model_index++) {
polymodel_t *cur = &polymodels[model_index];
fread(&cur->numfaces, sizeof(cur->numfaces), 1, model_cache);
cur->faces = (polytriangle_t *) calloc(sizeof(polytriangle_t), cur->numfaces);
fread(polymodels[model_index].faces, sizeof(polytriangle_t), cur->numfaces, model_cache);
}
fclose(model_cache);
return generateVBOs(start, end);
}
}

for ( c = start; c < end; ++c )
Expand Down Expand Up @@ -1617,6 +1630,14 @@ void generatePolyModels(int start, int end)
// free up quads for the next model
list_FreeAll(&quads);
}
if (generateAll && (model_cache = openUserFile("models.cache", "wb"))) {
for (size_t model_index = 0; model_index < nummodels; model_index++) {
polymodel_t *cur = &polymodels[model_index];
fwrite(&cur->numfaces, sizeof(cur->numfaces), 1, model_cache);
fwrite(cur->faces, sizeof(polytriangle_t), cur->numfaces, model_cache);
}
fclose(model_cache);
}

// now store models into VBOs
if ( !disablevbos )
Expand Down

0 comments on commit cc8c83b

Please sign in to comment.