Skip to content

Commit

Permalink
Limit appending of ShortStrings to Ropes to string length < 30
Browse files Browse the repository at this point in the history
The larger the size of ShortStrings we allow to be appended directly to
Ropes, the more memory Wattsi consumes at runtime.

We don’t need the size to be anywhere near as big as 255 — the existing
code never actually needs to directly append strings with lengths any
longer than 30. If we ever do run into need to append string lengths
longer than 30, we could at that time just bump UTF8InlineSize up to
whatever new size we actually need.

Upping the size to 30 from the old size of 15 seems to increase the
memory consumption by about 15–20%, or around 80–100MB. (In comparison,
upping it to 255 seems to roughly double the memory consumption.)
  • Loading branch information
sideshowbarker committed Aug 11, 2023
1 parent ccf648d commit 1ebe862
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/lib/ropes.pas
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ TFixedSizeRopeFragment = record
private
const
FragmentPayloadSize = SizeOf(TFixedSizeRopeFragment);
UTF8InlineSize = 255;
UTF8InlineSize = 30;
CodepointsSize = (FragmentPayloadSize-SizeOf(Byte)) div SizeOf(TUnicodeCodepoint);
type
TUTF8InlineIndex = 0..UTF8InlineSize-1;
TUTF8InlineIndexPlusOne = 0..UTF8InlineSize;
TCodepointsIndex = 0..CodepointsSize-1;
TCodepointsIndexPlusOne = 0..CodepointsSize;
TInlineString = String[UTF8InlineSize];
TInlineString = String[UTF8InlineSize + 1];
PRopeFragment = ^TRopeFragment;
TRopeFragment = record
Next: PRopeFragment;
Expand Down Expand Up @@ -963,6 +963,11 @@ procedure Rope.Append(const NewString: RopeInternals.TInlineString);
{$IFDEF VERBOSE} if (DebugNow) then Writeln('Ropes: Append(ShortString) on rope @', IntToHex(PtrUInt(@Self), 16), ' with data @', IntToHex(PtrUInt(FValue), 16)); {$ENDIF}
{$IFDEF VERBOSE} if (DebugNow) then Writeln('Ropes: Length(FValue)=', Length(FValue)); {$ENDIF}
Assert(Length(NewString) <= RopeInternals.UTF8InlineSize, 'Maximum size of short string is ' + IntToStr(RopeInternals.UTF8InlineSize));
if (Length(NewString) > RopeInternals.UTF8InlineSize) then
begin
Writeln('Error: Operation attempted with string length > UTF8InlineSize: "' + NewString + '..."');
Halt(1);
end;
if ((not Assigned(FLast)) or (FLast^.Kind <> rfUTF8Inline) or (RopeInternals.UTF8InlineSize - FLast^.InlineLength < Length(NewString))) then
begin
EnsureSize(1, 1);
Expand Down

0 comments on commit 1ebe862

Please sign in to comment.