Watch, Follow, &
Connect with Us
Public Report
Report From: Delphi-BCB/Internet/WebSnap    [ Add a report in this area ]  
Report #:  12053   Status: Closed
TSessions can be a bottleneck in web applications
Project:  Delphi Build #:  9.0.1882.30496
Version:    9.0 Submitted By:   Simon Page
Report Type:  Suggestion / Enhancement Request Date Reported:  4/14/2005 6:09:31 AM
Severity:    Commonly encountered problem Last Updated: 3/20/2012 2:24:39 AM
Platform:    All versions Internal Tracking #:   239851
Resolution: Inactive (Resolution Comments) Resolved in Build: : 7.0.95.395
Duplicate of:  None
Voting and Rating
Overall Rating: (2 Total Ratings)
3.00 out of 5
Total Votes: 1
Description
In SessColn.pas there is the TSessions class for managing web session information.

This class protects its sessions list with a TMREWS, however the perioid of time it holds read or write locks on this list is far greater than it needs to be and this can seriously impact performance when the number of sessions increases.

The reason for the poor performance is that TSessions locates appropriate TSessionItems by using a linear search on the TSessionItems ID string field and during this search the list is of course read or write locked.

This search is done every time information is added or removed from a user's session and of course for general session management.

The fix is to use a binary search and this can be implemented by:
1) Adding a method in TSessions to QuickSort the collection by its string ID.
2) Adding a method in TSessions to perform a binary search on the TSessionID
3) Modify the TSessions.FindSession to use the binary search method.
4) Add a call in TSessions.LoadFromStream after loading to sort if there are 2 or more items.

Extra performance improvements can be achieved by improving TSessions.ItemValue by changing it from

function TSessions.ItemValue(AID: TSessionID; const AName: string): Variant;
var
  S: TAbstractNamedVariants;
begin
  S := TNamedVariantsList.Create;
  try
    GetItems(AID, S);
    Result := S.Values[AName];
  finally
    S.Free;
  end;
end;

to:

function TSessions.ItemValue(AID: TSessionID; const AName: string): Variant;
var
  Item: TSessionItem;
begin
  ReadLock;
  try
    Item := FindSession(AID);
    if Item <> nil then
      Result := Item.Items.Values[AName]
    else
      Result := Unassigned;
  finally
    ReadUnlock;
  end;
end;

and by changing TSessions.NewSession from:

function TSessions.NewSession: TSessionItem;
var
  ID: TSessionID;
begin
  ID := NewSessionID;
  Result := TSessionItem.Create(Self);
  Result.ID := ID;
end;

to:

function TSessions.NewSession: TSessionItem;
var
  ID: TSessionID;
  Index: Integer;
begin
  repeat
    ID := NextSessionID;  // Note:  TSessions.NewSessionID method now unused
  until not QuickFindIndex(ID, Index); // the new binary search method
  Result := TSessionItem.Create(Self);
  Result.ID := ID;
  Result.Index := Index;
end;

To reduce coding, a sorted TStringList could be added to provide the necessary binary seach and QuickSort but given how little code is required anyway, it seems better just add the methods to TSessions and sort the collection items directly.

Steps to Reproduce:
To see the impact of this problem you will need to use a simple websnap application which uses session management (preferably one that also uses sessions to store some user context) and simulate reasonable load on that application while it is being profiled with a suitable tool or add accurate timing to the applicable calls in TSessions.

We use the OpenSTA product to simulate the activity of multiple users and found that with many sessions the contention between threads trying to access session information became significant and worth rectifying.
Workarounds
None, other than modify SessColn.pas as described in the description.
Attachment
None
Comments

None

Server Response from: ETNACODE01