Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor optimization. Inlined method must be implemented before it is u… #190

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 52 additions & 52 deletions OtlCollections.pas
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,53 @@ destructor TOmniBlockingCollection.Destroy;
inherited Destroy;
end; { TOmniBlockingCollection.Destroy }

function TOmniBlockingCollection.TryAdd(const value: TOmniValue): boolean;
var
{$IFDEF MSWINDOWS}
awaited: cardinal;
{$ELSE}
waitResult: TWaitResult;
Signaller: IOmniSynchro;
{$ENDIF}
begin
obcAddCountAndCompleted.Increment;
try
// IsCompleted can not change during the execution of this function
Result := not IsCompleted;
if Result then begin
obcAccessed := true;
if obcThrottling and (obcApproxCount.Value >= obcHighWaterMark) then begin
{$IFDEF MSWINDOWS}
{$WARN SYMBOL_PLATFORM OFF}
Win32Check(ResetEvent(obcNotOverflow));
{$WARN SYMBOL_PLATFORM ON}
{$ELSE}
obcNotOverflow.Reset;
{$ENDIF ~MSWINDOWS}
// it's possible that messages were removed and obcNotOverflow set *before* the
// previous line has executed so test again ...
if obcThrottling and (obcApproxCount.Value >= obcHighWaterMark) then begin
obcAddCountAndCompleted.Decrement; // Leave the Add temporarily so that CompleteAdding can succeed
{$IFDEF MSWINDOWS}
awaited := DSiWaitForTwoObjects(obcCompletedSignal, obcNotOverflow, false, INFINITE);
obcAddCountAndCompleted.Increment; // Re-enter Add; queue may be now in 'completed' state
if (awaited = WAIT_OBJECT_0) or IsCompleted then begin
{$ELSE}
waitResult := FCompletedWaiter.WaitAny(INFINITE,Signaller);
obcAddCountAndCompleted.Increment;
if ((waitResult = wrSignaled) and (Signaller = obcCompletedSignal)) or IsCompleted then begin
{$ENDIF}
Result := false; // completed
Exit;
end;
end;
end;
obcCollection.Enqueue(value);
obcApproxCount.Increment;
end;
finally obcAddCountAndCompleted.Decrement; end;
end; { TOmniBlockingCollection.TryAdd }

procedure TOmniBlockingCollection.Add(const value: TOmniValue);
begin
if not TryAdd(value) then
Expand Down Expand Up @@ -421,6 +468,11 @@ function TOmniBlockingCollection.IsFinalized: boolean;
Result := IsCompleted and obcCollection.IsEmpty;
end; { TOmniBlockingCollection.IsFinalized }

function TOmniBlockingCollection.Take(var value: TOmniValue): boolean;
begin
Result := TryTake(value, INFINITE);
end; { TOmniBlockingCollection.Take }

function TOmniBlockingCollection.Next: TOmniValue;
begin
if not Take(Result) then
Expand All @@ -444,11 +496,6 @@ procedure TOmniBlockingCollection.SetThrottling(highWaterMark, lowWaterMark: int
obcThrottling := true;
end; { TOmniBlockingCollection.SetThrottling }

function TOmniBlockingCollection.Take(var value: TOmniValue): boolean;
begin
Result := TryTake(value, INFINITE);
end; { TOmniBlockingCollection.Take }

{$IFDEF OTL_Generics}
{$IFDEF OTL_HasArrayOfT}
{$IFDEF OTL_ERTTI}
Expand Down Expand Up @@ -616,53 +663,6 @@ class function TOmniBlockingCollection.ToArray<T>(const coll: IOmniBlockingColle
{$ENDIF OTL_HasArrayOfT}
{$ENDIF OTL_Generics}

function TOmniBlockingCollection.TryAdd(const value: TOmniValue): boolean;
var
{$IFDEF MSWINDOWS}
awaited: cardinal;
{$ELSE}
waitResult: TWaitResult;
Signaller: IOmniSynchro;
{$ENDIF}
begin
obcAddCountAndCompleted.Increment;
try
// IsCompleted can not change during the execution of this function
Result := not IsCompleted;
if Result then begin
obcAccessed := true;
if obcThrottling and (obcApproxCount.Value >= obcHighWaterMark) then begin
{$IFDEF MSWINDOWS}
{$WARN SYMBOL_PLATFORM OFF}
Win32Check(ResetEvent(obcNotOverflow));
{$WARN SYMBOL_PLATFORM ON}
{$ELSE}
obcNotOverflow.Reset;
{$ENDIF ~MSWINDOWS}
// it's possible that messages were removed and obcNotOverflow set *before* the
// previous line has executed so test again ...
if obcThrottling and (obcApproxCount.Value >= obcHighWaterMark) then begin
obcAddCountAndCompleted.Decrement; // Leave the Add temporarily so that CompleteAdding can succeed
{$IFDEF MSWINDOWS}
awaited := DSiWaitForTwoObjects(obcCompletedSignal, obcNotOverflow, false, INFINITE);
obcAddCountAndCompleted.Increment; // Re-enter Add; queue may be now in 'completed' state
if (awaited = WAIT_OBJECT_0) or IsCompleted then begin
{$ELSE}
waitResult := FCompletedWaiter.WaitAny(INFINITE,Signaller);
obcAddCountAndCompleted.Increment;
if ((waitResult = wrSignaled) and (Signaller = obcCompletedSignal)) or IsCompleted then begin
{$ENDIF}
Result := false; // completed
Exit;
end;
end;
end;
obcCollection.Enqueue(value);
obcApproxCount.Increment;
end;
finally obcAddCountAndCompleted.Decrement; end;
end; { TOmniBlockingCollection.TryAdd }

{$IFDEF MSWINDOWS}
function TOmniBlockingCollection.TryTake(var value: TOmniValue;
timeout_ms: cardinal): boolean;
Expand Down
22 changes: 11 additions & 11 deletions OtlSync.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1567,6 +1567,14 @@ class function Atomic<I,T>.Initialize(var storage: I): I;

{ Locked<T> }

procedure Locked<T>.Clear;
begin
FLifecycle := nil;
FInitialized := false;
FValue := Default(T);
FOwnsObject := false;
end; { Locked }

constructor Locked<T>.Create(const value: T; ownsObject: boolean);
begin
Clear;
Expand All @@ -1592,13 +1600,10 @@ procedure Locked<T>.Acquire;
FLock.Acquire;
end; { Locked<T>.Acquire }

procedure Locked<T>.Clear;
procedure Locked<T>.Release;
begin
FLifecycle := nil;
FInitialized := false;
FValue := Default(T);
FOwnsObject := false;
end; { Locked }
FLock.Release;
end; { Locked<T>.Release }

procedure Locked<T>.Free;
begin
Expand Down Expand Up @@ -1697,11 +1702,6 @@ procedure Locked<T>.Locked(proc: TProcT);
finally Release; end;
end; { Locked<T>.Locked }

procedure Locked<T>.Release;
begin
FLock.Release;
end; { Locked<T>.Release }

{$IFDEF MSWINDOWS}

{ TOmniLockManager<K>.TNotifyPair<K> }
Expand Down
10 changes: 5 additions & 5 deletions src/GpLists.pas
Original file line number Diff line number Diff line change
Expand Up @@ -7832,6 +7832,11 @@ procedure TGpSkipList<T, K>.Clear;
Initialize;
end; { TGpSkipList }

function TGpSkipList<T,K>.GetKey(const el: T): K;
begin
Result := FGetKey(el);
end; { TGpSkipList<T,K> }

function TGpSkipList<T,K>.Compare(const key: K; el: TGpSkipListEl<T>): integer;
begin
Result := Compare(key, GetKey(el.Element));
Expand Down Expand Up @@ -7964,11 +7969,6 @@ function TGpSkipList<T,K>.GetEnumerator: TGpSkipListEnumerator<T>;
Result := TGpSkipListEnumerator<T>.Create(FHead, FTail);
end; { TGpSkipList }

function TGpSkipList<T,K>.GetKey(const el: T): K;
begin
Result := FGetKey(el);
end; { TGpSkipList<T,K> }

procedure TGpSkipList<T, K>.Initialize;
var
iPtr: integer;
Expand Down
120 changes: 60 additions & 60 deletions src/GpStuff.pas
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,26 @@ implementation
NativeUInt = cardinal;
{$ENDIF}

procedure OutputDebugString(const msg: string);
begin
{$IFDEF MSWINDOWS}
{$WARN SYMBOL_PLATFORM OFF}
if DebugHook <> 0 then
Windows.OutputDebugString(PChar(msg));
{$WARN SYMBOL_PLATFORM ON}
{$ENDIF}
end; { OutputDebugString }

procedure OutputDebugString(const msg: string; const params: array of const);
begin
{$IFDEF MSWINDOWS}
{$WARN SYMBOL_PLATFORM OFF}
if DebugHook <> 0 then
OutputDebugString(Format(msg, params));
{$WARN SYMBOL_PLATFORM ON}
{$ENDIF}
end; { OutputDebugString }

{$IFDEF GpStuff_ValuesEnumerators}
type
TGpIntegerValueEnumerator = class(TInterfacedObject, IGpIntegerValueEnumerator)
Expand Down Expand Up @@ -1139,7 +1159,7 @@ function AutoExecute(proc: TProc): IGpAutoExecute;
{$ENDIF GpStuff_Anonymous}

//copied from GpString unit
procedure GetDelimiters(const list: string; delim: string; const quoteChar: string;
procedure GetDelimiters(const list: string; const delim: string; const quoteChar: string;
addTerminators: boolean; var delimiters: TDelimiters); overload;
var
chk : boolean;
Expand Down Expand Up @@ -1185,7 +1205,7 @@ procedure GetDelimiters(const list: string; delim: string; const quoteChar: stri
SetLength(delimiters,idx);
end; { GetDelimiters }

procedure GetDelimiters(const list: string; delim: TSysCharSet; const quoteChar: string;
procedure GetDelimiters(const list: string; const delim: TSysCharSet; const quoteChar: string;
addTerminators: boolean; var delimiters: TDelimiters); overload;
var
chk : boolean;
Expand Down Expand Up @@ -2479,26 +2499,6 @@ function GetRefCount(const intf: IInterface): integer;
intf._Release;
end; { GetRefCount }

procedure OutputDebugString(const msg: string);
begin
{$IFDEF MSWINDOWS}
{$WARN SYMBOL_PLATFORM OFF}
if DebugHook <> 0 then
Windows.OutputDebugString(PChar(msg));
{$WARN SYMBOL_PLATFORM ON}
{$ENDIF}
end; { OutputDebugString }

procedure OutputDebugString(const msg: string; const params: array of const);
begin
{$IFDEF MSWINDOWS}
{$WARN SYMBOL_PLATFORM OFF}
if DebugHook <> 0 then
OutputDebugString(Format(msg, params));
{$WARN SYMBOL_PLATFORM ON}
{$ENDIF}
end; { OutputDebugString }

function ClassNameEx(obj: TObject): string;
begin
if assigned(obj) then
Expand Down Expand Up @@ -2807,6 +2807,44 @@ function TGpMemoryStream.Write(const buffer; count: longint): longint;

{ TGpBuffer }

procedure TGpBuffer.Allocate(size: integer);
begin
Assert(size >= 0);
FData.Size := size;
end; { TGpBuffer.Allocate }

procedure TGpBuffer.Append(data: pointer; size: integer);
begin
if size > 0 then begin
FData.Seek(0, soEnd);
FData.Write(data^, size);
end;
end; { TGpBuffer.Append }

procedure TGpBuffer.Assign(data: pointer; size: integer);
begin
Allocate(size);
if size > 0 then
Move(data^, Value^, size);
end; { TGpBuffer.Assign }

procedure TGpBuffer.Assign(stream: TStream);
begin
Size := 0;
Append(stream);
end; { TGpBuffer.Assign }

procedure TGpBuffer.Assign(const buffer: IGpBuffer);
begin
Size := 0;
Append(buffer);
end; { TGpBuffer.Assign }

procedure TGpBuffer.Clear;
begin
Allocate(0);
end; { TGpBuffer.Clear }

constructor TGpBuffer.Create;
begin
inherited Create;
Expand Down Expand Up @@ -2924,20 +2962,6 @@ procedure TGpBuffer.Add(ch: AnsiChar);
end; { TGpBuffer.Add }
{$ENDIF}

procedure TGpBuffer.Allocate(size: integer);
begin
Assert(size >= 0);
FData.Size := size;
end; { TGpBuffer.Allocate }

procedure TGpBuffer.Append(data: pointer; size: integer);
begin
if size > 0 then begin
FData.Seek(0, soEnd);
FData.Write(data^, size);
end;
end; { TGpBuffer.Append }

procedure TGpBuffer.Append(stream: TStream);
begin
if stream.Size > 0 then begin
Expand All @@ -2951,30 +2975,6 @@ procedure TGpBuffer.Append(const buffer: IGpBuffer);
Append(buffer.Value, buffer.Size);
end; { TGpBuffer.Append }

procedure TGpBuffer.Assign(data: pointer; size: integer);
begin
Allocate(size);
if size > 0 then
Move(data^, Value^, size);
end; { TGpBuffer.Assign }

procedure TGpBuffer.Assign(stream: TStream);
begin
Size := 0;
Append(stream);
end; { TGpBuffer.Assign }

procedure TGpBuffer.Assign(const buffer: IGpBuffer);
begin
Size := 0;
Append(buffer);
end; { TGpBuffer.Assign }

procedure TGpBuffer.Clear;
begin
Allocate(0);
end; { TGpBuffer.Clear }

{$IFDEF MSWINDOWS}
function TGpBuffer.GetAsAnsiString: AnsiString;
var
Expand Down