Cache result of -[Universe generateSystemData] -- very high hit rate, significant speed-up in set-up. Optimization hints in legacy_random.

git-svn-id: http://svn.berlios.de/svnroot/repos/oolite-linux/trunk@881 127b21dd-08f5-0310-b4b7-95ae10353056
This commit is contained in:
Jens Ayton 2007-04-01 14:07:10 +00:00
parent cd9d2260de
commit d701707e13
5 changed files with 82 additions and 57 deletions

View File

@ -49,7 +49,6 @@ NSMutableArray *ScanTokensFromString(NSString *values)
// Note: Shark suggests we're getting a lot of early exits, but testing showed a pretty steady 2% early exit rate.
if (EXPECT_NOT(values == nil)) return [NSArray array];
if (EXPECT_NOT(space_set == nil)) space_set = [[NSCharacterSet whitespaceAndNewlineCharacterSet] retain];
result = [NSMutableArray array];
@ -232,7 +231,7 @@ Random_Seed RandomSeedFromString(NSString *abcdefString)
else
{
OOLog(kOOLogStringRandomSeedConversion, @"***** ERROR cannot make Random_Seed from '%@': %@", abcdefString, error);
result = nil_seed();
result = kNilRandomSeed;
}
return result;

View File

@ -1282,7 +1282,7 @@ static Vector circleVertex[65]; // holds vector coordinates for a unit circle
- (void) updateExhaust2:(double) delta_t
{
#if ADDITIVE_BLENDING
#define OVERALL_ALPHA 0.3f
#define OVERALL_ALPHA 0.4f
#else
#define OVERALL_ALPHA 1.0f
#endif

View File

@ -5617,7 +5617,7 @@ BOOL maintainLinkedLists(Universe* uni)
return systems[i];
}
OOLog(kOOLogScriptNoSystemForName, @"SCRIPT ERROR could not find a system with the name '%@' in this galaxy", sysname);
return nil_seed();
return kNilRandomSeed;
}
- (NSDictionary *) shipyard
@ -5683,6 +5683,16 @@ BOOL maintainLinkedLists(Universe* uni)
- (NSDictionary *) generateSystemData:(Random_Seed) s_seed
{
static NSDictionary *cachedResult = nil;
static Random_Seed cachedSeed = {0};
// Cache hit ratio is over 95% during respawn, about 80% during initial set-up.
if (EXPECT(cachedResult != nil && equal_seeds(cachedSeed, s_seed))) return cachedResult;
[cachedResult autorelease]; // Stuff may have references to old value, so don't release right off the bat
cachedResult = nil;
cachedSeed = s_seed;
NSMutableDictionary* systemdata = [[NSMutableDictionary alloc] initWithCapacity:8];
int government = (s_seed.c / 8) & 7;
@ -5724,7 +5734,8 @@ BOOL maintainLinkedLists(Universe* uni)
if ([local_planetinfo_overrides objectForKey:override_key])
[systemdata addEntriesFromDictionary:(NSDictionary *)[local_planetinfo_overrides objectForKey:override_key]];
return [NSDictionary dictionaryWithDictionary:[systemdata autorelease]];
cachedResult = [systemdata copy];
return cachedResult;
}
- (NSDictionary *) currentSystemData

View File

@ -29,8 +29,14 @@ MA 02110-1301, USA.
#include <math.h>
#include "legacy_random.h"
const Random_Seed kNilRandomSeed = {0};
static struct random_seed rnd_seed;
// TODO: Why is this based on a static? Should change to MungeCheckSum(&checkSum, value);
static int checksum;
void clear_checksum()
{
@ -93,25 +99,6 @@ inline int ranrot_rand()
return m_high & 0x7FFFFFFF;
}
// a method used to determine interplanetary distances,
// if accurate, it has to scale distance down by a factor of 7.15:7.0
// to allow routes navigable in the original!
double distanceBetweenPlanetPositions ( int x1, int y1, int x2, int y2)
{
int dx = x1 - x2;
int dy = (y1 - y2)/2;
int dist = sqrt(dx*dx + dy*dy); // here's where the rounding errors come in!
return 0.4*dist;
}
double accurateDistanceBetweenPlanetPositions ( int x1, int y1, int x2, int y2)
{
double dx = x1 - x2;
double dy = (y1 - y2)/2;
double dist = sqrt(dx*dx + dy*dy); // here's where the rounding errors come in!
return 0.4*dist;
}
void seed_for_planet_description (Random_Seed s_seed)
{
rnd_seed.a = s_seed.c;
@ -194,17 +181,9 @@ void make_pseudo_random_seed (struct rand_seed_6uc *seed_ptr)
Random_Seed nil_seed()
{
Random_Seed result = { (unsigned char)0, (unsigned char)0, (unsigned char)0, (unsigned char)0, (unsigned char)0, (unsigned char)0};
return result;
return kNilRandomSeed;
}
int is_nil_seed(Random_Seed a_seed)
{
if (a_seed.a | a_seed.b | a_seed.c | a_seed.d | a_seed.e | a_seed.f)
return 0;
else
return -1;
}
void rotate_seed (struct rand_seed_6uc *seed_ptr)
{
@ -239,15 +218,3 @@ void rotate_seed (struct rand_seed_6uc *seed_ptr)
seed_ptr->e = x;
seed_ptr->f = y;
}
int rotate_byte_left (int x)
{
return ((x << 1) | (x >> 7)) & 255;
}
int equal_seeds (Random_Seed seed1, Random_Seed seed2)
{
return ((seed1.a == seed2.a)&&(seed1.b == seed2.b)&&(seed1.c == seed2.c)&&(seed1.d == seed2.d)&&(seed1.e == seed2.e)&&(seed1.f == seed2.f));
}

View File

@ -28,6 +28,8 @@ MA 02110-1301, USA.
#ifndef LEGACY_RANDOM_H
#define LEGACY_RANDOM_H
#import "OOFunctionAttributes.h"
struct rand_seed_6uc
{
@ -51,13 +53,17 @@ struct random_seed
typedef struct random_seed RNG_Seed;
extern const Random_Seed kNilRandomSeed;
// checksum stuff
void clear_checksum();
int munge_checksum(int value);
// cunning price rounding routine:
//
float cunningFee(float value);
float cunningFee(float value) CONST_FUNC;
// an implementation of RANROT
// pseudo random number generator
@ -65,8 +71,8 @@ float cunningFee(float value);
inline void ranrot_srand(unsigned int seed);
inline int ranrot_rand();
double distanceBetweenPlanetPositions ( int x1, int y1, int x2, int y2);
double accurateDistanceBetweenPlanetPositions ( int x1, int y1, int x2, int y2);
OOINLINE double distanceBetweenPlanetPositions(int x1, int y1, int x2, int y2) INLINE_CONST_FUNC;
OOINLINE double accurateDistanceBetweenPlanetPositions(int x1, int y1, int x2, int y2) INLINE_CONST_FUNC;
void seed_for_planet_description(Random_Seed s_seed);
void seed_RNG_only_for_planet_description(Random_Seed s_seed);
@ -80,12 +86,54 @@ int gen_rnd_number (void);
void make_pseudo_random_seed (struct rand_seed_6uc *seed_ptr);
Random_Seed nil_seed();
int is_nil_seed(Random_Seed a_seed);
Random_Seed nil_seed() DEPRECATED_FUNC;
OOINLINE int is_nil_seed(Random_Seed a_seed) INLINE_CONST_FUNC;
void rotate_seed (struct rand_seed_6uc *seed_ptr);
int rotate_byte_left (int x);
OOINLINE int rotate_byte_left (int x) INLINE_CONST_FUNC;
int equal_seeds (Random_Seed seed1, Random_Seed seed2);
OOINLINE int equal_seeds(Random_Seed seed1, Random_Seed seed2) INLINE_CONST_FUNC;
/*** Only inline definitions beyond this point ***/
OOINLINE int equal_seeds(Random_Seed seed1, Random_Seed seed2)
{
return ((seed1.a == seed2.a)&&(seed1.b == seed2.b)&&(seed1.c == seed2.c)&&(seed1.d == seed2.d)&&(seed1.e == seed2.e)&&(seed1.f == seed2.f));
}
OOINLINE int is_nil_seed(Random_Seed a_seed)
{
return equal_seeds(a_seed, kNilRandomSeed);
}
OOINLINE int rotate_byte_left(int x)
{
return ((x << 1) | (x >> 7)) & 255;
}
// a method used to determine interplanetary distances,
// if accurate, it has to scale distance down by a factor of 7.15:7.0
// to allow routes navigable in the original!
OOINLINE double distanceBetweenPlanetPositions ( int x1, int y1, int x2, int y2)
{
int dx = x1 - x2;
int dy = (y1 - y2)/2;
int dist = sqrtf(dx*dx + dy*dy); // here's where the rounding errors come in!
return 0.4 * dist;
}
OOINLINE double accurateDistanceBetweenPlanetPositions ( int x1, int y1, int x2, int y2)
{
double dx = x1 - x2;
double dy = (y1 - y2) / 2.0;
double dist = sqrt(dx*dx + dy*dy); // here's where the rounding errors come in!
return 0.4 * dist;
}
#endif