Skip to content

Commit

Permalink
optimized TFB /rawfortunes and /asyncfortunes
Browse files Browse the repository at this point in the history
- don't allocate TFortune.Message string
- reuse a per-thread Mustache rendering context
  • Loading branch information
Arnaud Bouchez committed Apr 10, 2024
1 parent 068f23c commit 1a71fbf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
16 changes: 12 additions & 4 deletions ex/techempower-bench/raw.pas
Expand Up @@ -13,6 +13,7 @@

{.$define USE_SQLITE3}
// may be defined to use a SQLite3 database instead of external PostgresSQL DB
// - note that /rawfortunes and /async* won't work

{.$define WITH_LOGS}
// logging is fine for debugging, less for benchmarking ;)
Expand Down Expand Up @@ -62,7 +63,7 @@
TWorlds = array of TWorldRec;
TFortune = packed record
id: integer;
message: RawUtf8;
message: PUtf8Char;
end;
TFortunes = array of TFortune;

Expand Down Expand Up @@ -370,22 +371,29 @@ function TRawAsyncServer.ComputeRawFortunes(
arr: TDynArray;
n: integer;
f: ^TFortune;
mus: TSynMustacheContextData;
begin
result := HTTP_BADREQUEST;
{$ifndef USE_SQLITE3}
if stmt = nil then
{$endif USE_SQLITE3}
// stmt.ColumnPUtf8 and stmt.Connection.GetThreadOwned won't work on SQLite3
exit;
arr.Init(TypeInfo(TFortunes), list, @n);
while stmt.Step do
begin
f := arr.NewPtr;
f.id := stmt.ColumnInt(0);
f.message := stmt.ColumnUtf8(1);
f.message := stmt.ColumnPUtf8(1);
end;
f := arr.NewPtr;
f.id := 0;
f.message := FORTUNES_MESSAGE;
arr.Sort(FortuneCompareByMessage);
ctxt.OutContent := fTemplate.RenderDataArray(arr);
mus := stmt.Connection.GetThreadOwned(TSynMustacheContextData);
if mus = nil then
mus := stmt.Connection.SetThreadOwned(fTemplate.NewMustacheContextData);
ctxt.OutContent := mus.RenderArray(arr);
ctxt.OutContentType := HTML_CONTENT_TYPE;
result := HTTP_SUCCESS;
end;
Expand Down Expand Up @@ -813,7 +821,7 @@ procedure TAsyncWorld.OnRes(Statement: TSqlDBPostgresAsyncStatement;
Rtti.RegisterFromText([
TypeInfo(TMessageRec), 'message:PUtf8Char',
TypeInfo(TWorldRec), 'id,randomNumber:integer',
TypeInfo(TFortune), 'id:integer message:RawUtf8']);
TypeInfo(TFortune), 'id:integer message:PUtf8Char']);

// compute default execution context from HW information
cpuCount := CurrentCpuSet(cpuMask); // may run from a "taskset" command
Expand Down
2 changes: 1 addition & 1 deletion src/mormot.commit.inc
@@ -1 +1 @@
'2.2.7324'
'2.2.7325'

0 comments on commit 1a71fbf

Please sign in to comment.