This commit is contained in:
Michael Schimmel
2025-10-31 18:12:53 +01:00
parent 0526ec8a24
commit 8abec8e98f
8 changed files with 199 additions and 106 deletions
+110 -28
View File
@@ -10,7 +10,7 @@ uses
type
// The runtime representation of an interned keyword
IKeyword = interface(IInterface)
IKeyword = interface
function GetIdx: Integer;
function GetName: string;
property Idx: Integer read GetIdx;
@@ -42,26 +42,52 @@ type
class function Intern(const AName: string): IKeyword; static;
end;
TKeywordMapping<T> = record
type
TField = record
Key: IKeyword;
Value: T;
public
constructor Create(const AKey: IKeyword; const AValue: T);
end;
private
FMap: TArray<Integer>;
FFields: TArray<TField>;
FFirst, FLast: Integer;
public
constructor Create(const AFields: TArray<TField>);
// Defines a mapping from Keywords to a generic value T
IKeywordMapping<T> = interface
{$region 'private'}
function GetFields: TArray<TPair<IKeyword, T>>;
{$endregion}
// Finds the 0-based index for a given keyword. Returns -1 if not found.
function IndexOf(const Key: IKeyword): Integer;
property Fields: TArray<TField> read FFields;
// Gets all fields in the mapping.
property Fields: TArray<TPair<IKeyword, T>> read GetFields;
end;
// Factory for creating IKeywordMapping instances
TKeywordMappingRegistry<T> = record
strict private
// Implementation class for IKeywordMapping
type
TKeywordMapping = class(TInterfacedObject, IKeywordMapping<T>)
private
FMap: TArray<Integer>;
FFields: TArray<TPair<IKeyword, T>>;
FFirst, FLast: Integer;
function GetFields: TArray<TPair<IKeyword, T>>;
public
// Expects AFields to be in the desired (canonical) order
constructor Create(const AFields: TArray<TPair<IKeyword, T>>);
function IndexOf(const Key: IKeyword): Integer;
end;
strict private
// Cache implementation
class var
FLock: TSpinLock;
class var
FRegistry: TDictionary<TArray<Integer>, IKeywordMapping<T>>;
class constructor Create;
class destructor Destroy;
public
// Creates or gets the cached keyword mapping.
class function Intern(const AFields: TArray<TPair<IKeyword, T>>): IKeywordMapping<T>; static;
end;
implementation
uses
System.Generics.Defaults; // For TComparer<Integer>
{ TKeywordRegistry.TKeyword }
constructor TKeywordRegistry.TKeyword.Create(const AName: string; AIdx: Integer);
@@ -108,21 +134,25 @@ begin
end;
end;
{ TKeywordMapping }
{ TKeywordMappingRegistry<T>.TKeywordMapping<T> }
constructor TKeywordMapping<T>.Create(const AFields: TArray<TField>);
constructor TKeywordMappingRegistry<T>.TKeywordMapping.Create(const AFields: TArray<TPair<IKeyword, T>>);
var
i, idx: Integer;
begin
inherited Create;
FFields := AFields;
FFirst := 0;
FLast := -1;
if Length(FFields) = 0 then
exit;
// Find min/max Idx for the map. Fields are *not* assumed to be sorted.
FFirst := FFields[0].Key.Idx;
FLast := FFirst;
for var i := 1 to High(FFields) do
for i := 1 to High(FFields) do
begin
var idx := FFields[i].Key.Idx;
idx := FFields[i].Key.Idx;
if FFirst > idx then
FFirst := idx;
if FLast < idx then
@@ -130,25 +160,77 @@ begin
end;
SetLength(FMap, FLast - FFirst + 1);
for var i := 0 to High(FMap) do
for i := 0 to High(FMap) do
FMap[i] := -1;
for var i := 0 to High(FFields) do
FMap[FFirst + FFields[i].Key.Idx] := i;
// Populate map based on the *original field order*
for i := 0 to High(FFields) do
FMap[FFields[i].Key.Idx - FFirst] := i;
end;
function TKeywordMapping<T>.IndexOf(const Key: IKeyword): Integer;
function TKeywordMappingRegistry<T>.TKeywordMapping.GetFields: TArray<TPair<IKeyword, T>>;
begin
var idx := Key.Idx - FFirst;
Result := FFields;
end;
function TKeywordMappingRegistry<T>.TKeywordMapping.IndexOf(const Key: IKeyword): Integer;
var
idx: Integer;
begin
idx := Key.Idx - FFirst;
if (idx < 0) or (Idx > High(FMap)) then
exit(-1);
Result := FMap[idx];
end;
constructor TKeywordMapping<T>.TField.Create(const AKey: IKeyword; const AValue: T);
{ TKeywordMappingRegistry<T> }
class constructor TKeywordMappingRegistry<T>.Create;
begin
Key := AKey;
Value := AValue;
FLock := TSpinLock.Create(false);
FRegistry := TDictionary<TArray<Integer>, IKeywordMapping<T>>.Create;
end;
class destructor TKeywordMappingRegistry<T>.Destroy;
begin
FRegistry.Free;
end;
class function TKeywordMappingRegistry<T>.Intern(const AFields: TArray<TPair<IKeyword, T>>): IKeywordMapping<T>;
var
key: TArray<Integer>;
begin
SetLength(key, Length(AFields));
for var i := 0 to High(key) do
key[i] := AFields[i].Key.Idx;
// 2. Lock
FLock.Enter;
try
// 3. Check cache
if FRegistry.TryGetValue(key, Result) then
exit; // Found it, exit
finally
FLock.Exit;
end;
// 4. Not in cache. Create (O(N+M)) - OUTSIDE lock
var newMapping := TKeywordMapping.Create(AFields) as IKeywordMapping<T>;
// 5. Lock again to add
FLock.Enter;
try
// 6. Check again (double-check)
if FRegistry.TryGetValue(key, Result) then
exit; // Another thread won the race
// 7. Add our new mapping.
Result := newMapping;
FRegistry.Add(key, Result);
finally
FLock.Exit;
end;
end;
end.
+3 -3
View File
@@ -18,7 +18,7 @@ type
// Creates a JSON definition string from a record type info.
class function RecordDefinitionToJson<T: record>: string; static;
// Creates a record definition from a JSON string.
class function JsonToRecordDefinition(const AJson: string): TScalarRecordDefinition; static;
class function JsonToRecordDefinition(const AJson: string): IScalarRecordDefinition; static;
end;
implementation
@@ -30,7 +30,7 @@ uses
{ TRttiAstHelper }
class function TRttiAstHelper.JsonToRecordDefinition(const AJson: string): TScalarRecordDefinition;
class function TRttiAstHelper.JsonToRecordDefinition(const AJson: string): IScalarRecordDefinition;
var
jsonValue: TJSONValue;
fieldsArray: TJSONArray;
@@ -63,7 +63,7 @@ begin
fields[i] := TScalarRecordField.Create(fieldKey, TScalar.TKind(GetEnumValue(TypeInfo(TScalar.TKind), kindStr)));
end;
Result := TScalarRecordDefinition.Create(fields);
Result := TScalarRecordRegistry.Intern(fields);
finally
jsonValue.Free;
end;
+61 -42
View File
@@ -4,6 +4,8 @@ interface
uses
System.SysUtils,
System.Generics.Collections,
System.Generics.Defaults,
Myc.Data.Decimal,
Myc.Data.Series,
Myc.Data.Keyword;
@@ -92,21 +94,22 @@ type
TScalarTuple = TArray<TScalar>;
// A field definition for a scalar record.
TScalarRecordField = TKeywordMapping<TScalar.TKind>.TField;
TScalarRecordDefinition = TKeywordMapping<TScalar.TKind>;
TScalarRecordField = TPair<IKeyword, TScalar.TKind>;
IScalarRecordDefinition = IKeywordMapping<TScalar.TKind>;
TScalarRecordRegistry = TKeywordMappingRegistry<TScalar.TKind>;
// A record of scalar values, based on a definition.
TScalarRecord = record
public
constructor Create(const ADef: TScalarRecordDefinition; const AFields: TArray<TScalar.TValue>);
function GetDef: TScalarRecordDefinition; inline;
constructor Create(const ADef: IScalarRecordDefinition; const AFields: TArray<TScalar.TValue>);
function GetDef: IScalarRecordDefinition; inline;
function GetFields: TArray<TScalar.TValue>; inline;
function GetKeys(const Key: IKeyword): TScalar;
property Def: TScalarRecordDefinition read GetDef;
property Def: IScalarRecordDefinition read GetDef;
property Fields: TArray<TScalar.TValue> read GetFields;
property Keys[const Key: IKeyword]: TScalar read GetKeys; default;
strict private
FDef: TScalarRecordDefinition;
FDef: IScalarRecordDefinition;
FFields: TArray<TScalar.TValue>;
end;
@@ -142,22 +145,21 @@ type
IRecordSeries = interface
{$region 'private'}
function GetCount: Int64;
function GetDef: TScalarRecordDefinition;
function GetItems(Idx: Integer): TScalarRecord;
function GetDef: IScalarRecordDefinition;
function GetTotalCount: Int64;
function GetFields(const Key: IKeyword): ISeries;
{$endregion}
procedure Add(const Item: TScalarRecord; Lookback: Int64 = -1);
function CreateMemberSeries(const Key: IKeyword): ISeries;
property Count: Int64 read GetCount;
property Def: TScalarRecordDefinition read GetDef;
property Items[Idx: Integer]: TScalarRecord read GetItems; default;
property Def: IScalarRecordDefinition read GetDef;
property Fields[const Key: IKeyword]: ISeries read GetFields; default;
property TotalCount: Int64 read GetTotalCount;
end;
// A time series of scalar records, optimized for memory and access speed.
TScalarRecordSeries = class(TInterfacedObject, IRecordSeries)
type
TMemberSeries = class(TInterfacedObject, ISeries)
TMemberSeries = class(TObject, ISeries)
private
FRecordSeries: TScalarRecordSeries;
FKind: TScalar.TKind;
@@ -165,26 +167,29 @@ type
function GetCount: Int64;
function GetItems(Idx: Integer): TScalar;
function GetTotalCount: Int64;
function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
public
constructor Create(ARecordSeries: TScalarRecordSeries; AElementIdx: Integer);
destructor Destroy; override;
end;
private
FDef: TScalarRecordDefinition;
FDef: IScalarRecordDefinition;
FArray: TChunkArray<TScalar.TValue>;
FFields: TArray<TMemberSeries>;
FTotalCount: Int64;
function GetCount: Int64; inline;
function GetDef: TScalarRecordDefinition; inline;
function GetItems(Idx: Integer): TScalarRecord; inline;
function GetFields(const Key: IKeyword): ISeries;
function GetDef: IScalarRecordDefinition; inline;
function GetTotalCount: Int64; inline;
function GetItemRef(Idx: Integer): TChunkArray<TScalar.TValue>.PT; inline;
public
constructor Create(const ADef: TScalarRecordDefinition);
constructor Create(const ADef: IScalarRecordDefinition);
destructor Destroy; override;
procedure Add(const Item: TScalarRecord; Lookback: Int64 = -1);
function CreateMemberSeries(const Key: IKeyword): ISeries;
property Count: Int64 read GetCount;
property Def: TScalarRecordDefinition read GetDef;
property Items[Idx: Integer]: TScalarRecord read GetItems; default;
property Fields[const Key: IKeyword]: ISeries read GetFields; default;
property Def: IScalarRecordDefinition read GetDef;
property TotalCount: Int64 read GetTotalCount;
end;
@@ -519,14 +524,14 @@ end;
{ TScalarRecord }
constructor TScalarRecord.Create(const ADef: TScalarRecordDefinition; const AFields: TArray<TScalar.TValue>);
constructor TScalarRecord.Create(const ADef: IScalarRecordDefinition; const AFields: TArray<TScalar.TValue>);
begin
Assert(Length(ADef.Fields) = Length(AFields), 'Field definition and value count must match.');
FDef := ADef;
FFields := AFields;
end;
function TScalarRecord.GetDef: TScalarRecordDefinition;
function TScalarRecord.GetDef: IScalarRecordDefinition;
begin
Result := FDef;
end;
@@ -549,11 +554,21 @@ end;
{ TScalarRecordSeries }
constructor TScalarRecordSeries.Create(const ADef: TScalarRecordDefinition);
constructor TScalarRecordSeries.Create(const ADef: IScalarRecordDefinition);
begin
Assert(Length(ADef.Fields) > 0);
FDef := ADef;
FTotalCount := 0;
SetLength(FFields, Length(FDef.Fields));
for var i := 0 to High(FFields) do
FFields[i] := TMemberSeries.Create(Self, i);
end;
destructor TScalarRecordSeries.Destroy;
begin
for var i := High(FFields) downto 0 do
FFields[i].Free;
inherited;
end;
procedure TScalarRecordSeries.Add(const Item: TScalarRecord; Lookback: Int64 = -1);
@@ -562,13 +577,13 @@ begin
inc(FTotalCount);
end;
function TScalarRecordSeries.CreateMemberSeries(const Key: IKeyword): ISeries;
function TScalarRecordSeries.GetFields(const Key: IKeyword): ISeries;
begin
var elem := FDef.IndexOf(Key);
if elem < 0 then
raise EArgumentException.CreateFmt('Field ":%s" not found in record definition.', [Key.Name]);
Result := TMemberSeries.Create(Self, elem);
Result := FFields[elem];
end;
function TScalarRecordSeries.GetCount: Int64;
@@ -576,7 +591,7 @@ begin
Result := FArray.Count div Length(FDef.Fields);
end;
function TScalarRecordSeries.GetDef: TScalarRecordDefinition;
function TScalarRecordSeries.GetDef: IScalarRecordDefinition;
begin
Result := FDef;
end;
@@ -587,15 +602,6 @@ begin
Result := FArray.ItemRef[(FArray.Count - len) - (Idx * len)];
end;
function TScalarRecordSeries.GetItems(Idx: Integer): TScalarRecord;
var
values: TArray<TScalar.TValue>;
begin
SetLength(values, Length(FDef.Fields));
Move(GetItemRef(Idx)^, values[0], sizeof(TScalar.TValue) * Length(values));
Result.Create(FDef, values);
end;
function TScalarRecordSeries.GetTotalCount: Int64;
begin
Result := FTotalCount;
@@ -643,21 +649,16 @@ begin
end;
end;
{ TScalarRecordSeries.TMemberSeries }
constructor TScalarRecordSeries.TMemberSeries.Create(ARecordSeries: TScalarRecordSeries; AElementIdx: Integer);
begin
inherited Create;
FRecordSeries := ARecordSeries;
FRecordSeries._AddRef;
FKind := FRecordSeries.FDef.Fields[AElementIdx].Value;
FOffset := sizeof(TScalar.TValue) * AElementIdx;
end;
destructor TScalarRecordSeries.TMemberSeries.Destroy;
begin
FRecordSeries._Release;
inherited;
end;
function TScalarRecordSeries.TMemberSeries.GetCount: Int64;
begin
Result := FRecordSeries.Count;
@@ -677,6 +678,24 @@ begin
Result := FRecordSeries.TotalCount;
end;
function TScalarRecordSeries.TMemberSeries.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
if GetInterface(IID, Obj) then
Result := S_OK
else
Result := E_NOINTERFACE;
end;
function TScalarRecordSeries.TMemberSeries._AddRef: Integer;
begin
Result := FRecordSeries._AddRef;
end;
function TScalarRecordSeries.TMemberSeries._Release: Integer;
begin
Result := FRecordSeries._Release;
end;
{ TScalarSeries }
constructor TScalarSeries.Create(AKind: TScalar.TKind);