From 53ae081cc9a3e61095cab49862658a46b00eacf7 Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Tue, 1 Oct 2024 19:43:11 +0200 Subject: [PATCH] avoid threadvar with const field table (#89) --- toml_serialization/private/array_reader.nim | 9 ++++----- toml_serialization/reader.nim | 20 +++++++++++--------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/toml_serialization/private/array_reader.nim b/toml_serialization/private/array_reader.nim index 58cff67..4991903 100644 --- a/toml_serialization/private/array_reader.nim +++ b/toml_serialization/private/array_reader.nim @@ -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] diff --git a/toml_serialization/reader.nim b/toml_serialization/reader.nim index d48c42a..bf36477 100644 --- a/toml_serialization/reader.nim +++ b/toml_serialization/reader.nim @@ -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: