mirror of
https://github.com/atom/atom.git
synced 2026-04-28 03:01:47 -04:00
Cache compiled CoffeeScript on the Objective-C side.
This is a temporary fix, it just stores compiled scripts in memory. It doesn't speed up the app start since the scripts need to be compiled once. Here are some numbers. #file load pre: 1.5 seconds post: 0.25 seconds #spec load pre: 5.4 seconds post: 0.36 seconds
This commit is contained in:
@@ -11,4 +11,7 @@
|
||||
- (void)performActionForMenuItem:(AtomMenuItem *)item;
|
||||
- (void)resetMainMenu;
|
||||
|
||||
- (NSString *)getCachedScript:(NSString *)filePath;
|
||||
- (void)setCachedScript:(NSString *)filePath contents:(NSString *)contents;
|
||||
|
||||
@end
|
||||
|
||||
@@ -9,6 +9,11 @@
|
||||
#define ATOM_USER_PATH ([[NSString stringWithString:@"~/.atom/"] stringByStandardizingPath])
|
||||
#define ATOM_STORAGE_PATH ([ATOM_USER_PATH stringByAppendingPathComponent:@".app-storage"])
|
||||
|
||||
@interface AtomApp () {
|
||||
NSMutableDictionary *_coffeeCache;
|
||||
}
|
||||
@end
|
||||
|
||||
@implementation AtomApp
|
||||
|
||||
@synthesize controllers = _controllers;
|
||||
@@ -75,6 +80,8 @@
|
||||
}
|
||||
|
||||
- (void)applicationDidFinishLaunching:(NSNotification *)notification {
|
||||
_coffeeCache = [[NSMutableDictionary alloc] init];
|
||||
|
||||
if ([[[NSProcessInfo processInfo] environment] objectForKey:@"AUTO-TEST"]) {
|
||||
[self runSpecs:self];
|
||||
}
|
||||
@@ -105,4 +112,33 @@
|
||||
[self resetMenu:self.mainMenu];
|
||||
}
|
||||
|
||||
- (NSString *)getCachedScript:(NSString *)filePath {
|
||||
NSDictionary *cachedObject = [_coffeeCache objectForKey:filePath];
|
||||
if (!cachedObject) return nil;
|
||||
|
||||
NSFileManager *fm = [NSFileManager defaultManager];
|
||||
NSError *error = nil;
|
||||
NSDictionary *attributes = [fm attributesOfItemAtPath:filePath error:&error];
|
||||
if (error) {
|
||||
NSLog(@"Error reading cached scripts: %@", [error localizedDescription]);
|
||||
return nil;
|
||||
}
|
||||
|
||||
NSDate *cachedAt = [cachedObject objectForKey:@"cachedAt"];
|
||||
NSDate *modifedAt = [attributes objectForKey:NSFileModificationDate];
|
||||
if (modifedAt && [modifedAt compare:cachedAt] == NSOrderedAscending) {
|
||||
return [cachedObject objectForKey:@"script"];
|
||||
}
|
||||
else {
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
|
||||
- (void)setCachedScript:(NSString *)filePath contents:(NSString *)contents {
|
||||
NSMutableDictionary *cachedObject = [NSMutableDictionary dictionary];
|
||||
[cachedObject setObject:[NSDate date] forKey:@"cachedAt"];
|
||||
[cachedObject setObject:contents forKey:@"script"];
|
||||
[_coffeeCache setObject:cachedObject forKey:filePath];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -58,8 +58,7 @@ exts =
|
||||
__jsc__.evalJSString_withScriptPath code, file
|
||||
__defines.pop()?.call()
|
||||
coffee: (file) ->
|
||||
{CoffeeScript} = require 'coffee-script'
|
||||
exts.js(file, CoffeeScript.compile(__read(file), filename: file))
|
||||
exts.js(file, __coffeeCache(file))
|
||||
|
||||
resolve = (file) ->
|
||||
if /!/.test file
|
||||
@@ -110,6 +109,14 @@ __expand = (path) ->
|
||||
__exists = (path) ->
|
||||
OSX.NSFileManager.defaultManager.fileExistsAtPath path
|
||||
|
||||
__coffeeCache = (filePath) ->
|
||||
js = OSX.NSApp.getCachedScript(filePath)
|
||||
if not js
|
||||
{CoffeeScript} = require 'coffee-script'
|
||||
js = CoffeeScript.compile(__read(filePath), filename: filePath)
|
||||
OSX.NSApp.setCachedScript_contents(filePath, js)
|
||||
js
|
||||
|
||||
__read = (path) ->
|
||||
try
|
||||
OSX.NSString.stringWithContentsOfFile(path).toString()
|
||||
|
||||
Reference in New Issue
Block a user