很多人不明白为什么要用TStringBuilder, Delphi中有string,有几乎所有TStringBuilder的功能函数, System.SysUtils中还有TStringHelper方便扩展String,更方便String的使用。
从Delphi提供的源码看,TStringBuilder与string最大的区别是,TStringBuilder是可以预先申请了内存空间,并限制最大内存空间,不会自动释放,而string是用多少申请多少内存,自动释放。
1.在开发服务器软件时,使用TStringBuilder池,可以有效地防止内存碎片化.
2.由于不需要开辟新的存储空间存储更大的字符串,预先申请内存的TStringBuilder的运行效率将超越string.
下面的代码中TServerString继承自TStringBuilder,内存空间限制大小为MaxMemorySize,, 当准备写入的字符将要超过内存大小时,将自动抛出异常,
unit Iocp.ServerString;
interface
uses SysUtils, Classes;
type
TServerStringPool=class;
TServerString=class(TStringBuilder)
protected
class var FPool:TServerStringPool;
constructor Create;
public
destructor Destroy;override;
end;
TServerStringPool=class
protected
FList:TThreadList;
public
constructor Create(Capacity:Integer);
destructor Destroy;override;
function GetString:TServerString;
procedure FreeString(Value:TServerString);
end;
implementation
const
MaxMemorySize=16384;
{ TServerString }
constructor TServerString.Create;
var
N:Integer;
begin
N:=(MaxMemorySize-InstanceSize) div SizeOf(Char);
inherited Create(N, N);
end;
destructor TServerString.Destroy;
begin
FPool.FreeString(Self);
end;
{ TServerStringPool }
constructor TServerStringPool.Create(Capacity: Integer);
var
i: Integer;
L:TList;
begin
inherited Create;
L:=FList.LockList;
try
for i := 1 to Capacity do
begin
L.Add(TServerString.Create);
end;
finally
FList.UnlockList;
end;
end;
destructor TServerStringPool.Destroy;
var
i: Integer;
L:TList;
begin
L:=FList.LockList;
try
for i := L.Count-1 to 0 do
TStringBuilder(L[i]).Free;
finally
FList.UnLockList;
end;
FList.Free;
inherited;
end;
procedure TServerStringPool.FreeString(Value: TServerString);
begin
FList.Add(Value);
end;
function TServerStringPool.GetString: TServerString;
var
L:TList;
begin
L:=FList.LockList;
try
if L.Count=0 then
begin
Result:=TServerString.Create;
end
else
begin
Result:=TServerString(L[L.Count-1]);
L.Delete(L.Count-1);
end;
finally
FList.UnlockList;
end;
end;
initialization
TServerString.FPool:=TServerStringPool.Create(10240);
finalization
TServerString.FPool.Free;
end.