Skip to content

[SwiftSyntax] Refine several aspects of BumpPtrAllocator. #2441

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
Jan 29, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Sources/SwiftParser/Lexer/LexemeSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extension Lexer {
///
/// The memory footprint of not freeing past lexer states is negligible. It's
/// usually less than 0.1% of the memory allocated by the syntax arena.
var lexerStateAllocator = BumpPtrAllocator(slabSize: 256)
var lexerStateAllocator = BumpPtrAllocator(initialSlabSize: 256)

/// The offset of the trailing trivia end of `nextToken` relative to the source buffer’s start.
var offsetToNextTokenEnd: Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ extension StringSegmentSyntax {
// defensive as it's currently not used by `lexCharacterInStringLiteral`.
let state = Lexer.Cursor.State.inStringLiteral(kind: stringLiteralKind, delimiterLength: delimiterLength)
let transition = Lexer.StateTransition.push(newState: state)
cursor.perform(stateTransition: transition, stateAllocator: BumpPtrAllocator(slabSize: 256))
cursor.perform(stateTransition: transition, stateAllocator: BumpPtrAllocator(initialSlabSize: 256))

while true {
let lex = cursor.lexCharacterInStringLiteral(
Expand Down
14 changes: 6 additions & 8 deletions Sources/SwiftSyntax/BumpPtrAllocator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//

/// A ``BumpPtrAllocator`` that allocates `slabSize` at a time.
/// `slabSize` initiates with `initialSlabSize` and doubles periodically as allocations occur.
/// Once all memory in a slab has been used, it allocates a new slab and no
/// memory allocations are necessary until that slab is completely filled up.
@_spi(BumpPtrAllocator) @_spi(Testing)
Expand All @@ -20,8 +21,7 @@ public class BumpPtrAllocator {
static private let GROWTH_DELAY: Int = 128
static private let SLAB_ALIGNMENT: Int = 8

/// Initial slab size.
private var slabSize: Int
private let initialSlabSize: Int

private var slabs: [Slab]
/// Pair of pointers in the current slab.
Expand All @@ -38,8 +38,8 @@ public class BumpPtrAllocator {
private var _totalBytesAllocated: Int

/// Construct a new ``BumpPtrAllocator``.
public init(slabSize: Int) {
self.slabSize = slabSize
public init(initialSlabSize: Int) {
self.initialSlabSize = initialSlabSize
slabs = []
current = nil
customSizeSlabs = []
Expand All @@ -48,8 +48,6 @@ public class BumpPtrAllocator {

deinit {
/// Deallocate all memory.
_totalBytesAllocated = 0
current = nil
while let slab = slabs.popLast() {
slab.deallocate()
}
Expand All @@ -61,7 +59,7 @@ public class BumpPtrAllocator {
/// Calculate the size of the slab at the index.
private func slabSize(at index: Int) -> Int {
// Double the slab size every 'GROWTH_DELAY' slabs.
return self.slabSize * (1 << min(30, index / Self.GROWTH_DELAY))
return self.initialSlabSize * (1 << min(30, index / Self.GROWTH_DELAY))
}

private func startNewSlab() {
Expand Down Expand Up @@ -114,7 +112,7 @@ public class BumpPtrAllocator {
}

// If the size is too big, allocate a dedicated slab for it.
if byteCount >= self.slabSize {
if byteCount >= self.initialSlabSize {
let customSlab = Slab.allocate(
byteCount: byteCount,
alignment: alignment
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftSyntax/SyntaxArena.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class SyntaxArena {
}

fileprivate init(slabSize: Int) {
self.allocator = BumpPtrAllocator(slabSize: slabSize)
self.allocator = BumpPtrAllocator(initialSlabSize: slabSize)
self.childRefs = []
#if DEBUG || SWIFTSYNTAX_ENABLE_ASSERTIONS
self.hasParent = false
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftSyntaxTest/BumpPtrAllocatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import XCTest
final class BumpPtrAllocatorTests: XCTestCase {

func testBasic() {
let allocator = BumpPtrAllocator(slabSize: 4096)
let allocator = BumpPtrAllocator(initialSlabSize: 4096)

let byteBuffer = allocator.allocate(byteCount: 42, alignment: 4)
XCTAssertNotNil(byteBuffer.baseAddress)
Expand Down