Skip to content

Commit b6746f0

Browse files
committed
[libSyntax] Improve performance for commonly requested SyntaxArena.containsPointer calls
In practice SyntaxArena.containsPointer is almost always called with a pointer from the SyntaxArena's source buffer. To avoid walking through all of the bump allocator's slabs until we find the one containing the source buffer, add a hot use memory region (which lives inside the bump allocator) that is checked first before consulting the bump allocator.
1 parent 5637c25 commit b6746f0

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

include/swift/Syntax/SyntaxArena.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,34 @@ class SyntaxArena : public llvm::ThreadSafeRefCountedBase<SyntaxArena> {
3131

3232
llvm::BumpPtrAllocator Allocator;
3333

34+
/// The start (inclusive) and end (exclusive) pointers of a memory region that
35+
/// is frequently requested using \c containsPointer. Must be inside \c
36+
/// Allocator but \c containsPointer will check this region first and will
37+
/// thus return more quickly for pointers that lie within this region.
38+
const void *HotUseMemoryRegionStart = nullptr;
39+
const void *HotUseMemoryRegionEnd = nullptr;
40+
3441
public:
3542
SyntaxArena() {}
3643

3744
static RC<SyntaxArena> make() { return RC<SyntaxArena>(new SyntaxArena()); }
3845

46+
void setHotUseMemoryRegion(const void *Start, const void *End) {
47+
assert(containsPointer(Start) &&
48+
"The hot use memory region should be in the Arena's bump allocator");
49+
HotUseMemoryRegionStart = Start;
50+
HotUseMemoryRegionEnd = End;
51+
}
52+
3953
llvm::BumpPtrAllocator &getAllocator() { return Allocator; }
4054
void *Allocate(size_t size, size_t alignment) {
4155
return Allocator.Allocate(size, alignment);
4256
}
4357

4458
bool containsPointer(const void *Ptr) {
59+
if (HotUseMemoryRegionStart <= Ptr && Ptr < HotUseMemoryRegionEnd) {
60+
return true;
61+
}
4562
return getAllocator().identifyObject(Ptr) != llvm::None;
4663
}
4764
};

lib/SyntaxParse/SyntaxTreeCreator.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ SyntaxTreeCreator::SyntaxTreeCreator(SourceManager &SM, unsigned bufferID,
4747
std::uninitialized_copy(BufferContent.begin(), BufferContent.end(), Data);
4848
ArenaSourceBuffer = StringRef(Data, BufferContent.size());
4949
assert(ArenaSourceBuffer == BufferContent);
50+
Arena->setHotUseMemoryRegion(ArenaSourceBuffer.begin(),
51+
ArenaSourceBuffer.end());
5052
}
5153

5254
SyntaxTreeCreator::~SyntaxTreeCreator() = default;

0 commit comments

Comments
 (0)