Skip to content

Commit e91d74b

Browse files
authored
Merge pull request #2441 from pinkjuice66/improve-bumpptrallocator
[SwiftSyntax] Refine several aspects of BumpPtrAllocator.
2 parents 9635de6 + 3e9de20 commit e91d74b

File tree

5 files changed

+10
-12
lines changed

5 files changed

+10
-12
lines changed

Sources/SwiftParser/Lexer/LexemeSequence.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ extension Lexer {
3030
///
3131
/// The memory footprint of not freeing past lexer states is negligible. It's
3232
/// usually less than 0.1% of the memory allocated by the syntax arena.
33-
var lexerStateAllocator = BumpPtrAllocator(slabSize: 256)
33+
var lexerStateAllocator = BumpPtrAllocator(initialSlabSize: 256)
3434

3535
/// The offset of the trailing trivia end of `nextToken` relative to the source buffer’s start.
3636
var offsetToNextTokenEnd: Int {

Sources/SwiftParser/StringLiteralRepresentedLiteralValue.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ extension StringSegmentSyntax {
7979
// defensive as it's currently not used by `lexCharacterInStringLiteral`.
8080
let state = Lexer.Cursor.State.inStringLiteral(kind: stringLiteralKind, delimiterLength: delimiterLength)
8181
let transition = Lexer.StateTransition.push(newState: state)
82-
cursor.perform(stateTransition: transition, stateAllocator: BumpPtrAllocator(slabSize: 256))
82+
cursor.perform(stateTransition: transition, stateAllocator: BumpPtrAllocator(initialSlabSize: 256))
8383

8484
while true {
8585
let lex = cursor.lexCharacterInStringLiteral(

Sources/SwiftSyntax/BumpPtrAllocator.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

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

23-
/// Initial slab size.
24-
private var slabSize: Int
24+
private let initialSlabSize: Int
2525

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

4040
/// Construct a new ``BumpPtrAllocator``.
41-
public init(slabSize: Int) {
42-
self.slabSize = slabSize
41+
public init(initialSlabSize: Int) {
42+
self.initialSlabSize = initialSlabSize
4343
slabs = []
4444
current = nil
4545
customSizeSlabs = []
@@ -48,8 +48,6 @@ public class BumpPtrAllocator {
4848

4949
deinit {
5050
/// Deallocate all memory.
51-
_totalBytesAllocated = 0
52-
current = nil
5351
while let slab = slabs.popLast() {
5452
slab.deallocate()
5553
}
@@ -61,7 +59,7 @@ public class BumpPtrAllocator {
6159
/// Calculate the size of the slab at the index.
6260
private func slabSize(at index: Int) -> Int {
6361
// Double the slab size every 'GROWTH_DELAY' slabs.
64-
return self.slabSize * (1 << min(30, index / Self.GROWTH_DELAY))
62+
return self.initialSlabSize * (1 << min(30, index / Self.GROWTH_DELAY))
6563
}
6664

6765
private func startNewSlab() {
@@ -114,7 +112,7 @@ public class BumpPtrAllocator {
114112
}
115113

116114
// If the size is too big, allocate a dedicated slab for it.
117-
if byteCount >= self.slabSize {
115+
if byteCount >= self.initialSlabSize {
118116
let customSlab = Slab.allocate(
119117
byteCount: byteCount,
120118
alignment: alignment

Sources/SwiftSyntax/SyntaxArena.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class SyntaxArena {
5858
}
5959

6060
fileprivate init(slabSize: Int) {
61-
self.allocator = BumpPtrAllocator(slabSize: slabSize)
61+
self.allocator = BumpPtrAllocator(initialSlabSize: slabSize)
6262
self.childRefs = []
6363
#if DEBUG || SWIFTSYNTAX_ENABLE_ASSERTIONS
6464
self.hasParent = false

Tests/SwiftSyntaxTest/BumpPtrAllocatorTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import XCTest
1616
final class BumpPtrAllocatorTests: XCTestCase {
1717

1818
func testBasic() {
19-
let allocator = BumpPtrAllocator(slabSize: 4096)
19+
let allocator = BumpPtrAllocator(initialSlabSize: 4096)
2020

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

0 commit comments

Comments
 (0)