{"action":"created","issue":{"url":"https://api.github.com/repos/SubtitleEdit/subtitleedit/issues/978","labels_url":"https://api.github.com/repos/SubtitleEdit/subtitleedit/issues/978/labels{/name}","comments_url":"https://api.github.com/repos/SubtitleEdit/subtitleedit/issues/978/comments","events_url":"https://api.github.com/repos/SubtitleEdit/subtitleedit/issues/978/events","html_url":"https://github.com/SubtitleEdit/subtitleedit/pull/978","id":92911119,"number":978,"title":"\"Reduce more memory allocation\"","user":{"login":"ivandrofly","id":6579818,"avatar_url":"https://avatars.githubusercontent.com/u/6579818?v=3","gravatar_id":"","url":"https://api.github.com/users/ivandrofly","html_url":"https://github.com/ivandrofly","followers_url":"https://api.github.com/users/ivandrofly/followers","following_url":"https://api.github.com/users/ivandrofly/following{/other_user}","gists_url":"https://api.github.com/users/ivandrofly/gists{/gist_id}","starred_url":"https://api.github.com/users/ivandrofly/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ivandrofly/subscriptions","organizations_url":"https://api.github.com/users/ivandrofly/orgs","repos_url":"https://api.github.com/users/ivandrofly/repos","events_url":"https://api.github.com/users/ivandrofly/events{/privacy}","received_events_url":"https://api.github.com/users/ivandrofly/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-07-03T17:12:30Z","updated_at":"2015-07-06T09:43:04Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/SubtitleEdit/subtitleedit/pulls/978","html_url":"https://github.com/SubtitleEdit/subtitleedit/pull/978","diff_url":"https://github.com/SubtitleEdit/subtitleedit/pull/978.diff","patch_url":"https://github.com/SubtitleEdit/subtitleedit/pull/978.patch"},"body":"# Do not merge yet!"},"comment":{"url":"https://api.github.com/repos/SubtitleEdit/subtitleedit/issues/comments/118787990","html_url":"https://github.com/SubtitleEdit/subtitleedit/pull/978#issuecomment-118787990","issue_url":"https://api.github.com/repos/SubtitleEdit/subtitleedit/issues/978","id":118787990,"user":{"login":"StevenThuriot","id":544444,"avatar_url":"https://avatars.githubusercontent.com/u/544444?v=3","gravatar_id":"","url":"https://api.github.com/users/StevenThuriot","html_url":"https://github.com/StevenThuriot","followers_url":"https://api.github.com/users/StevenThuriot/followers","following_url":"https://api.github.com/users/StevenThuriot/following{/other_user}","gists_url":"https://api.github.com/users/StevenThuriot/gists{/gist_id}","starred_url":"https://api.github.com/users/StevenThuriot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/StevenThuriot/subscriptions","organizations_url":"https://api.github.com/users/StevenThuriot/orgs","repos_url":"https://api.github.com/users/StevenThuriot/repos","events_url":"https://api.github.com/users/StevenThuriot/events{/privacy}","received_events_url":"https://api.github.com/users/StevenThuriot/received_events","type":"User","site_admin":false},"created_at":"2015-07-06T09:43:04Z","updated_at":"2015-07-06T09:43:04Z","body":"You could indeed inherit the `Regex` class since it isn't sealed, but I'm not completely sure if I follow what you would try to accomplish doing so? You won't be able to override the ctor, merely call it from your own code, so you'd basically have to re-implement all the static callers you use. The ctor where all the magic happens is a private one and which is also the endpoint for all the others (they call :this(...) )\r\n\r\nCare to explain further what you want to achieve?\r\n\r\n------------\r\n\r\nPersonally, I'd suggest the following:\r\n- Pull the `Regex.Replace` instances back outside of the for-loop so the `linkedlist` doesn't have to be traversed each iteration of the `for-loop`. (assuming they'll still be in the cache, worst case they'll be recompiled again)\r\n- Optimize the regexes created in `iList` to reduce memory instead :arrow_right: **huge gain**.\r\n\r\nFor instance, the following block of code \r\n\r\n```csharp\r\nMyRegEx(@\"\\bat i aldrig\\b\"),\r\nMyRegEx(@\"\\bat i alle bliver\\b\"),\r\nMyRegEx(@\"\\bat i alle er\\b\"),\r\nMyRegEx(@\"\\bat i alle forventer\\b\"),\r\nMyRegEx(@\"\\bat i alle gør\\b\"),\r\nMyRegEx(@\"\\bat i alle har\\b\"),\r\nMyRegEx(@\"\\bat i alle ved\\b\"),\r\nMyRegEx(@\"\\bat i alle vil\\b\"),\r\nMyRegEx(@\"\\bat i bare\\b\"),\r\nMyRegEx(@\"\\bat i bager\\b\"),\r\nMyRegEx(@\"\\bat i bruger\\b\"),\r\nMyRegEx(@\"\\bat i dræber\\b\"),\r\nMyRegEx(@\"\\bat i dræbte\\b\"),\r\nMyRegEx(@\"\\bat i fandt\\b\"),\r\nMyRegEx(@\"\\bat i fik\\b\"),\r\nMyRegEx(@\"\\bat i finder\\b\"),\r\nMyRegEx(@\"\\bat i forstår\\b\"),\r\nMyRegEx(@\"\\bat i får\\b\"),\r\nMyRegEx(@\"\\bat i ikke\\b\"),\r\nMyRegEx(@\"\\bat i kom\\b\"),\r\nMyRegEx(@\"\\bat i kommer\\b\"),\r\nMyRegEx(@\"\\bat i næsten er\\b\"),\r\nMyRegEx(@\"\\bat i næsten fik\\b\"),\r\nMyRegEx(@\"\\bat i næsten har\\b\"),\r\nMyRegEx(@\"\\bat i næsten skulle\\b\"),\r\nMyRegEx(@\"\\bat i næsten var\\b\"),\r\nMyRegEx(@\"\\bat i også får\\b\"),\r\nMyRegEx(@\"\\bat i også gør\\b\"),\r\nMyRegEx(@\"\\bat i også mener\\b\"),\r\nMyRegEx(@\"\\bat i også siger\\b\"),\r\nMyRegEx(@\"\\bat i også tror\\b\"),\r\nMyRegEx(@\"\\bat i rev\\b\"),\r\nMyRegEx(@\"\\bat i river\\b\"),\r\nMyRegEx(@\"\\bat i samarbejder\\b\"),\r\nMyRegEx(@\"\\bat i snakkede\\b\"),\r\nMyRegEx(@\"\\bat i scorer\\b\"),\r\nMyRegEx(@\"\\bat i siger\\b\"),\r\nMyRegEx(@\"\\bat i skal\\b\"),\r\nMyRegEx(@\"\\bat i skulle\\b\"),\r\nMyRegEx(@\"\\bat i to ikke\\b\"),\r\nMyRegEx(@\"\\bat i to siger\\b\"),\r\nMyRegEx(@\"\\bat i to har\\b\"),\r\nMyRegEx(@\"\\bat i to er\\b\"),\r\nMyRegEx(@\"\\bat i to bager\\b\"),\r\nMyRegEx(@\"\\bat i to skal\\b\"),\r\nMyRegEx(@\"\\bat i to gør\\b\"),\r\nMyRegEx(@\"\\bat i to får\\b\"),\r\nMyRegEx(@\"\\bat i udnyttede\\b\"),\r\nMyRegEx(@\"\\bat i udnytter\\b\"),\r\nMyRegEx(@\"\\bat i vil\\b\"),\r\nMyRegEx(@\"\\bat i ville\\b\"),\r\n```\r\n\r\nhas a lot of reoccurring pieces, so it can easily be replaced with just one single regex (off the cuff, sorry if I made a typo)\r\n\r\n```csharp\r\nMyRegEx(@\"\\bat i [aldrig|alle bliver|alle er|alle forventer|alle gør|alle har|alle ved|alle vil|bare|bager|bruger|dræber|dræbte|fandt|fik|finder|forstår|får|ikke|kom|kommer|næsten er|næsten fik|næsten har|næsten skulle|næsten var|også får|også gør|også mener|også siger|også tror|rev|river|samarbejder|snakkede|scorer|siger|skal|skulle|to ikke|to siger|to har|to er|to bager|to skal|to gør|to får|udnyttede|udnytter|vil|ville|ikke]\\b\")\r\n```\r\n\r\nThat is 51 regexes replaced with one. I did a quick count: the fixes list seems to contain almost 600 regexes and most of them can be grouped in the same way. You can probably easily cut that list from 600 to 10-15. Most of your memory allocation (and quite a bit actually) will be gained here. You could get it all into one single regex, but that might end up hard to read and difficult to maintain. Might be worth it, though, but I will leave that up to you to decide. In that case, the inner for-loop will be gone, too!\r\n\r\nAs an extra, I personally like to keep them in a separate class like this:\r\n\r\n```csharp\r\n    static class SubtitleEditRegex\r\n    {\r\n        public static class DanishLetterI\r\n        {\r\n            public static readonly IReadOnlyCollection<Regex> Fixes;\r\n\r\n            static DanishLetterI()\r\n            {\r\n                Fixes = new []\r\n                        {\r\n                            // not a complete list, more phrases will come\r\n                            new Regex(......, RegexOptions.Compiled)\r\n                            ...............\r\n                        };\r\n            }\r\n        }\r\n\r\n        public static class AnotherRegexFixingClass\r\n        {\r\n            public static readonly IReadOnlyCollection<Regex> Fixes;\r\n            .........\r\n        }\r\n    }\r\n```\r\n\r\nThe method would then just call\r\n\r\n```csharp\r\n                if (littleIRegex.IsMatch(text))\r\n                {\r\n                    foreach (var regex in SERegex.DanishLetterI.Fixes)\r\n                    {\r\n...\r\n```\r\n\r\nThe neat thing about the static class with subclasses is that they'll be easy to maintain *(wether using multiple files and `partial` or not)*, serve as a cache and the static ctor's of the subclasses will only be called when the subclass is accessed the first time, not the main class *(unlike a method on the main class)*, so only the ones you need at that time will be compiled. The subclass also makes it somewhat descriptive, like a method would. :smile: "}}