-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf!: improve performance of reading input
- Loading branch information
Showing
10 changed files
with
264 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
public import DequeModule | ||
|
||
public struct BufferQueue: ~Copyable, Sendable { | ||
@usableFromInline | ||
var buffers: Deque<ArraySlice<Unicode.Scalar>> | ||
|
||
@inlinable | ||
public init(_ buf: ArraySlice<Unicode.Scalar>) { | ||
self.buffers = [buf] | ||
} | ||
|
||
mutating func prepend(_ buf: ArraySlice<Unicode.Scalar>) { | ||
guard !buf.isEmpty else { return } | ||
self.buffers.prepend(buf) | ||
} | ||
|
||
func peek() -> Unicode.Scalar? { | ||
self.buffers.first.flatMap { $0.first } | ||
} | ||
|
||
mutating func popFirst() -> Unicode.Scalar? { | ||
guard !self.buffers.isEmpty else { return nil } | ||
defer { if self.buffers[0].isEmpty { self.buffers.removeFirst() } } | ||
return self.buffers[0].popFirst() | ||
} | ||
|
||
mutating func pop(except s: consuming SmallCharSet) -> PopResult? { | ||
guard !self.buffers.isEmpty else { return nil } | ||
defer { if self.buffers[0].isEmpty { self.buffers.removeFirst() } } | ||
let others = self.buffers[0].prefix { $0.value >= 64 || !s.contains($0) } | ||
self.buffers[0].removeFirst(others.count) | ||
return if others.isEmpty { | ||
self.buffers[0].popFirst().map(PopResult.known) | ||
} else { | ||
.others(others) | ||
} | ||
} | ||
|
||
mutating func removeFirst() { | ||
guard !self.buffers.isEmpty else { return } | ||
self.buffers[0].removeFirst() | ||
if self.buffers[0].isEmpty { self.buffers.removeFirst() } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
enum PopResult: ~Copyable { | ||
case known(Unicode.Scalar) | ||
case others(ArraySlice<Unicode.Scalar>) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
struct SmallCharSet { | ||
var bits: UInt64 | ||
|
||
@inlinable | ||
func contains(_ c: Unicode.Scalar) -> Bool { | ||
self.bits & 1 << c.value != 0 | ||
} | ||
} | ||
|
||
extension SmallCharSet: ExpressibleByArrayLiteral { | ||
init(arrayLiteral elements: Unicode.Scalar...) { | ||
self.bits = elements.lazy.map { 1 << $0.value }.reduce(0, |) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.