Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache bridge deserialization #138

Open
BeyondiOS opened this issue May 24, 2023 · 2 comments
Open

Cache bridge deserialization #138

BeyondiOS opened this issue May 24, 2023 · 2 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@BeyondiOS
Copy link

struct User3: Codable, Defaults.Serializable {
    let name: String
    let age: String
}

extension Defaults.Keys {
    static let user3 = Key<User3>("user3", default: .init(name: "Hello", age: "24"))
}
...
    func test2() {
        print(Defaults[.user3])
        print(Defaults[.user3])
    }
...

The above code will call bridge.deserialize twice. If user3 is an array and there are many attributes of the User3 type, will it affect performance?

Would it be better to cache the data after Defaults[.user3] is executed once?

extension Defaults.Serializable {
	....
	static func toValue(_ anyObject: Any) -> Self? {
		// Return directly if `anyObject` can cast to Value, since it means `Value` is a natively supported type.
		if
			isNativelySupportedType,
			let anyObject = anyObject as? Self
		{
			return anyObject
		} else if let value = bridge.deserialize(anyObject as? Serializable) {
			return value as? Self
		}

		return nil
	}
        ....
@sindresorhus
Copy link
Owner

sindresorhus commented Jun 20, 2023

It can affect performance yes, if the data is large and you're calling it many times in a loop. But in common cases, it should not be a problem. You should not store huge amounts of data in UserDefaults anyway.

I do think we should look into caching the deserialized data using an LRU cache.

Actually, the first thing we could do is to simply cache all calls for 5 seconds. That way, we can make sure that we don't waste time on deserializing things that are called in a loop.

@sindresorhus sindresorhus changed the title If it is a custom type, deserialize will be called every time it is used, will it affect performance? Cache bridge deserialization Jun 20, 2023
@sindresorhus sindresorhus added enhancement New feature or request help wanted Extra attention is needed labels Jun 20, 2023
@trevorturk
Copy link

trevorturk commented Jan 31, 2025

I just wanted to note that I'm working around this issue with a cache that gets read in the getter and reset in the setter, just an example for illustrative purposes, could be worth considering as a generic feature?

import Foundation

@MainActor
class WeatherManager: ObservableObject {
    static let shared = WeatherManager()
    private init() {}

    private let savedDataManager = SavedDataManager.shared
    private var cachedData: Weather?

    var weather: Weather? {
        get {
            guard cachedData == nil else { return cachedData }
            guard let data = savedDataManager.store.data(forKey: SavedDataManager.Keys.weather.rawValue) else { return nil }
            guard let weather = try? JSONDecoder.decoder.decode(Weather.self, from: data) else { return nil }
            cachedData = weather
            return weather
        }
        set {
            guard let data = try? JSONEncoder.encoder.encode(newValue) else { return }
            savedDataManager.store.set(data, forKey: SavedDataManager.Keys.weather.rawValue)
            cachedData = nil
            objectWillChange.send()
        }
    }

    func reset() {
        cachedData = nil
    }
}

The only real downside is that I need to call reset() if the iOS app updates the data, but the watch app needs to be informed. (I'm not sure if there's a better way to do this...)

Anyway, just wanted to toss that out as an alternative to a 5 second LRU cache. I'd say if we're caching by time, 1 second would be plenty, or ideally we could cache for the duration of a SwiftUI View being rendered, since I think that's the only place where this might be a common enough issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants