From faceaf1cd4d35cc8e7c0270417f440d11f35a4c2 Mon Sep 17 00:00:00 2001 From: Oscar Blanco Date: Fri, 4 Sep 2026 19:42:29 +0200 Subject: [PATCH] Cache the integer and decimal token sets like the string sets already are ConstrainedJSONGenerator.init unconditionally called buildValidIntegerTokens/buildValidDecimalTokens on every construction, each scanning the full vocabulary (0..]>([:]) + private let integerTokensByKey = Locked<[Key: Set]>([:]) + private let decimalTokensByKey = Locked<[Key: Set]>([:]) func tokens(for key: Key) -> Set? { tokensByKey.withLock { $0[key] } @@ -54,6 +56,22 @@ private final class StringTokenCache: @unchecked Sendable { func store(_ tokens: Set, for key: Key) { tokensByKey.withLock { $0[key] = tokens } } + + func integerTokens(for key: Key) -> Set? { + integerTokensByKey.withLock { $0[key] } + } + + func storeIntegerTokens(_ tokens: Set, for key: Key) { + integerTokensByKey.withLock { $0[key] = tokens } + } + + func decimalTokens(for key: Key) -> Set? { + decimalTokensByKey.withLock { $0[key] } + } + + func storeDecimalTokens(_ tokens: Set, for key: Key) { + decimalTokensByKey.withLock { $0[key] = tokens } + } } // MARK: - JSON Generator @@ -194,6 +212,11 @@ struct ConstrainedJSONGenerator { } private static func buildValidIntegerTokens(backend: Backend) -> Set { + let cacheKey = stringTokenCacheKey(for: backend) + if let cached = StringTokenCache.shared.integerTokens(for: cacheKey) { + return cached + } + var allowed = Set() for token in 0 ..< backend.vocabSize { if backend.isSpecialToken(token) { continue } @@ -204,10 +227,17 @@ struct ConstrainedJSONGenerator { allowed.insert(token) } } + + StringTokenCache.shared.storeIntegerTokens(allowed, for: cacheKey) return allowed } private static func buildValidDecimalTokens(backend: Backend) -> Set { + let cacheKey = stringTokenCacheKey(for: backend) + if let cached = StringTokenCache.shared.decimalTokens(for: cacheKey) { + return cached + } + var allowed = Set() for token in 0 ..< backend.vocabSize { if backend.isSpecialToken(token) { continue } @@ -218,6 +248,8 @@ struct ConstrainedJSONGenerator { allowed.insert(token) } } + + StringTokenCache.shared.storeDecimalTokens(allowed, for: cacheKey) return allowed }