avoid threadvar with const field table (#89)

This commit is contained in:
Jacek Sieka 2024-10-01 19:43:11 +02:00 committed by GitHub
parent 162358a675
commit 53ae081cc9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 14 deletions

View File

@ -12,7 +12,6 @@ import
type
ArrayReader*[RecordType, Reader] = tuple[
numRead: int,
fieldName: string,
reader: proc (rec: var RecordType, reader: var Reader, idx: int)
{.gcsafe, nimcall, raises: [SerializationError].}
@ -60,7 +59,7 @@ proc makeArrayReadersTable(RecordType, Reader: distinct type, L: static[int]):
when RecordType is tuple: obj[i] else: field(obj, realFieldName),
err)
result[fieldPos] = (0, fieldName, readArrayFieldImpl)
result[fieldPos] = (fieldName, readArrayFieldImpl)
inc fieldPos
template arrayReadersTable*(RecordType, Reader: distinct type): auto =
@ -77,6 +76,6 @@ proc findArrayReader*(fieldsTable: ArrayReadersTable,
result = BadArrayReader
template readArray*[T, Y](idx: int, fieldsTable: var ArrayReadersTable, rec: var T, r: var Y) =
fieldsTable[idx].reader(rec, r, fieldsTable[idx].numRead)
inc fieldsTable[idx].numRead
template readArray*[T, Y](idx: int, fieldsTable: ArrayReadersTable, numRead: var openArray[int], rec: var T, r: var Y) =
fieldsTable[idx].reader(rec, r, numRead[idx])
inc numRead[idx]

View File

@ -231,13 +231,15 @@ proc decodeRecord[T](r: var TomlReader, value: var T) =
const
totalFields = T.totalSerializedFields
arrayFields = T.totalArrayFields
when totalFields > 0:
let fields = T.fieldReadersTable(TomlReader)
const
fields = T.fieldReadersTable(TomlReader)
arrayFields = T.totalArrayFields
when arrayFields > 0:
var arrayReaders = T.arrayReadersTable(TomlReader)
const arrayReaders = T.arrayReadersTable(TomlReader)
var numRead: array[arrayReaders.len, int]
var
expectedFieldPos = 0
@ -287,7 +289,7 @@ proc decodeRecord[T](r: var TomlReader, value: var T) =
if r.state == ArrayOfTable:
let reader = findArrayReader(arrayReaders, fieldName, r.tomlCase)
if reader != BadArrayReader:
reader.readArray(arrayReaders, value, r)
reader.readArray(arrayReaders, numRead, value, r)
elif TomlUnknownFields in r.lex.flags:
r.skipTableBody
else:
@ -297,10 +299,10 @@ proc decodeRecord[T](r: var TomlReader, value: var T) =
continue
when value is tuple:
var reader = fields[][expectedFieldPos].reader
var reader = fields[expectedFieldPos].reader
expectedFieldPos += 1
else:
var reader = findFieldReader(fields[],
var reader = findFieldReader(fields,
fieldName, expectedFieldPos, r.tomlCase)
if reader != nil:
@ -331,7 +333,7 @@ proc decodeInlineTable[T](r: var TomlReader, value: var T) =
when totalFields > 0:
parseInlineTableImpl(r.lex):
# initial action
let fields = T.fieldReadersTable(TomlReader)
const fields = T.fieldReadersTable(TomlReader)
var
expectedFieldPos = 0
fieldName = newStringOfCap(defaultStringCapacity)
@ -345,10 +347,10 @@ proc decodeInlineTable[T](r: var TomlReader, value: var T) =
do:
# value action
when value is tuple:
var reader = fields[][expectedFieldPos].reader
var reader = fields[expectedFieldPos].reader
expectedFieldPos += 1
else:
var reader = findFieldReader(fields[],
var reader = findFieldReader(fields,
fieldName, expectedFieldPos, r.tomlCase)
if reader != nil: