Click here to Skip to main content
16,016,140 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralAh the joys of late stage optimization Pin
honey the codewitch2hrs 58mins ago
mvahoney the codewitch2hrs 58mins ago 
Don't optimize until you have to generally.

But that can sometimes backfire, like when an optimization requires an architecture change.

To save memory and remain flexible, my SVG engine writes its SVGs out using callbacks with color runs and coordinates.

The exception is when

A) your canvas bounds are the same as the bound bitmap bounding box
B) the bitmap is RGBA8888 (32-bit pixels)

I just added an optimization to do direct writes in lieu of using callbacks. It greatly speeds up rendering - if you have the memory for it.

Despite being a totally different way of rendering, I exposed it through a relatively common interface, and automatically direct bind to targets that fulfill A and B:

C++
template<typename Destination> 
struct xdraw_canvas_binder {
    static gfx_result canvas(Destination& destination, const srect16& bounds, ::gfx::canvas* out_canvas) {
        ::gfx::canvas result((size16)bounds.dimensions());
        gfx_result gr = result.initialize();
        if(gr!=gfx_result::success) {
            return gr;
        }
        using st_t = xdraw_canvas_state<Destination>;
        st_t* st = (st_t*)malloc(sizeof(st_t));
        if(st==nullptr) {
            result.deinitialize();
            return gfx_result::out_of_memory;
        }
        st->dest = &destination;
        st->dim = (size16)bounds.dimensions();
        st->offset = bounds.point1();
        *out_canvas=gfx_move(result);
        out_canvas->on_write_callback(xdraw_canvas_write_callback<Destination>,st,::free);
        out_canvas->on_read_callback(xdraw_canvas_read_callback<Destination>,st,::free);
        return gfx_result::success;
    }
};
template<> 
struct xdraw_canvas_binder<bitmap<rgba_pixel<32>>>
{
    static gfx_result canvas(bitmap<rgba_pixel<32>>& destination, const srect16& bounds, ::gfx::canvas* out_canvas) {
        ::gfx::canvas result((size16)bounds.dimensions());
        gfx_result gr = result.initialize();
        if(gr!=gfx_result::success) {
            return gr;
        }
        using st_t = xdraw_canvas_state<bitmap<rgba_pixel<32>>>;
        st_t* st = (st_t*)malloc(sizeof(st_t));
        if(st==nullptr) {
            result.deinitialize();
            return gfx_result::out_of_memory;
        }
        st->dest = &destination;
        st->dim = (size16)bounds.dimensions();
        st->offset = bounds.point1();
        *out_canvas=::gfx::helpers::gfx_move(result);
        if(bounds==(srect16)destination.bounds()) {
            gr=out_canvas->direct_bitmap_rgba32(&destination);
            if(gr!=gfx_result::success) {
                return gr;
            }
        } else {
            out_canvas->on_write_callback(xdraw_canvas_write_callback<bitmap<rgba_pixel<32>>>,st,::free);
            out_canvas->on_read_callback(xdraw_canvas_read_callback<bitmap<rgba_pixel<32>>>,st,::free);
        }
        return gfx_result::success;
    }
};


Here I have a specialization for a general bind and one that binds directly if your pixel format is rgba_pixel<32>

I can then invoke to appropriate method with this lil guy:

C++
template<typename Destination>
static gfx_result canvas(Destination& destination, const srect16& bounds, ::gfx::canvas* out_canvas) {
    return xdraw_canvas_binder<Destination>::canvas(destination,bounds,out_canvas);
}


Because I did that, I don't need to change any code that uses ::gfx::draw::canvas<>

Radically different ways of writing, and template specializations to the rescue once again.

A little planning up front led me here, and paid for itself in spades.

Now I have transparent optimization without changing the surface area of my API, even hiding a radical departure in terms of what its draw target is (callbacks vs bitmap writes)

Anyway, as a general rule, I do an optimization pass during many of my design iterations (not version iterations, but process iterations) to make sure I'm not painting myself into a corner. When I do this I assume everything in a critical codepath will need to be optimized. Then I ask myself, "what would that look like and what impact would it have on the final architecture", and I design with that in mind, as I did here.

It's part knack, part luck, and part experience, but optimization shouldn't entirely be ignored during the design phase IMO. Don't optimize, but ask yourself what would optimization do to the design if it has to happen? and then mitigate that in the design.
Check out my IoT graphics library here:
https://honeythecodewitch.com/gfx
And my IoT UI/User Experience library here:
https://honeythecodewitch.com/uix

GeneralRe: Ah the joys of late stage optimization Pin
obermd53mins ago
obermd53mins ago 
GeneralRe: Ah the joys of late stage optimization Pin
honey the codewitch2mins ago
mvahoney the codewitch2mins ago 
GeneralOpening up the web to embedded (CSS post thoughts ctd) Pin
honey the codewitch5hrs 2mins ago
mvahoney the codewitch5hrs 2mins ago 
GeneralRe: Opening up the web to embedded (CSS post thoughts ctd) Pin
Gary Stachelski 20213hrs 24mins ago
Gary Stachelski 20213hrs 24mins ago 
GeneralRe: Opening up the web to embedded (CSS post thoughts ctd) Pin
honey the codewitch3hrs 21mins ago
mvahoney the codewitch3hrs 21mins ago 
GeneralRe: Opening up the web to embedded (CSS post thoughts ctd) Pin
Gary Stachelski 20212hrs 13mins ago
Gary Stachelski 20212hrs 13mins ago 
GeneralRe: Opening up the web to embedded (CSS post thoughts ctd) Pin
honey the codewitch2hrs 11mins ago
mvahoney the codewitch2hrs 11mins ago 
GeneralRe: Opening up the web to embedded (CSS post thoughts ctd) Pin
Gary Stachelski 20211 hr 52mins ago
Gary Stachelski 20211 hr 52mins ago 
NewsCSS is awesome. Pin
Jeremy Falcon5hrs 36mins ago
professionalJeremy Falcon5hrs 36mins ago 
GeneralRe: CSS is awesome. PinPopular
Richard Andrew x645hrs 9mins ago
professionalRichard Andrew x645hrs 9mins ago 
GeneralRe: CSS is awesome. Pin
Jeremy Falcon3hrs 8mins ago
professionalJeremy Falcon3hrs 8mins ago 
GeneralRe: CSS is awesome. Pin
jochance5hrs 7mins ago
jochance5hrs 7mins ago 
GeneralRe: CSS is awesome. Pin
Jeremy Falcon2hrs 53mins ago
professionalJeremy Falcon2hrs 53mins ago 
GeneralRe: CSS is awesome. PinPopular
PJ Arends4hrs 40mins ago
professionalPJ Arends4hrs 40mins ago 
GeneralRe: CSS is awesome. Pin
Nelek3hrs 14mins ago
protectorNelek3hrs 14mins ago 
GeneralRe: CSS is awesome. Pin
Jeremy Falcon2hrs 57mins ago
professionalJeremy Falcon2hrs 57mins ago 
GeneralRe: CSS is awesome. Pin
Jeremy Falcon2hrs 58mins ago
professionalJeremy Falcon2hrs 58mins ago 
GeneralRe: CSS is awesome. Pin
0x01AA2hrs 10mins ago
mve0x01AA2hrs 10mins ago 
GeneralRe: CSS is awesome. Pin
Jeremy Falcon1 hr 56mins ago
professionalJeremy Falcon1 hr 56mins ago 
GeneralVOIP robocalls suggestions Pin
Choroid6hrs 50mins ago
Choroid6hrs 50mins ago 
GeneralRe: VOIP robocalls suggestions Pin
Richard Andrew x645hrs 14mins ago
professionalRichard Andrew x645hrs 14mins ago 
GeneralRe: VOIP robocalls suggestions Pin
jochance5hrs 6mins ago
jochance5hrs 6mins ago 
GeneralPet peeve du jour Pin
Gary Wheeler10hrs 14mins ago
Gary Wheeler10hrs 14mins ago 
GeneralRe: Pet peeve du jour Pin
Richard Deeming9hrs 53mins ago
mveRichard Deeming9hrs 53mins ago 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.