{"action":"created","comment":{"url":"https://api.github.com/repos/realm/realm-java/pulls/comments/67504361","id":67504361,"diff_hunk":"@@ -784,6 +785,82 @@ private Scanner getFullStringScanner(InputStream in) {\n     }\n \n     /**\n+     * Bulk insert of a list of RealmObjects, this is faster than {@link #copyToRealm(Iterable)} since it\n+     * doesn't return the inserted elements, and doesn't perform checks (threads, parameters etc.).\n+     * <p>\n+     * Please note, copying an object will copy all field values. Any unset field in the object and child objects will be\n+     * set to their default value if not provided.\n+     *\n+     * @param objects RealmObjects to insert.\n+     */\n+    public void insertToRealm(Collection<? extends RealmModel> objects) {\n+        Iterator<? extends RealmModel> iterator = objects.iterator();\n+        RealmModel object = null;\n+        Map<RealmModel, Long> cache = new IdentityHashMap<RealmModel, Long>(objects.size());\n+        if (iterator.hasNext()) {\n+            // access the first element to figure out the type\n+            object = iterator.next();\n+            configuration.getSchemaMediator().insertToRealm(this, object, cache);\n+        }\n+\n+        if (iterator.hasNext()) {\n+            configuration.getSchemaMediator().insertToRealm(this, object, iterator, cache);\n+        }\n+    }\n+\n+    /**\n+     * Insert of a RealmObject, this is faster than {@link #copyToRealm(RealmModel)} since it\n+     * doesn't return the inserted elements, and doesn't perform checks (threads, parameters etc.).\n+     * <p>\n+     * Please note, copying an object will copy all field values. Any unset field in the object and child objects will be\n+     * set to their default value if not provided.\n+     *\n+     * @param object RealmObjects to insert.\n+     */\n+    public void insertToRealm(RealmModel object) {\n+        Map<RealmModel, Long> cache = new IdentityHashMap<RealmModel, Long>();\n+        configuration.getSchemaMediator().insertToRealm(this, object, cache);","path":"realm/realm-library/src/main/java/io/realm/Realm.java","position":65,"original_position":65,"commit_id":"bd2af80f8aa5e8eaecb6d7a9e16e203f5becf901","original_commit_id":"bd2af80f8aa5e8eaecb6d7a9e16e203f5becf901","user":{"login":"nhachicha","id":1793238,"avatar_url":"https://avatars.githubusercontent.com/u/1793238?v=3","gravatar_id":"","url":"https://api.github.com/users/nhachicha","html_url":"https://github.com/nhachicha","followers_url":"https://api.github.com/users/nhachicha/followers","following_url":"https://api.github.com/users/nhachicha/following{/other_user}","gists_url":"https://api.github.com/users/nhachicha/gists{/gist_id}","starred_url":"https://api.github.com/users/nhachicha/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nhachicha/subscriptions","organizations_url":"https://api.github.com/users/nhachicha/orgs","repos_url":"https://api.github.com/users/nhachicha/repos","events_url":"https://api.github.com/users/nhachicha/events{/privacy}","received_events_url":"https://api.github.com/users/nhachicha/received_events","type":"User","site_admin":false},"body":"I intentionally disabled all checks for the sake of speed, if we start checking here we need to be consistent and perform checks in the generated code as well.  This is part of the tradeoff, if the user pass a `null` it's going to throw an NPE anyway later. Same argument for managed object.\r\n\r\nIf you want safety, use the pessimistic method `copyToRealm` that perform all kind of logical checks.","created_at":"2016-06-17T12:47:13Z","updated_at":"2016-06-17T12:47:13Z","html_url":"https://github.com/realm/realm-java/pull/2999#discussion_r67504361","pull_request_url":"https://api.github.com/repos/realm/realm-java/pulls/2999","_links":{"self":{"href":"https://api.github.com/repos/realm/realm-java/pulls/comments/67504361"},"html":{"href":"https://github.com/realm/realm-java/pull/2999#discussion_r67504361"},"pull_request":{"href":"https://api.github.com/repos/realm/realm-java/pulls/2999"}}},"pull_request":{"url":"https://api.github.com/repos/realm/realm-java/pulls/2999","id":73837435,"html_url":"https://github.com/realm/realm-java/pull/2999","diff_url":"https://github.com/realm/realm-java/pull/2999.diff","patch_url":"https://github.com/realm/realm-java/pull/2999.patch","issue_url":"https://api.github.com/repos/realm/realm-java/issues/2999","number":2999,"state":"open","locked":false,"title":"bulk inserts","user":{"login":"nhachicha","id":1793238,"avatar_url":"https://avatars.githubusercontent.com/u/1793238?v=3","gravatar_id":"","url":"https://api.github.com/users/nhachicha","html_url":"https://github.com/nhachicha","followers_url":"https://api.github.com/users/nhachicha/followers","following_url":"https://api.github.com/users/nhachicha/following{/other_user}","gists_url":"https://api.github.com/users/nhachicha/gists{/gist_id}","starred_url":"https://api.github.com/users/nhachicha/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nhachicha/subscriptions","organizations_url":"https://api.github.com/users/nhachicha/orgs","repos_url":"https://api.github.com/users/nhachicha/repos","events_url":"https://api.github.com/users/nhachicha/events{/privacy}","received_events_url":"https://api.github.com/users/nhachicha/received_events","type":"User","site_admin":false},"body":"Fixes https://github.com/realm/realm-java/issues/1684 \r\n\r\n# Benchmarks with 100_000 objects\r\n## Before (using `copyToRealm`)\r\n![benchmark_batchwrite](https://cloud.githubusercontent.com/assets/1793238/16064422/fdd88640-3298-11e6-9601-b9bdfbaabab2.png)\r\n\r\n\r\n## Using Bulk Inserts   \r\n![benchmark_batchwrite](https://cloud.githubusercontent.com/assets/1793238/16064440/2754c560-3299-11e6-8439-d64f6779eb44.png)\r\n\r\n# Cap \r\n\r\nThe new API accepts a list of `Collection` instead of a list of `Iterator` because we need to know the size of the collection, in order to optimise the `Map` that acts as a cache for the previously inserted objects.\r\nhere is an Android _traceview_ dump that stress the importance of initialising the `IdentityMap` to avoid the expensive `rehash` invocation for each `put` operation.\r\n\r\n## Without initialising the Map\r\n<img width=\"780\" src=\"https://cloud.githubusercontent.com/assets/1793238/16064630/cbf7d12e-329a-11e6-844f-1e95a2d9eeb5.png\">\r\nwe spend 35% of the overall time in the `put` operation \r\n<img width=\"693\" src=\"https://cloud.githubusercontent.com/assets/1793238/16064676/39c1bcf6-329b-11e6-915a-986eaeaaf0de.png\">\r\nthe `rehash` could be avoided if we initialise the Map with the appropriate size\r\n\r\n## Initialising the Map with the size of the Collection\r\n<img width=\"628\" alt=\"after_rehash_opts\" src=\"https://cloud.githubusercontent.com/assets/1793238/16064663/19aa16de-329b-11e6-9341-ac05cec7d0f1.png\">\r\n\r\nAlthough we reduced the overall time spent in the Map, we can't reach the performance of lowlevel Realm, because the lowlevel Realm doesn't need the `Map` to cache objects.\r\n","created_at":"2016-06-15T00:55:13Z","updated_at":"2016-06-17T12:47:13Z","closed_at":null,"merged_at":null,"merge_commit_sha":"a947a545f64e5f48cb50fe6cf340163fe60931b4","assignee":{"login":"nhachicha","id":1793238,"avatar_url":"https://avatars.githubusercontent.com/u/1793238?v=3","gravatar_id":"","url":"https://api.github.com/users/nhachicha","html_url":"https://github.com/nhachicha","followers_url":"https://api.github.com/users/nhachicha/followers","following_url":"https://api.github.com/users/nhachicha/following{/other_user}","gists_url":"https://api.github.com/users/nhachicha/gists{/gist_id}","starred_url":"https://api.github.com/users/nhachicha/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nhachicha/subscriptions","organizations_url":"https://api.github.com/users/nhachicha/orgs","repos_url":"https://api.github.com/users/nhachicha/repos","events_url":"https://api.github.com/users/nhachicha/events{/privacy}","received_events_url":"https://api.github.com/users/nhachicha/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/realm/realm-java/pulls/2999/commits","review_comments_url":"https://api.github.com/repos/realm/realm-java/pulls/2999/comments","review_comment_url":"https://api.github.com/repos/realm/realm-java/pulls/comments{/number}","comments_url":"https://api.github.com/repos/realm/realm-java/issues/2999/comments","statuses_url":"https://api.github.com/repos/realm/realm-java/statuses/bd2af80f8aa5e8eaecb6d7a9e16e203f5becf901","head":{"label":"realm:nh/bulk_insert","ref":"nh/bulk_insert","sha":"bd2af80f8aa5e8eaecb6d7a9e16e203f5becf901","user":{"login":"realm","id":7575099,"avatar_url":"https://avatars.githubusercontent.com/u/7575099?v=3","gravatar_id":"","url":"https://api.github.com/users/realm","html_url":"https://github.com/realm","followers_url":"https://api.github.com/users/realm/followers","following_url":"https://api.github.com/users/realm/following{/other_user}","gists_url":"https://api.github.com/users/realm/gists{/gist_id}","starred_url":"https://api.github.com/users/realm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/realm/subscriptions","organizations_url":"https://api.github.com/users/realm/orgs","repos_url":"https://api.github.com/users/realm/repos","events_url":"https://api.github.com/users/realm/events{/privacy}","received_events_url":"https://api.github.com/users/realm/received_events","type":"Organization","site_admin":false},"repo":{"id":4084908,"name":"realm-java","full_name":"realm/realm-java","owner":{"login":"realm","id":7575099,"avatar_url":"https://avatars.githubusercontent.com/u/7575099?v=3","gravatar_id":"","url":"https://api.github.com/users/realm","html_url":"https://github.com/realm","followers_url":"https://api.github.com/users/realm/followers","following_url":"https://api.github.com/users/realm/following{/other_user}","gists_url":"https://api.github.com/users/realm/gists{/gist_id}","starred_url":"https://api.github.com/users/realm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/realm/subscriptions","organizations_url":"https://api.github.com/users/realm/orgs","repos_url":"https://api.github.com/users/realm/repos","events_url":"https://api.github.com/users/realm/events{/privacy}","received_events_url":"https://api.github.com/users/realm/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/realm/realm-java","description":"Realm is a mobile database: a replacement for SQLite & ORMs","fork":false,"url":"https://api.github.com/repos/realm/realm-java","forks_url":"https://api.github.com/repos/realm/realm-java/forks","keys_url":"https://api.github.com/repos/realm/realm-java/keys{/key_id}","collaborators_url":"https://api.github.com/repos/realm/realm-java/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/realm/realm-java/teams","hooks_url":"https://api.github.com/repos/realm/realm-java/hooks","issue_events_url":"https://api.github.com/repos/realm/realm-java/issues/events{/number}","events_url":"https://api.github.com/repos/realm/realm-java/events","assignees_url":"https://api.github.com/repos/realm/realm-java/assignees{/user}","branches_url":"https://api.github.com/repos/realm/realm-java/branches{/branch}","tags_url":"https://api.github.com/repos/realm/realm-java/tags","blobs_url":"https://api.github.com/repos/realm/realm-java/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/realm/realm-java/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/realm/realm-java/git/refs{/sha}","trees_url":"https://api.github.com/repos/realm/realm-java/git/trees{/sha}","statuses_url":"https://api.github.com/repos/realm/realm-java/statuses/{sha}","languages_url":"https://api.github.com/repos/realm/realm-java/languages","stargazers_url":"https://api.github.com/repos/realm/realm-java/stargazers","contributors_url":"https://api.github.com/repos/realm/realm-java/contributors","subscribers_url":"https://api.github.com/repos/realm/realm-java/subscribers","subscription_url":"https://api.github.com/repos/realm/realm-java/subscription","commits_url":"https://api.github.com/repos/realm/realm-java/commits{/sha}","git_commits_url":"https://api.github.com/repos/realm/realm-java/git/commits{/sha}","comments_url":"https://api.github.com/repos/realm/realm-java/comments{/number}","issue_comment_url":"https://api.github.com/repos/realm/realm-java/issues/comments{/number}","contents_url":"https://api.github.com/repos/realm/realm-java/contents/{+path}","compare_url":"https://api.github.com/repos/realm/realm-java/compare/{base}...{head}","merges_url":"https://api.github.com/repos/realm/realm-java/merges","archive_url":"https://api.github.com/repos/realm/realm-java/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/realm/realm-java/downloads","issues_url":"https://api.github.com/repos/realm/realm-java/issues{/number}","pulls_url":"https://api.github.com/repos/realm/realm-java/pulls{/number}","milestones_url":"https://api.github.com/repos/realm/realm-java/milestones{/number}","notifications_url":"https://api.github.com/repos/realm/realm-java/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/realm/realm-java/labels{/name}","releases_url":"https://api.github.com/repos/realm/realm-java/releases{/id}","deployments_url":"https://api.github.com/repos/realm/realm-java/deployments","created_at":"2012-04-20T09:18:42Z","updated_at":"2016-06-17T09:35:55Z","pushed_at":"2016-06-17T09:26:04Z","git_url":"git://github.com/realm/realm-java.git","ssh_url":"git@github.com:realm/realm-java.git","clone_url":"https://github.com/realm/realm-java.git","svn_url":"https://github.com/realm/realm-java","homepage":"http://realm.io","size":62485,"stargazers_count":5263,"watchers_count":5263,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":706,"mirror_url":null,"open_issues_count":242,"forks":706,"open_issues":242,"watchers":5263,"default_branch":"master"}},"base":{"label":"realm:master","ref":"master","sha":"a248ae172bb768a106b592a9cab64ca405e53afe","user":{"login":"realm","id":7575099,"avatar_url":"https://avatars.githubusercontent.com/u/7575099?v=3","gravatar_id":"","url":"https://api.github.com/users/realm","html_url":"https://github.com/realm","followers_url":"https://api.github.com/users/realm/followers","following_url":"https://api.github.com/users/realm/following{/other_user}","gists_url":"https://api.github.com/users/realm/gists{/gist_id}","starred_url":"https://api.github.com/users/realm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/realm/subscriptions","organizations_url":"https://api.github.com/users/realm/orgs","repos_url":"https://api.github.com/users/realm/repos","events_url":"https://api.github.com/users/realm/events{/privacy}","received_events_url":"https://api.github.com/users/realm/received_events","type":"Organization","site_admin":false},"repo":{"id":4084908,"name":"realm-java","full_name":"realm/realm-java","owner":{"login":"realm","id":7575099,"avatar_url":"https://avatars.githubusercontent.com/u/7575099?v=3","gravatar_id":"","url":"https://api.github.com/users/realm","html_url":"https://github.com/realm","followers_url":"https://api.github.com/users/realm/followers","following_url":"https://api.github.com/users/realm/following{/other_user}","gists_url":"https://api.github.com/users/realm/gists{/gist_id}","starred_url":"https://api.github.com/users/realm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/realm/subscriptions","organizations_url":"https://api.github.com/users/realm/orgs","repos_url":"https://api.github.com/users/realm/repos","events_url":"https://api.github.com/users/realm/events{/privacy}","received_events_url":"https://api.github.com/users/realm/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/realm/realm-java","description":"Realm is a mobile database: a replacement for SQLite & ORMs","fork":false,"url":"https://api.github.com/repos/realm/realm-java","forks_url":"https://api.github.com/repos/realm/realm-java/forks","keys_url":"https://api.github.com/repos/realm/realm-java/keys{/key_id}","collaborators_url":"https://api.github.com/repos/realm/realm-java/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/realm/realm-java/teams","hooks_url":"https://api.github.com/repos/realm/realm-java/hooks","issue_events_url":"https://api.github.com/repos/realm/realm-java/issues/events{/number}","events_url":"https://api.github.com/repos/realm/realm-java/events","assignees_url":"https://api.github.com/repos/realm/realm-java/assignees{/user}","branches_url":"https://api.github.com/repos/realm/realm-java/branches{/branch}","tags_url":"https://api.github.com/repos/realm/realm-java/tags","blobs_url":"https://api.github.com/repos/realm/realm-java/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/realm/realm-java/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/realm/realm-java/git/refs{/sha}","trees_url":"https://api.github.com/repos/realm/realm-java/git/trees{/sha}","statuses_url":"https://api.github.com/repos/realm/realm-java/statuses/{sha}","languages_url":"https://api.github.com/repos/realm/realm-java/languages","stargazers_url":"https://api.github.com/repos/realm/realm-java/stargazers","contributors_url":"https://api.github.com/repos/realm/realm-java/contributors","subscribers_url":"https://api.github.com/repos/realm/realm-java/subscribers","subscription_url":"https://api.github.com/repos/realm/realm-java/subscription","commits_url":"https://api.github.com/repos/realm/realm-java/commits{/sha}","git_commits_url":"https://api.github.com/repos/realm/realm-java/git/commits{/sha}","comments_url":"https://api.github.com/repos/realm/realm-java/comments{/number}","issue_comment_url":"https://api.github.com/repos/realm/realm-java/issues/comments{/number}","contents_url":"https://api.github.com/repos/realm/realm-java/contents/{+path}","compare_url":"https://api.github.com/repos/realm/realm-java/compare/{base}...{head}","merges_url":"https://api.github.com/repos/realm/realm-java/merges","archive_url":"https://api.github.com/repos/realm/realm-java/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/realm/realm-java/downloads","issues_url":"https://api.github.com/repos/realm/realm-java/issues{/number}","pulls_url":"https://api.github.com/repos/realm/realm-java/pulls{/number}","milestones_url":"https://api.github.com/repos/realm/realm-java/milestones{/number}","notifications_url":"https://api.github.com/repos/realm/realm-java/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/realm/realm-java/labels{/name}","releases_url":"https://api.github.com/repos/realm/realm-java/releases{/id}","deployments_url":"https://api.github.com/repos/realm/realm-java/deployments","created_at":"2012-04-20T09:18:42Z","updated_at":"2016-06-17T09:35:55Z","pushed_at":"2016-06-17T09:26:04Z","git_url":"git://github.com/realm/realm-java.git","ssh_url":"git@github.com:realm/realm-java.git","clone_url":"https://github.com/realm/realm-java.git","svn_url":"https://github.com/realm/realm-java","homepage":"http://realm.io","size":62485,"stargazers_count":5263,"watchers_count":5263,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":706,"mirror_url":null,"open_issues_count":242,"forks":706,"open_issues":242,"watchers":5263,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/realm/realm-java/pulls/2999"},"html":{"href":"https://github.com/realm/realm-java/pull/2999"},"issue":{"href":"https://api.github.com/repos/realm/realm-java/issues/2999"},"comments":{"href":"https://api.github.com/repos/realm/realm-java/issues/2999/comments"},"review_comments":{"href":"https://api.github.com/repos/realm/realm-java/pulls/2999/comments"},"review_comment":{"href":"https://api.github.com/repos/realm/realm-java/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/realm/realm-java/pulls/2999/commits"},"statuses":{"href":"https://api.github.com/repos/realm/realm-java/statuses/bd2af80f8aa5e8eaecb6d7a9e16e203f5becf901"}}}}
{"actor":{"display_login":"nhachicha"}}