Wednesday, February 10, 2010

String Buffer Performance

Benchmarks for the yesterday's post.

 
program project1;
 
{$mode objfpc}{$H+}
 
uses
  Classes, DateUtils, SysUtils, StrBuf;
 
type
  TBench = procedure(const PartSize, MaxSize: Integer);
 
procedure BuildString(const PartSize, MaxSize: Integer);
var
  Buf: String;
  BufSize: Integer;
begin
  Buf := '';
  BufSize := 0;
  while BufSize < MaxSize do begin
    Buf += StringOfChar(' ', PartSize);
    BufSize += PartSize;
  end;
end;
 
procedure BuildStringList(const PartSize, MaxSize: Integer);
var
  Buf: TStringList;
  BufSize: Integer;
begin
  Buf := TStringList.Create;
  BufSize := 0;
  while BufSize < MaxSize do begin
    Buf.Add(StringOfChar(' ', PartSize));
    BufSize += PartSize;
  end;
  Buf.Free;
end;
 
procedure BuildStrBuf(const PartSize, MaxSize: Integer);
var
  Buf: TStrBuf;
  BufSize: Integer;
begin
  BufSize := 0;
  while BufSize < MaxSize do begin
    Buf.W(StringOfChar(' ', PartSize));
    BufSize += PartSize;
  end;
end;
 
procedure Build(const PartSize, MaxSize: Integer; Bench: TBench; const Name: String);
const RUN = 5;
var
  T0: TDateTime;
  Msec: Int64;
  I: Integer;
begin
  T0 := Now;
  for I := 1 to RUN do
    Bench(PartSize, MaxSize);
  MSec := MilliSecondsBetween(Now, T0);
  Writeln(Format('partsize: %.5d T: %.7dms %s ', [PartSize, MSec, Name]));
end;
 
const
  UBOUND_PART = 100;
  UBOUND_BUF = 1000000;
var
  PartSize: Integer;
begin
  PartSize := 1;
  repeat
    Build(PartSize, UBOUND_BUF, @BuildString, 'String');
    Build(PartSize, UBOUND_BUF, @BuildStringList, 'StringList');
    Build(PartSize, UBOUND_BUF, @BuildStrBuf, 'StrBuf');
    PartSize := ((3 * PartSize) + 1) div 2;
  until PartSize > UBOUND_PART;
end.

$ fpc -B ./project1 && ./project1 | sort
Free Pascal Compiler version 2.5.1 [2010/02/07] for x86_64
Copyright (c) 1993-2009 by Florian Klaempfl
Target OS: Linux for x86-64
Compiling ./project1.pas
Compiling strbuf.pas
Linking project1
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
189 lines compiled, 0.2 sec 
partsize: 00001 T: 0000446ms StrBuf 
partsize: 00001 T: 0000602ms String 
partsize: 00001 T: 0001150ms StringList 
partsize: 00002 T: 0000233ms StrBuf 
partsize: 00002 T: 0000261ms String 
partsize: 00002 T: 0000534ms StringList 
partsize: 00003 T: 0000159ms StrBuf 
partsize: 00003 T: 0000190ms String 
partsize: 00003 T: 0000379ms StringList 
partsize: 00005 T: 0000101ms StrBuf 
partsize: 00005 T: 0000127ms String 
partsize: 00005 T: 0000211ms StringList 
partsize: 00008 T: 0000076ms StrBuf 
partsize: 00008 T: 0000086ms String 
partsize: 00008 T: 0000167ms StringList 
partsize: 00012 T: 0000050ms StrBuf 
partsize: 00012 T: 0000060ms String 
partsize: 00012 T: 0000100ms StringList 
partsize: 00018 T: 0000038ms StrBuf 
partsize: 00018 T: 0000043ms String 
partsize: 00018 T: 0000069ms StringList 
partsize: 00027 T: 0000030ms StrBuf 
partsize: 00027 T: 0000033ms String 
partsize: 00027 T: 0000045ms StringList 
partsize: 00041 T: 0000023ms String 
partsize: 00041 T: 0000024ms StrBuf 
partsize: 00041 T: 0000034ms StringList 
partsize: 00062 T: 0000015ms StrBuf 
partsize: 00062 T: 0000020ms String 
partsize: 00062 T: 0000020ms StringList 
partsize: 00093 T: 0000014ms String 
partsize: 00093 T: 0000015ms StringList 
partsize: 00093 T: 0000017ms StrBuf 
$

StrBuf's performance looks good or better compared to TStringList and/or String concatenating while you're appending short pieces of text, say up to few dozens of characters and that's what it was intended and written for.

Tuesday, February 9, 2010

Simple String Buffer

 
unit StrBuf;
 
{$mode objfpc}{$H+}
 
{ Simple string buffer
 
  No setup/teardown required, memory mngmt, initialization, and finalization
  is done by FPC itself.
 
  Copyright (C) 2010 bflm, contact: befelemepeseveze at Google's free mail
 
  This library is free software; you can redistribute it and/or modify it
  under the terms of the GNU Library General Public License as published by
  the Free Software Foundation; either version 2 of the License, or (at your
  option) any later version with the following modification:
 
  As a special exception, the copyright holders of this library give you
  permission to link this library with independent modules to produce an
  executable, regardless of the license terms of these independent modules,and
  to copy and distribute the resulting executable under terms of your choice,
  provided that you also meet, for each linked independent module, the terms
  and conditions of the license of that module. An independent module is a
  module which is not derived from or based on this library. If you modify
  this library, you may extend this exception to your version of the library,
  but you are not obligated to do so. If you do not wish to do so, delete this
  exception statement from your version.
 
  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  for more details.
 
  You should have received a copy of the GNU Library General Public License
  along with this library; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
 
interface
 
uses
  SysUtils;
 
type
 
  { TStrBuf }
 
  TStrBuf = object
  private
    FLen: Integer;
    FS: String;
    function GetS: String;
  public
    procedure W(const S: String);
    procedure W(const Fmt: String; Args: array of const);
    property S: String read GetS;
  end;
 
operator +(var Buf: TStrBuf; const S: String): TStrBuf;
operator :=(const S: String): TStrBuf;
operator :=(var Buf: TStrBuf): String;
 
implementation
 
{ TStrBuf }
 
function TStrBuf.GetS: String;
begin
  if FS = '' then
    FLen := 0;
  Result := LeftStr(FS, FLen);
end;
 
procedure TStrBuf.W(const S: String);
var Len, Len1: Integer;
begin
  Len := Length(S);
  if Len = 0 then
    Exit;
  if FS = '' then
    FLen := 0;
  Len1 := FLen + Len;
  if Len1 > Length(FS) then
    SetLength(FS, 2 * Len1);
  Move(S[1], FS[FLen + 1], Len);
  FLen += Len;
end;
 
procedure TStrBuf.W(const Fmt: String; Args: array of const);
begin
  W(Format(Fmt, Args));
end;
 
operator +(var Buf: TStrBuf; const S: String): TStrBuf;
begin
  Result.FS := Buf.FS;
  Result.FLen := Buf.FLen;
  Result.W(S);
end;
 
operator :=(const S: String): TStrBuf;
begin
  Result.FLen := Length(S);
  Result.FS := S;
end;
 
operator :=(var Buf: TStrBuf): String;
begin
  Result := Buf.S;
end;
 
end.
 

 
program project1;
 
{$mode objfpc}{$H+}
 
// Simple string buffer example
 
uses
  heaptrc, StrBuf;
 
function F(N: Integer): String;
var
  Buf: TStrBuf;
  I: Integer;
begin
  for I := 1 to N do
    Buf.W('line %d%s', [I, sLineBreak]);
  Result := Buf.S;
end;
 
var
  Buf, Buf2: TStrBuf;
 
begin
  Buf.W('abc');
  Buf.W('%d', [123]);
  Buf += 'xyz';
  Writeln(Buf.S);
  Writeln(String(Buf));
  Buf := '456';
  Writeln(Buf.S);
  Buf2 := Buf + ' bar';
  Buf += ' foo';
  Writeln(Buf.S);
  Writeln(Buf2.S);
  Write(F(10));
end.

$ fpc -B project1.pas && ./project1 
Free Pascal Compiler version 2.5.1 [2010/02/07] for x86_64
Copyright (c) 1993-2009 by Florian Klaempfl
Target OS: Linux for x86-64
Compiling project1.pas
Compiling strbuf.pas
Linking project1
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
148 lines compiled, 0.2 sec 
abc123xyz
abc123xyz
456
456 foo
456 bar
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
Heap dump by heaptrc unit
71 memory blocks allocated : 2926/3200
71 memory blocks freed     : 2926/3200
0 unfreed memory blocks : 0
True heap size : 360448
True free heap : 360448
$

Thursday, November 5, 2009

Multicore Compiler Make

Tested today Jonas Maebe's multicore FPC build tip from the Recompiling new changes thread at the Free Pascal Community.
$ time ( cd ~/svn/fpc/trunk && make clean && make all OPT="-gl" && make install INSTALL_PREFIX=~ )
...
real 3m15.641s
user 2m16.350s
sys  0m28.710s
$ 

# vs

$ time ( cd ~/svn/fpc/trunk && make -j 2 clean && make -j 2 all OPT="-gl" && make -j 2 install INSTALL_PREFIX=~ )
...
real 2m44.049s
user 2m18.530s
sys  0m27.990s
$ 
Both runs were "hot cache" on a dual core PC.

Friday, October 2, 2009

WebKit/Gtk Experimenting

Tried to play a bit with WebKit/Gtk using the current Free Pascal (SVN) version. Proof of concept on Linux (only) is below. Don't expect the program to run and/or compile on Windows without modifications.
 
program GtkLauncher;
 
{$mode objfpc}{$H+}
 
{ This is a FPC port/modification of the original source from:
  http://trac.webkit.org/browser/trunk/WebKitTools/GtkLauncher/main.c
 
  - Tested on Linux only.
  - Depends on the libwebkit[-dev] package (which is not installed by default
    at least on Ubuntu).
  - You must include the protocol specifier when entering an uri in the address
    edit box, eg. 'http://google.com' and not just 'google.com' as you can
    in most real browsers.
 
/*
 * Copyright (C) 2006, 2007 Apple Inc.
 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
}
 
uses
  gtk2, glib2, math;
 
const
  LIB = 'webkit-1.0';
 
type
  WebKitWebView = record
    parent_instance: TGtkContainer;
    priv: pointer;
  end;
  PWebKitWebView = ^WebKitWebView;
 
  WebKitWebFrame = record
    parent_instance: PGObject;
    priv: pointer;
  end;
  PWebKitWebFrame = ^WebKitWebFrame;
 
var
  load_progress: gint = 0;
  main_statusbar: PGtkStatusbar = nil;
  main_title: pgchar = nil;
  main_window: PGtkWidget = nil;
  status_context_id: guint = 0;
  uri_entry: PGtkWidget = nil;
  web_view: PWebKitWebView = nil;
 
function web_view_new: PGtkWidget; cdecl; external LIB name 'webkit_web_view_new';
procedure web_view_go_back(web_view: PWebKitWebView); cdecl; external LIB name 'webkit_web_view_go_back';
procedure web_view_go_forward(web_view: PWebKitWebView); cdecl; external LIB name 'webkit_web_view_go_forward';
procedure web_view_open(web_view: PWebKitWebView; uri: Pgchar); cdecl; external LIB name 'webkit_web_view_open';
 
procedure activate_uri_entry_cb(entry: PGtkWidget; data: gpointer); cdecl;
var uri: Pgchar;
begin
  uri := gtk_entry_get_text(GTK_ENTRY(entry));
  assert(assigned(uri));
  web_view_open(web_view, uri);
end;
 
procedure update_title (window: PGtkWindow);
var
  string_: PGString;
  title: Pgchar;
begin
  string_ := g_string_new(main_title);
  g_string_append(string_, ' - FPC WebKit/Gtk Launcher');
  if load_progress < 100 then
    g_string_append_printf(string_, ' (%d%%)', load_progress);
  title := g_string_free(string_, false);
  gtk_window_set_title(window, title);
  g_free(title);
end;
 
procedure link_hover_cb(page: PWebKitWebView; const title: pgchar; const link: pgchar; data: gpointer); cdecl;
begin
  gtk_statusbar_pop(main_statusbar, status_context_id);
  if assigned(link) then
    gtk_statusbar_push(main_statusbar, status_context_id, link);
end;
 
procedure title_change_cb(web_view: PWebKitWebView; web_frame: PWebKitWebFrame; const title: pgchar; data: gpointer); cdecl;
begin
  if assigned(main_title) then
    g_free(main_title);
  main_title := g_strdup(title);
  update_title(GTK_WINDOW(main_window));
end;
 
procedure progress_change_cb(page: PWebKitWebView; progress: gint; data: gpointer); cdecl;
begin
  load_progress := progress;
  update_title(GTK_WINDOW(main_window));
end;
 
procedure destroy_cb(widget: pGtkWidget; data: gpointer); cdecl;
begin
  gtk_main_quit;
end;
 
procedure go_back_cb(widget: PGtkWidget; data: gpointer); cdecl;
begin
  web_view_go_back(web_view);
end;
 
procedure go_forward_cb(widget: PGtkWidget; data: gpointer); cdecl;
begin
  web_view_go_forward(web_view);
end;
 
function create_browser: PGtkWidget;
begin
  result := gtk_scrolled_window_new(nil, nil);
  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(result), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
  web_view := PWebKitWebView(web_view_new);
  gtk_container_add(GTK_CONTAINER(result), GTK_WIDGET(web_view));
  g_signal_connect(web_view, 'title-changed', G_CALLBACK(@title_change_cb), web_view);
  g_signal_connect(web_view, 'load-progress-changed', G_CALLBACK(@progress_change_cb), web_view);
  g_signal_connect(web_view, 'hovering-over-link', G_CALLBACK(@link_hover_cb), web_view);
end;
 
function create_statusbar: PGtkWidget;
begin
  main_statusbar := GTK_STATUSBAR(gtk_statusbar_new);
  status_context_id := gtk_statusbar_get_context_id(main_statusbar, 'Link Hover');
  result := PGtkWidget(main_statusbar);
end;
 
function create_toolbar: PGtkWidget;
var item: PGtkToolItem;
begin
  result := gtk_toolbar_new;
  gtk_toolbar_set_orientation(GTK_TOOLBAR(result), GTK_ORIENTATION_HORIZONTAL);
  gtk_toolbar_set_style(GTK_TOOLBAR(result), GTK_TOOLBAR_BOTH_HORIZ);
  item := gtk_tool_button_new_from_stock(GTK_STOCK_GO_BACK);
  g_signal_connect(G_OBJECT(item), 'clicked', G_CALLBACK(@go_back_cb), nil);
  gtk_toolbar_insert(GTK_TOOLBAR(result), item, -1);
  item := gtk_tool_button_new_from_stock(GTK_STOCK_GO_FORWARD);
  g_signal_connect(G_OBJECT(item), 'clicked', G_CALLBACK(@go_forward_cb), nil);
  gtk_toolbar_insert(GTK_TOOLBAR(result), item, -1);
  item := gtk_tool_item_new;
  gtk_tool_item_set_expand(item, true);
  uri_entry := gtk_entry_new;
  gtk_container_add(GTK_CONTAINER(item), uri_entry);
  g_signal_connect(G_OBJECT(uri_entry), 'activate', G_CALLBACK(@activate_uri_entry_cb), nil);
  gtk_toolbar_insert(GTK_TOOLBAR(result), item, -1);
  item := gtk_tool_button_new_from_stock(GTK_STOCK_OK);
  g_signal_connect_swapped(G_OBJECT(item), 'clicked', G_CALLBACK(@activate_uri_entry_cb), gpointer(uri_entry));
  gtk_toolbar_insert(GTK_TOOLBAR(result), item, -1);
end;
 
function create_window: PGtkWidget;
begin
  result := gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size(PGtkWindow(result), 800, 600);
  gtk_widget_set_name(result, 'FPC GtkLauncher');
  g_signal_connect(result, 'destroy', G_CALLBACK(@destroy_cb), nil);
end;
 
var vbox: PGtkWidget;
 
begin
  math.SetExceptionMask([exInvalidOp, exPrecision]); // strange magic, removes webkit's SIGFPE
  gtk_init(@argc,@argv);
  vbox := gtk_vbox_new(false, 0);
  gtk_box_pack_start(PGtkBox(vbox), create_toolbar, false, false, 0);
  gtk_box_pack_start(PGtkBox(vbox), create_browser, true, true, 0);
  gtk_box_pack_start(PGtkBox(vbox), create_statusbar, false, false, 0);
  main_window := create_window;
  gtk_container_add(PGtkContainer(main_window), vbox);
  web_view_open(web_view, 'http://www.freepascal.org');
  gtk_widget_grab_focus(GTK_WIDGET(web_view));
  gtk_widget_show_all(main_window);
  gtk_main;
end.
 

You should get something like this at runtime: