Made iterations skip only unread fields, Sped up string reads

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-05 12:26:03 +01:00
parent ef5648d26a
commit b50cf2406e
4 changed files with 47 additions and 22 deletions
+7 -1
View File
@@ -275,6 +275,10 @@ Structure MsgPackData
; Number of fields left to iterate on, map keys and values included, only used by views.
IteratorFieldsLeft.q
; Number of fields the offset was moved past by reads, skips and examinations since the current entry,
; so `MsgPackNextEntry()` only skips the ones that weren't read. Only used by views.
IteratorFieldsRead.q
; Number of arrays and maps containing the data, `0` for regular structures, `1` for views over a top-level one.
; Counted against `#MsgPack_MaxDepth`.
IteratorDepth.i
@@ -313,10 +317,12 @@ Macro _MsgPackOverlapsBuffer(MsgPackWrapper, Pointer, Size)
(Pointer <> #Null And (Size) > 0 And Pointer < MsgPackWrapper\Buffer + MsgPackWrapper\BufferSize And Pointer + (Size) > MsgPackWrapper\Buffer)
EndMacro
; Moves the offset past a field of `Size` bytes that was fully read, if the flags allow it.
; Moves the offset past a single field of `Size` bytes that was fully read, if the flags allow it.
; Every read moving the offset must use it, `MsgPackNextEntry()` relies on `IteratorFieldsRead`.
Macro _MsgPackMoveOffsetAfterRead(MsgPackWrapper, Size)
If _MsgPackHasFlag(MsgPackWrapper, #MsgPack_DataFlags_MoveOffsetAfterRead)
MsgPackWrapper\BufferOffset = MsgPackWrapper\BufferOffset + (Size)
MsgPackWrapper\IteratorFieldsRead = MsgPackWrapper\IteratorFieldsRead + 1
EndIf
EndMacro
+27 -3
View File
@@ -485,6 +485,8 @@ EndProcedure
; Map keys and values count as separate fields.
; Returns `#MsgPack_Error_Success` on success, something else otherwise, in which case the offset isn't moved.
Procedure.MsgPack_ErrorCode MsgPackSkip(*MsgPackData.MsgPackData, FieldCount.q = 1)
Protected ErrorCode.MsgPack_ErrorCode
CompilerIf Not #MsgPack_DisableNullChecks
If Not *MsgPackData
DebuggerError("A #Null MsgPackData pointer was passed !")
@@ -498,7 +500,12 @@ Procedure.MsgPack_ErrorCode MsgPackSkip(*MsgPackData.MsgPackData, FieldCount.q =
ProcedureReturn #MsgPack_Error_InvalidValue
EndIf
ProcedureReturn _MsgPackSkipFields(*MsgPackData, FieldCount)
ErrorCode = _MsgPackSkipFields(*MsgPackData, FieldCount)
If ErrorCode = #MsgPack_Error_Success
*MsgPackData\IteratorFieldsRead = *MsgPackData\IteratorFieldsRead + FieldCount
EndIf
ProcedureReturn ErrorCode
EndProcedure
@@ -585,6 +592,8 @@ Procedure.i MsgPackExamine(*MsgPackData.MsgPackData, ExaminationOptions.MsgPack_
If IsUnsafe Or Not _MsgPackHasFlag(*MsgPackData, #MsgPack_DataFlags_MoveOffsetAfterRead)
*MsgPackData\BufferOffset = StartOffset
Else
*MsgPackData\IteratorFieldsRead = *MsgPackData\IteratorFieldsRead + 1
EndIf
*MsgPackData\LastError = #MsgPack_Error_Success
@@ -599,6 +608,7 @@ EndProcedure
; OnError: Returns `#False` and sets the `MsgPackData\LastError` field.
Procedure.b MsgPackNextEntry(*Iterator.MsgPackData)
Protected FieldsPerEntry.q = 1
Protected FieldsToSkip.q
CompilerIf Not #MsgPack_DisableNullChecks
If Not *Iterator
@@ -617,20 +627,34 @@ Procedure.b MsgPackNextEntry(*Iterator.MsgPackData)
; First call, the first entry is at the very start of the view.
*Iterator\IteratorEntryOffset = 0
ElseIf *Iterator\IteratorFieldsLeft > 0
; Skipping from the entry's start, regardless of what was read from it.
If _MsgPackHasFlag(*Iterator, #MsgPack_DataFlags_CanProvideKeys)
FieldsPerEntry = 2
EndIf
; Only the fields that weren't read are skipped, from where the reads left the offset.
; Reading past the entry falls back to skipping all of it from its start.
If *Iterator\IteratorFieldsRead > FieldsPerEntry
*Iterator\BufferOffset = *Iterator\IteratorEntryOffset
If _MsgPackSkipFields(*Iterator, FieldsPerEntry) <> #MsgPack_Error_Success
FieldsToSkip = FieldsPerEntry
Else
FieldsToSkip = FieldsPerEntry - *Iterator\IteratorFieldsRead
EndIf
If FieldsToSkip > 0
If _MsgPackSkipFields(*Iterator, FieldsToSkip) <> #MsgPack_Error_Success
; Failing again from the entry's start, so the error doesn't depend on what was read,
; the "more fields left than bytes left" check may catch it earlier from there.
*Iterator\BufferOffset = *Iterator\IteratorEntryOffset
_MsgPackSkipFields(*Iterator, FieldsPerEntry)
ProcedureReturn #False
EndIf
EndIf
*Iterator\IteratorEntryOffset = *Iterator\BufferOffset
*Iterator\IteratorFieldsLeft = *Iterator\IteratorFieldsLeft - FieldsPerEntry
EndIf
*Iterator\BufferOffset = *Iterator\IteratorEntryOffset
*Iterator\IteratorFieldsRead = 0
*Iterator\LastError = #MsgPack_Error_Success
If *Iterator\IteratorFieldsLeft <= 0
+2 -8
View File
@@ -73,16 +73,10 @@ Procedure.b MsgPackReadBoolean(*MsgPackData.MsgPackData)
; We don't check if we're 1 byte off the end since the previous check implies it.
If FastPeekA(*MsgPackData\Buffer + *MsgPackData\BufferOffset) = #MsgPack_FormatCode_False
If _MsgPackHasFlag(*MsgPackData, #MsgPack_DataFlags_MoveOffsetAfterRead)
*MsgPackData\BufferOffset = *MsgPackData\BufferOffset + 1
EndIf
_MsgPackMoveOffsetAfterRead(*MsgPackData, 1)
ProcedureReturn #False
ElseIf FastPeekA(*MsgPackData\Buffer + *MsgPackData\BufferOffset) = #MsgPack_FormatCode_True
If _MsgPackHasFlag(*MsgPackData, #MsgPack_DataFlags_MoveOffsetAfterRead)
*MsgPackData\BufferOffset = *MsgPackData\BufferOffset + 1
EndIf
_MsgPackMoveOffsetAfterRead(*MsgPackData, 1)
ProcedureReturn #True
Else
Debug("The format code isn't the one for a boolean ! (" +
+8 -7
View File
@@ -116,7 +116,6 @@ Procedure.s MsgPackReadString(*MsgPackData.MsgPackData, Encoding.i = #PB_UTF8)
Protected HeaderSize.i
Protected StringSize.q
Protected *StringStart
Protected ReturnedValue.s = ""
If _MsgPackReadSizedFieldHeader(*MsgPackData, #MsgPack_DataTypes_String, @HeaderSize, @StringSize) <> #MsgPack_Error_Success
ProcedureReturn ""
@@ -136,20 +135,22 @@ Procedure.s MsgPackReadString(*MsgPackData.MsgPackData, Encoding.i = #PB_UTF8)
*StringStart = *MsgPackData\Buffer + *MsgPackData\BufferOffset + HeaderSize
; Moved before decoding, `PeekS()` can't fail.
_MsgPackMoveOffsetAfterRead(*MsgPackData, HeaderSize + StringSize)
; Returned directly, assigning `PeekS()` to a local first copies the string once more.
If StringSize > 0
Select Encoding
Case #PB_UTF8
ReturnedValue = PeekS(*StringStart, StringSize, #PB_UTF8 | #PB_ByteLength)
ProcedureReturn PeekS(*StringStart, StringSize, #PB_UTF8 | #PB_ByteLength)
Case #PB_Ascii
ReturnedValue = PeekS(*StringStart, StringSize, #PB_Ascii)
ProcedureReturn PeekS(*StringStart, StringSize, #PB_Ascii)
Case #PB_Unicode
ReturnedValue = PeekS(*StringStart, StringSize / 2, #PB_Unicode)
ProcedureReturn PeekS(*StringStart, StringSize / 2, #PB_Unicode)
EndSelect
EndIf
_MsgPackMoveOffsetAfterRead(*MsgPackData, HeaderSize + StringSize)
ProcedureReturn ReturnedValue
ProcedureReturn ""
EndProcedure