crude caching of directory contents

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@34471 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2012-01-09 20:49:01 +00:00
parent b143b3fabf
commit 01355eaf39
2 changed files with 46 additions and 15 deletions

View file

@ -1,3 +1,8 @@
2012-01-09 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSBundle.m: Crude/simple caching of directory contents to
test impact on theme loading ... needs reverting or improving.
2012-01-09 Richard Frith-Macdonald <rfm@gnu.org> 2012-01-09 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSConcretePointerFunctions.m: * Source/NSConcretePointerFunctions.m:

View file

@ -39,6 +39,7 @@
#import "Foundation/NSArray.h" #import "Foundation/NSArray.h"
#import "Foundation/NSDictionary.h" #import "Foundation/NSDictionary.h"
#import "Foundation/NSEnumerator.h" #import "Foundation/NSEnumerator.h"
#import "Foundation/NSNull.h"
#import "Foundation/NSProcessInfo.h" #import "Foundation/NSProcessInfo.h"
#import "Foundation/NSUserDefaults.h" #import "Foundation/NSUserDefaults.h"
#import "Foundation/NSNotification.h" #import "Foundation/NSNotification.h"
@ -74,6 +75,8 @@ manager()
return mgr; return mgr;
} }
static NSLock *pathCacheLock = nil;
static NSMutableDictionary *pathCache = nil;
@interface NSObject (PrivateFrameworks) @interface NSObject (PrivateFrameworks)
+ (NSString*) frameworkEnv; + (NSString*) frameworkEnv;
@ -299,17 +302,32 @@ GSPrivateExecutablePath()
return executablePath; return executablePath;
} }
static BOOL static NSArray *
bundle_directory_readable(NSString *path) bundle_directory_readable(NSString *path)
{ {
NSFileManager *mgr = manager(); id found;
BOOL directory;
if ([mgr fileExistsAtPath: path isDirectory: &directory] == NO [pathCacheLock lock];
|| !directory) found = [[[pathCache objectForKey: path] retain] autorelease];
return NO; [pathCacheLock unlock];
if (nil == found)
{
NSFileManager *mgr = manager();
return [mgr isReadableFileAtPath: path]; found = [mgr directoryContentsAtPath: path];
if (nil == found)
{
found = [NSNull null];
}
[pathCacheLock lock];
[pathCache setObject: found forKey: path];
[pathCacheLock unlock];
}
if ((id)[NSNull null] == found)
{
found = nil;
}
return (NSArray*)found;
} }
/* Get the object file that should be located in the bundle of the same name */ /* Get the object file that should be located in the bundle of the same name */
@ -861,6 +879,9 @@ _bundle_load_callback(Class theClass, struct objc_category *theCategory)
*/ */
manager(); manager();
pathCacheLock = [NSLock new];
pathCache = [NSMutableDictionary new];
/* Need to make this recursive since both mainBundle and /* Need to make this recursive since both mainBundle and
* initWithPath: want to lock the thread. * initWithPath: want to lock the thread.
*/ */
@ -1413,7 +1434,7 @@ IF_NO_GC(
} }
[load_lock unlock]; [load_lock unlock];
if (bundle_directory_readable(path) == NO) if (bundle_directory_readable(path) == nil)
{ {
NSDebugMLLog(@"NSBundle", @"Could not access path %@ for bundle", path); NSDebugMLLog(@"NSBundle", @"Could not access path %@ for bundle", path);
// if this is not the main bundle ... deallocate and return. // if this is not the main bundle ... deallocate and return.
@ -1781,6 +1802,7 @@ IF_NO_GC(
{ {
NSFileManager *mgr = manager(); NSFileManager *mgr = manager();
NSString *path; NSString *path;
NSString *file;
NSEnumerator *pathlist; NSEnumerator *pathlist;
if (name == nil) if (name == nil)
@ -1790,20 +1812,24 @@ IF_NO_GC(
if ([extension length] == 0) if ([extension length] == 0)
{ {
extension = nil; extension = nil;
file = name;
}
else
{
file = [name stringByAppendingPathExtension: extension];
} }
pathlist = [[self _bundleResourcePathsWithRootPath: rootPath pathlist = [[self _bundleResourcePathsWithRootPath: rootPath
subPath: subPath localization: nil] objectEnumerator]; subPath: subPath localization: nil] objectEnumerator];
while ((path = [pathlist nextObject]) != nil) while ((path = [pathlist nextObject]) != nil)
{ {
if (bundle_directory_readable(path)) id paths = bundle_directory_readable(path);
if ((id)[NSNull null] != paths
&& YES == [(NSArray*)paths containsObject: file])
{ {
path = [path stringByAppendingPathComponent: name]; path = [path stringByAppendingPathComponent: file];
if (extension != nil) if (YES == [mgr isReadableFileAtPath: path])
{
path = [path stringByAppendingPathExtension: extension];
}
if ([mgr isReadableFileAtPath: path])
{ {
return path; return path;
} }