Skip to content

Prune nameString to reduce runtime size of Name #1806

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

Merged
merged 2 commits into from
Jul 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 2 additions & 17 deletions Sources/SwiftProtobuf/NameMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ public struct _NameMap: ExpressibleByDictionaryLiteral {
// coordinating the lifecycle with the lifecycle of the pool
// where the raw UTF8 gets interned.
fileprivate init(staticString: StaticString, pool: InternPool) {
self.nameString = .staticString(staticString)
if staticString.hasPointerRepresentation {
self.utf8Buffer = UnsafeRawBufferPointer(
start: staticString.utf8Start,
Expand All @@ -201,38 +200,24 @@ public struct _NameMap: ExpressibleByDictionaryLiteral {
fileprivate init(string: String, pool: InternPool) {
let utf8 = string.utf8
self.utf8Buffer = pool.intern(utf8: utf8)
self.nameString = .string(string)
}

// This is for building a transient `Name` object sufficient for lookup purposes.
// It MUST NOT be exposed outside of this file.
fileprivate init(transientUtf8Buffer: UnsafeRawBufferPointer) {
self.nameString = .staticString("")
self.utf8Buffer = transientUtf8Buffer
}

// This is for building a `Name` object from a slice of a bytecode `StaticString`.
// It MUST NOT be exposed outside of this file.
fileprivate init(bytecodeUTF8Buffer: UnsafeBufferPointer<UInt8>) {
self.nameString = .immortalBuffer(bytecodeUTF8Buffer)
self.utf8Buffer = UnsafeRawBufferPointer(bytecodeUTF8Buffer)
}

private(set) var utf8Buffer: UnsafeRawBufferPointer

private enum NameString {
case string(String)
case staticString(StaticString)
case immortalBuffer(UnsafeBufferPointer<UInt8>)
}
private var nameString: NameString
internal let utf8Buffer: UnsafeRawBufferPointer

public var description: String {
switch nameString {
case .string(let s): return s
case .staticString(let s): return s.description
case .immortalBuffer(let b): return String(decoding: b, as: UTF8.self)
}
String(decoding: self.utf8Buffer, as: UTF8.self)
}

public func hash(into hasher: inout Hasher) {
Expand Down