in reply to Optimizing a Text Canvas: An inner loop and a cloner
I think the best way to optimise your application would involve a substantial redesign from what you currently have.
The subs/loops you are seeking to speed up deal are looping over arrays of attributes. Those attributes could be more efficiently stored using bytes (or maybe just bits) in a string. Besides saving some space (A scalar of 5 bytes requires ~17 bytes of ram, versus an array holding 5 scalars needing something like 400), and being more efficient to duplicate, some or all of the loops you are currently using comparing, copying and 'defaulting' attributes maybe replacable by boolean operations.
For example, if your attributes were stored as a string: "FBubk" where F/B are the foreground and background colors; u/o/k are the underline bold and blink attributes; then this
push @a, _attr($atr) if #!_ateq($lat,$atr); $lat->[AS_BG] ne $atr->[AS_BG] || $lat->[AS_FG] ne $atr->[AS_FG] || $lat->[AS_U] != $atr->[AS_U] || $lat->[AS_BOLD] != $atr->[AS_BOLD] || $lat->[AS_BLINK] != $atr->[AS_BLINK];
Could become
push @a, $atr if $lat ne $atr;
This local $_ = clone($_[1]); would simply be local $_ = $_[1]; avoiding a function call.
Even with your current setup, it's not clear to me why you need to use clone() to replicate and array? A simple local $_ = [ @$_[1] ]; would work just as well and (I think) be more efficient.
It's also possible (but I haven't fully digested your code so I might be wrong), that by using strings instead of arrays, that:
sub _atlayer { #$_[0] = shift; local $_ = clone($_[1]); #$_->{$_} = $_[1]->{$_} foreach keys %{$_[1]}; $_->[AS_BG] = $_[0]->[AS_BG] if !$_->[AS_BG]; $_->[AS_FG] = $_[0]->[AS_FG] if !$_->[AS_FG]; $_->[AS_U] = $_[0]->[AS_U] if $_->[AS_U] < 0; $_->[AS_BOLD] = $_[0]->[AS_BOLD] if $_->[AS_BOLD] < 0; $_->[AS_BLINK] = $_[0]->[AS_BLINK] if $_->[AS_BLINK] < 0; return $_; }
might become something like
sub _atlayer { #$_[0] = shift; local $_ = $_[1]; $_ &= $_[ 0 ]; return $_; }
Though you might have to tweak how you represent the absence of attributes.
Given your current setup, that sub might be slightly more efficient coded as
sub _atlayer { local $_ = [ @$_[1] ]; $_->[AS_BG] ||= $_[0]->[AS_BG]; $_->[AS_FG] ||= $_[0]->[AS_FG]; $_->[AS_U] ||= $_[0]->[AS_U]; $_->[AS_BOLD] ||= $_[0]->[AS_BOLD]; $_->[AS_BLINK] ||= $_[0]->[AS_BLINK]; return $_; }
if you could represent the absence of underline, bold and blink as 0 or undef rather than a negative number (-1?).
From the bits commented out in your code, it looks like you already made the transition from using hashes to arrays to store your attributes. I think that moving to use strings would be even better. Not PC perhaps, but certainly more efficient.
Sorry this is all ifs and maybes. I'm not able to run your code here.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Optimizing a Text Canvas: An inner loop and a cloner
by JosiahBryan (Novice) on Jul 06, 2007 at 16:00 UTC |