Skip to content

Commit 07bb522

Browse files
committed
fix: significantly reduces memory usage
Avoid compiling RE repeatedly in loops, especially in large metadata scripts.
1 parent 8a08934 commit 07bb522

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

xcode/Ext-Safari/Functions.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,18 @@ func parse(_ content: String) -> [String: Any]? {
158158
var metadata = [:] as [String: [String]]
159159
// iterate through the possible metadata keys in file
160160
if let metas = Range(match.range(at: g2), in: content) {
161+
let p = #"^(?:[ \t]*(?:\/\/)?[ \t]*@)([\w-]+)[ \t]+([^\s]+[^\r\n\t\v\f]*)"#
162+
// this pattern checks for specific keys that won't have values
163+
let p2 = #"^(?:[ \t]*(?:\/\/)?[ \t]*@)(noframes)[ \t]*$"#
164+
// force try b/c pattern is known to be valid regex
165+
let re = try! NSRegularExpression(pattern: p, options: [])
166+
let re2 = try! NSRegularExpression(pattern: p2, options: [])
161167
// split metadatas by new line
162168
let metaArray = content[metas].split(whereSeparator: \.isNewline)
163169
// loop through metadata lines and populate metadata dictionary
164170
for meta in metaArray {
165-
let p = #"^(?:[ \t]*(?:\/\/)?[ \t]*@)([\w-]+)[ \t]+([^\s]+[^\r\n\t\v\f]*)"#
166-
// this pattern checks for specific keys that won't have values
167-
let p2 = #"^(?:[ \t]*(?:\/\/)?[ \t]*@)(noframes)[ \t]*$"#
168171
// the individual meta string, ie. // @name File Name
169172
let metaString = String(meta).trimmingCharacters(in: .whitespaces)
170-
// force try b/c pattern is known to be valid regex
171-
let re = try! NSRegularExpression(pattern: p, options: [])
172-
let re2 = try! NSRegularExpression(pattern: p2, options: [])
173173
let range = NSRange(location: 0, length: metaString.utf16.count)
174174
// key lines not properly prefixed & without values will be skipped
175175
if let m = re.firstMatch(in: metaString, options: [], range: range) {

0 commit comments

Comments
 (0)