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.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

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

    Much appreciated reply - I didn't even think of the [ @$_[1] ] construct - that should save a lot of clone() calls - I'll test asap.

    BTW, as way of explanation, the _atlayer sub basically 'layers' the attributes of the current "character" onto the already-existing attributes at the position. First arg to _atlayer is existing attributes, second arg is 'new' attributes. The clone/copy of the new atrributes is necessary because the 'new' attributes are stored as an array ref in the object - a la the current 'brush' in a Qt painter. I can't just modify the "brush" array ref - must copy first, else the master "brush" would be modified for all subsequent calls.

    The 'layering' mentioned basically says that if there are no attributes in the "brush" (e.g. the -1 or the undef), then apply attributes from the existing attribute ref, but if the brush has any attributes for that category (even an 'off' flag, such as for bold - which would make the final output have a DIM ansi code) the value sent in the second argument will take precedents over the existing attribute at that location.

    Whew..*takes a breath * ... sorry, complicated explanation for what is actually kinda simple. Does it make sense?