{"action":"created","issue":{"url":"https://api.github.com/repos/bonecode/TBCEditor/issues/272","labels_url":"https://api.github.com/repos/bonecode/TBCEditor/issues/272/labels{/name}","comments_url":"https://api.github.com/repos/bonecode/TBCEditor/issues/272/comments","events_url":"https://api.github.com/repos/bonecode/TBCEditor/issues/272/events","html_url":"https://github.com/bonecode/TBCEditor/issues/272","id":123685281,"number":272,"title":"Performance issues in TBCEditorHighlighter.GetTokenKind","user":{"login":"EricGrange","id":2197210,"avatar_url":"https://avatars.githubusercontent.com/u/2197210?v=3","gravatar_id":"","url":"https://api.github.com/users/EricGrange","html_url":"https://github.com/EricGrange","followers_url":"https://api.github.com/users/EricGrange/followers","following_url":"https://api.github.com/users/EricGrange/following{/other_user}","gists_url":"https://api.github.com/users/EricGrange/gists{/gist_id}","starred_url":"https://api.github.com/users/EricGrange/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/EricGrange/subscriptions","organizations_url":"https://api.github.com/users/EricGrange/orgs","repos_url":"https://api.github.com/users/EricGrange/repos","events_url":"https://api.github.com/users/EricGrange/events{/privacy}","received_events_url":"https://api.github.com/users/EricGrange/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/bonecode/TBCEditor/labels/optimization","name":"optimization","color":"ededed"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-12-23T16:17:39Z","updated_at":"2015-12-24T07:12:36Z","closed_at":"2015-12-24T07:12:36Z","body":"When working on a largish file (like BCEditor.Editor.Base), I am seeing rather high CPU usage that can be traced back to GetTokenKindTBCEditorHighlighter.GetTokenKind\r\n\r\n* the GetToken call allocates a new String each time per token\r\n* \"if FCurrentRange.KeyList[i].KeyList[j]=LToken then\" performs a double indirection and a string copy each time, in addition to a string comparison\r\n\r\nThe double indirection can be solved and the code clarified by using a variable for \"FCurrentRange.KeyList[i]\".\r\n\r\nThe inner loop may be simplified by setting the inner KeyList's Sorted property to true and using IndexOf (which will then perform a binary search).\r\n\r\nThe GetToken allocation can be alleviated in several ways, most efficient would be to return a record with a PChar & length, but then TStringList can no longer be used directly and has to be replaced with something else.\r\nA less effective but simpler solution would be to use a GetToken procedure rather than a function (as in \"procedure GetToken(var result : String)\") and adjusting the passed string's length rather than reallocating anew. The LToken variable would have to be replaced by a Field. Since tokens are typically of similar short length, this should effectively simplify most of the re-allocations.\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/bonecode/TBCEditor/issues/comments/167059308","html_url":"https://github.com/bonecode/TBCEditor/issues/272#issuecomment-167059308","issue_url":"https://api.github.com/repos/bonecode/TBCEditor/issues/272","id":167059308,"user":{"login":"bonecode","id":11475177,"avatar_url":"https://avatars.githubusercontent.com/u/11475177?v=3","gravatar_id":"","url":"https://api.github.com/users/bonecode","html_url":"https://github.com/bonecode","followers_url":"https://api.github.com/users/bonecode/followers","following_url":"https://api.github.com/users/bonecode/following{/other_user}","gists_url":"https://api.github.com/users/bonecode/gists{/gist_id}","starred_url":"https://api.github.com/users/bonecode/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bonecode/subscriptions","organizations_url":"https://api.github.com/users/bonecode/orgs","repos_url":"https://api.github.com/users/bonecode/repos","events_url":"https://api.github.com/users/bonecode/events{/privacy}","received_events_url":"https://api.github.com/users/bonecode/received_events","type":"User","site_admin":false},"created_at":"2015-12-24T07:12:36Z","updated_at":"2015-12-24T07:12:36Z","body":"Fixed."}}