Stabilize large CSV loading and large-mode detection

This commit is contained in:
h3p 2026-03-06 19:29:12 +01:00
parent 728c6ec1ab
commit f3e6e0099d
3 changed files with 23 additions and 3 deletions

View file

@ -361,7 +361,7 @@
CODE_SIGNING_ALLOWED = YES;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 400;
CURRENT_PROJECT_VERSION = 401;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_TEAM = CS727NF72U;
ENABLE_APP_SANDBOX = YES;
@ -444,7 +444,7 @@
CODE_SIGNING_ALLOWED = YES;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 400;
CURRENT_PROJECT_VERSION = 401;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_TEAM = CS727NF72U;
ENABLE_APP_SANDBOX = YES;

View file

@ -82,6 +82,10 @@ private enum EditorLoadHelper {
defer { input.close() }
var aggregate = Data()
if let expectedSize = try? url.resourceValues(forKeys: [.fileSizeKey]).fileSize,
expectedSize > 0 {
aggregate.reserveCapacity(expectedSize)
}
var buffer = [UInt8](repeating: 0, count: streamChunkBytes)
while true {
@ -1145,7 +1149,13 @@ class EditorViewModel {
let data: Data
if isLargeCandidate {
data = try EditorLoadHelper.streamFileData(from: url)
// Prefer memory-mapped IO for very large files to reduce peak memory churn.
// Fall back to streaming if mapping is unavailable for the provider.
if let mapped = try? Data(contentsOf: url, options: [.mappedIfSafe]) {
data = mapped
} else {
data = try EditorLoadHelper.streamFileData(from: url)
}
} else {
data = try Data(contentsOf: url, options: [.mappedIfSafe])
}

View file

@ -1441,12 +1441,22 @@ struct ContentView: View {
let exceedsLineThreshold: Bool = {
if exceedsByteThreshold { return true }
var lineBreaks = 0
var currentLineLength = 0
let csvLongLineThreshold = 16_000
for codeUnit in text.utf16 {
if codeUnit == 10 { // '\n'
lineBreaks += 1
currentLineLength = 0
if lineBreaks >= lineThreshold {
return true
}
continue
}
if isCSVLike {
currentLineLength += 1
if currentLineLength >= csvLongLineThreshold {
return true
}
}
}
return false