From 093f1c264a2ffe7e4afd26c91ee2a5878c1764ab Mon Sep 17 00:00:00 2001 From: Richard Webb Date: Sun, 26 Apr 2026 11:42:11 +0100 Subject: [PATCH] Change the 'run ruie' functions to special case empty arrays, in order to reduce the number of allocated zero length arrays. As it stands, running the rules allocates a massive number of zero length arrays, which has quite a large effect on the amount of memory allocated. Many of these are from FSharp.Core functions which always create a new array, but which can be avoided with small local wrappers. --- src/FSharpLint.Core/Framework/Rules.fs | 6 +++--- src/FSharpLint.Core/Framework/Utilities.fs | 8 ++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/FSharpLint.Core/Framework/Rules.fs b/src/FSharpLint.Core/Framework/Rules.fs index d53629800..74dfb5f70 100644 --- a/src/FSharpLint.Core/Framework/Rules.fs +++ b/src/FSharpLint.Core/Framework/Rules.fs @@ -97,12 +97,12 @@ let toWarning (identifier:string) (ruleName:string) (filePath:string) (lines:str let runAstNodeRule (rule:RuleMetadata) (config:AstNodeRuleParams) = rule.RuleConfig.Runner config - |> Array.map (toWarning rule.Identifier rule.Name config.FilePath config.Lines) + |> Array.mapIfNotEmpty (toWarning rule.Identifier rule.Name config.FilePath config.Lines) let runLineRuleWithContext (rule:RuleMetadata>) (context:'Context) (config:LineRuleParams) = rule.RuleConfig.Runner context config - |> Array.map (toWarning rule.Identifier rule.Name config.FilePath config.Lines) + |> Array.mapIfNotEmpty (toWarning rule.Identifier rule.Name config.FilePath config.Lines) let runLineRule (rule:RuleMetadata) (config:LineRuleParams) = rule.RuleConfig.Runner config - |> Array.map (toWarning rule.Identifier rule.Name config.FilePath config.Lines) + |> Array.mapIfNotEmpty (toWarning rule.Identifier rule.Name config.FilePath config.Lines) diff --git a/src/FSharpLint.Core/Framework/Utilities.fs b/src/FSharpLint.Core/Framework/Utilities.fs index 8973b8ac1..f51f0b3c0 100644 --- a/src/FSharpLint.Core/Framework/Utilities.fs +++ b/src/FSharpLint.Core/Framework/Utilities.fs @@ -23,6 +23,14 @@ module Dictionary = dict.Add(key, value) +module Array = + + let inline mapIfNotEmpty ([] mapping: 'Source -> 'Dest) (array: 'Source array) = + if Array.isEmpty array + then Array.empty + else + Array.map mapping array + module ExpressionUtilities = open System