{"action":"created","issue":{"url":"https://api.github.com/repos/google/flatbuffers/issues/3837","repository_url":"https://api.github.com/repos/google/flatbuffers","labels_url":"https://api.github.com/repos/google/flatbuffers/issues/3837/labels{/name}","comments_url":"https://api.github.com/repos/google/flatbuffers/issues/3837/comments","events_url":"https://api.github.com/repos/google/flatbuffers/issues/3837/events","html_url":"https://github.com/google/flatbuffers/issues/3837","id":146094919,"number":3837,"title":"Small performance optimization in C# code","user":{"login":"klapek","id":1790627,"avatar_url":"https://avatars.githubusercontent.com/u/1790627?v=3","gravatar_id":"","url":"https://api.github.com/users/klapek","html_url":"https://github.com/klapek","followers_url":"https://api.github.com/users/klapek/followers","following_url":"https://api.github.com/users/klapek/following{/other_user}","gists_url":"https://api.github.com/users/klapek/gists{/gist_id}","starred_url":"https://api.github.com/users/klapek/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/klapek/subscriptions","organizations_url":"https://api.github.com/users/klapek/orgs","repos_url":"https://api.github.com/users/klapek/repos","events_url":"https://api.github.com/users/klapek/events{/privacy}","received_events_url":"https://api.github.com/users/klapek/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2016-04-05T20:00:33Z","updated_at":"2016-04-08T17:33:20Z","closed_at":null,"body":"Hi guys,\r\n\r\nThere is a small performance optimization possible in c# code in \r\n  ```\r\npublic class ByteBuffer\r\n{\r\n...\r\n    private void AssertOffsetAndLength(int offset, int length){\r\n            if (offset < 0 ||\r\n                offset >= _buffer.Length ||\r\n                offset + length > _buffer.Length)\r\n                throw new ArgumentOutOfRangeException();\r\n    }\r\n...\r\n}\r\n```\r\n\r\nAs this check is run for each put and get even small changes gives performance boost.\r\nIn this case these 3 checks need to run each time, even when those lines:\r\n\r\n```\r\noffset >= _buffer.Length ||\r\noffset + length > _buffer.Length\r\n```\r\nAre essentially equivalent as they can be transformed to this\r\n  \r\n```\r\noffset >= _buffer.Length ||\r\noffset >= _buffer.Length - length +1\r\n```\r\nand  as length is always greater than 0 then second check is equally or less forgiving then the first one so the first one doesn't need to run at all.\r\n\r\nhence:\r\n```\r\n    private void AssertOffsetAndLength(int offset, int length){\r\n            if (offset < 0 ||\r\n                offset > _buffer.Length - length)\r\n                throw new ArgumentOutOfRangeException();\r\n    }\r\n```\r\n\r\nIs equivalent and faster. From my simple benchmark inserting 2 000 000 doubles / longs takes about 87% of original time, so speedup in inserting is about 13% with this simple change.\r\n\r\nI can make a pull request if you see it fit.\r\n\r\nThanks\r\n"},"comment":{"url":"https://api.github.com/repos/google/flatbuffers/issues/comments/207528728","html_url":"https://github.com/google/flatbuffers/issues/3837#issuecomment-207528728","issue_url":"https://api.github.com/repos/google/flatbuffers/issues/3837","id":207528728,"user":{"login":"gwvo","id":6145400,"avatar_url":"https://avatars.githubusercontent.com/u/6145400?v=3","gravatar_id":"","url":"https://api.github.com/users/gwvo","html_url":"https://github.com/gwvo","followers_url":"https://api.github.com/users/gwvo/followers","following_url":"https://api.github.com/users/gwvo/following{/other_user}","gists_url":"https://api.github.com/users/gwvo/gists{/gist_id}","starred_url":"https://api.github.com/users/gwvo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gwvo/subscriptions","organizations_url":"https://api.github.com/users/gwvo/orgs","repos_url":"https://api.github.com/users/gwvo/repos","events_url":"https://api.github.com/users/gwvo/events{/privacy}","received_events_url":"https://api.github.com/users/gwvo/received_events","type":"User","site_admin":false},"created_at":"2016-04-08T17:33:20Z","updated_at":"2016-04-08T17:33:20Z","body":"Ok. Yes, please make a PR. "}}