Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Performance gap between [string equal] and [string compare]

Closed / Fixed3069593fffffffffffffffffffffffffffffffffCreated 16 years agoUpdated 16 years ago
Descriptionplain2010-09-17 22:54:49
The "streq" and "strcmp" instructions could benefit from a bit of unification (as suggested by a comment in tclExecute.c).

For example, [string compare] has very efficient special cases for ByteArray and String operands, that [string equal] lacks.
As a consequence, a naive test of equality on big byte arrays with [string equal] is ~7 times slower than the less obvious [string compare], despite the fact that the latter produces a more elaborate result (three-valued instead of binary):

Source this:

  set z [open /dev/zero r]
  fconfigure $z -translation binary
  set b1 [read $z 1000000]
  set b2 [read $z 1000000]
  set y 1

Then interactively:

  % time {string compare $b1 $b2} 1000
  301.596 microseconds per iteration
  % time {string compare $b1 $b2} 1000
  299.637 microseconds per iteration
  % time {string compare $b1 $b2} 1000
  298.203 microseconds per iteration
  % ::tcl::unsupported::representation  $b1
  value is a bytearray with a refcount of 2, object pointer at 0x9b55e50, internal representation 0xb69ae010:0x9b56fc0, no string representation.
  % ::tcl::unsupported::representation  $b2
  value is a bytearray with a refcount of 2, object pointer at 0x9b56e58, internal representation 0xb68ac010:0x9b55e50, no string representation.
  % time {string equal $b1 $b2} 1000
  2159.225 microseconds per iteration
  % time {string equal $b1 $b2} 1000
  2128.354 microseconds per iteration
  % time {string equal $b1 $b2} 1000
  2129.929 microseconds per iteration
  % ::tcl::unsupported::representation  $b1
  value is a bytearray with a refcount of 2, object pointer at 0x9b55e50, internal representation 0xb69ae010:0x9b56fc0, no string representation.
  % ::tcl::unsupported::representation  $b2
  value is a bytearray with a refcount of 2, object pointer at 0x9b56e58, internal representation 0xb68ac010:0x9b55e50, no string representation.
hobbsplain2010-09-22 08:26:30
memcmp used in INST_STR_(N)EQ now.  Note that better libc's were not effected by this disparity.  INST_STR_(N)EQ could be improved with obj-type-aware, but that's not critical.
hobbsplain2010-09-25 09:26:38
allow_comments - 1

HEAD has all variations condensed into one path.  Speedups for several cases.
Log in (anonymous is fine) to add a comment.