{"action":"created","issue":{"url":"https://api.github.com/repos/realm/realm-cocoa/issues/2510","labels_url":"https://api.github.com/repos/realm/realm-cocoa/issues/2510/labels{/name}","comments_url":"https://api.github.com/repos/realm/realm-cocoa/issues/2510/comments","events_url":"https://api.github.com/repos/realm/realm-cocoa/issues/2510/events","html_url":"https://github.com/realm/realm-cocoa/issues/2510","id":105758052,"number":2510,"title":"Slow open RLMRealm on Realm 0.93.2 and greater (0.95.0 included)","user":{"login":"javialgaba","id":4939637,"avatar_url":"https://avatars.githubusercontent.com/u/4939637?v=3","gravatar_id":"","url":"https://api.github.com/users/javialgaba","html_url":"https://github.com/javialgaba","followers_url":"https://api.github.com/users/javialgaba/followers","following_url":"https://api.github.com/users/javialgaba/following{/other_user}","gists_url":"https://api.github.com/users/javialgaba/gists{/gist_id}","starred_url":"https://api.github.com/users/javialgaba/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/javialgaba/subscriptions","organizations_url":"https://api.github.com/users/javialgaba/orgs","repos_url":"https://api.github.com/users/javialgaba/repos","events_url":"https://api.github.com/users/javialgaba/events{/privacy}","received_events_url":"https://api.github.com/users/javialgaba/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":{"login":"tgoyne","id":408944,"avatar_url":"https://avatars.githubusercontent.com/u/408944?v=3","gravatar_id":"","url":"https://api.github.com/users/tgoyne","html_url":"https://github.com/tgoyne","followers_url":"https://api.github.com/users/tgoyne/followers","following_url":"https://api.github.com/users/tgoyne/following{/other_user}","gists_url":"https://api.github.com/users/tgoyne/gists{/gist_id}","starred_url":"https://api.github.com/users/tgoyne/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tgoyne/subscriptions","organizations_url":"https://api.github.com/users/tgoyne/orgs","repos_url":"https://api.github.com/users/tgoyne/repos","events_url":"https://api.github.com/users/tgoyne/events{/privacy}","received_events_url":"https://api.github.com/users/tgoyne/received_events","type":"User","site_admin":false},"milestone":null,"comments":3,"created_at":"2015-09-10T08:25:01Z","updated_at":"2015-09-10T17:11:54Z","closed_at":null,"body":"I have found a regression in **realm-cocoa**. Version *0.93.1* and before load the first `RLMRealm` instance of a .realm file much much faster than newer versions. I have attached a sample code where you can compare results.\r\n\r\nI am developing an app with lots of different Realms, and this bug has forced me to downgrade to Realm 0.93.1. Before opening this issue, I have tested different realm-cocoa versions, and I can confirm that this problem start to arise at version **0.93.2**.\r\n\r\nAnyway, thank you very much for your amazing work. Realm is really great :)\r\n\r\n```\r\n- (void)viewDidLoad {\r\n    [super viewDidLoad];\r\n    \r\n    // Load instance after a couple of seconds to get more time accuracy\r\n    dispatch_queue_t queue = dispatch_get_main_queue();\r\n    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC), queue, ^{\r\n        NSTimeInterval startTime = CACurrentMediaTime();\r\n        [RLMRealm defaultRealm]; // Load a Realm instance\r\n        NSLog(@\"realm loaded (s): %f\", CACurrentMediaTime()-startTime);\r\n    });\r\n}\r\n```\r\n\r\nHere is a simple benchmark I have done comparing *0.93.1* and *0.95.0* (time in seconds)\r\n\r\n| Realm 0.93.1 | Realm 0.95.0  |\r\n| ------------------ | ------------------ |\r\n| 0,025706       | 0,598671        |\r\n| 0,015229       | 0,558317        |\r\n| 0,015650       | 0,586977        |\r\n| 0,016144       | 0,788107        |\r\n| 0,015895       | 0,545924        |\r\n| 0,016177       | 0,562112        |\r\n| 0,016205       | 0,565462        |\r\n| 0,016735       | 0,514518        |\r\n| 0,017433       | 0,578577        |\r\n| 0,015473       | 0,564286        |\r\n\r\n**Average**\r\n\r\n| Realm 0.93.1 | Realm 0.95.0 |\r\n| ------------------ | ------------------ |\r\n| 0,0170647     | 0,5862951      |\r\n\r\nWe are seeing here that v0.93.1 is **3435,72% faster** on average loading the first realm instance"},"comment":{"url":"https://api.github.com/repos/realm/realm-cocoa/issues/comments/139313374","html_url":"https://github.com/realm/realm-cocoa/issues/2510#issuecomment-139313374","issue_url":"https://api.github.com/repos/realm/realm-cocoa/issues/2510","id":139313374,"user":{"login":"tgoyne","id":408944,"avatar_url":"https://avatars.githubusercontent.com/u/408944?v=3","gravatar_id":"","url":"https://api.github.com/users/tgoyne","html_url":"https://github.com/tgoyne","followers_url":"https://api.github.com/users/tgoyne/followers","following_url":"https://api.github.com/users/tgoyne/following{/other_user}","gists_url":"https://api.github.com/users/tgoyne/gists{/gist_id}","starred_url":"https://api.github.com/users/tgoyne/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tgoyne/subscriptions","organizations_url":"https://api.github.com/users/tgoyne/orgs","repos_url":"https://api.github.com/users/tgoyne/repos","events_url":"https://api.github.com/users/tgoyne/events{/privacy}","received_events_url":"https://api.github.com/users/tgoyne/received_events","type":"User","site_admin":false},"created_at":"2015-09-10T17:11:54Z","updated_at":"2015-09-10T17:11:54Z","body":"To clarify, this is when the Realm file doesn't already exist, as opposed to the first time opening an existing Realm file? I wrote the following simple performance test for this and ran it on both 0.93.1 and 0.93.2:\r\n\r\n```objc\r\n- (void)testRealmFileCreation {\r\n    __block int measurement = 0;\r\n    const int iterations = 50;\r\n    NSString *path = [[[RLMRealm defaultRealmPath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@\"dir\"];\r\n    [NSFileManager.defaultManager removeItemAtPath:path error:nil];\r\n    [NSFileManager.defaultManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];\r\n    [self measureBlock:^{\r\n        for (int i = 0; i < iterations; ++i) {\r\n            @autoreleasepool {\r\n                [RLMRealm realmWithPath:[path stringByAppendingPathComponent:@(measurement * iterations + i).stringValue]];\r\n            }\r\n        }\r\n        ++measurement;\r\n    }];\r\n}\r\n```\r\n\r\nThis gave an average of 2.819 second with 0.93.1 and 7.358 seconds with 0.93.2, which is an unpleasant hit (creating the file is pretty much the worst possible case for the change jpsim mentioned), but a significantly smaller difference than what you're seeing.\r\n\r\nOne possibility is that since `F_FULLFSYNC` waits for all of the drive's buffers to be flushed, you're seeing significant delays from it waiting for writes from outside your app to complete, which wouldn't show in in a small artificial test. If that's the case, creating a second Realm file at a different path will take significantly less time than creating the first one did."}}