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."

In reply to Re: Optimizing a Text Canvas: An inner loop and a cloner by BrowserUk
in thread Optimizing a Text Canvas: An inner loop and a cloner by JosiahBryan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.