Another idea might be to avoid making the costly Done check every time.
Something like this perhaps (untested)?

    // NewDoneChecker returns a DoneChecker value
    // that amortizes the cost of calling ctx.Done by only
    // calling ctx.Done every interval times that
    // DoneChecker.Done is called.
    func NewDoneChecker(ctx context.Context, interval int) *DoneChecker {
        return &DoneChecker{
            ctx:      ctx,
            interval: interval,
        }
    }

    type DoneChecker struct {
        ctx      context.Context
        interval int
        n        int
        done     bool
    }

    // Done reports whether the underlying context has reported
    // that it is done. It only does the check every so often.
    func (c *DoneChecker) Done() bool {
        c.n--
        if c.n > 0 || c.done {
            return c.done
        }
        select {
        case <-c.ctx.Done():
            c.done = true
            return true
        default:
            c.n = c.interval
            return false
        }
    }


On Thu, 23 May 2019 at 10:40, Max <massimiliano.ghila...@gmail.com> wrote:

> A bit less "funky" idea - I currently use it in my
> https://github.com/cosmos72/gomacro
> to interrupt the interpreter if the user hits Ctrl+C.
> Caveat: I don't know how difficult is to adapt it to GoAWK.
>
> The idea is conceptually simple: loop unrolling. The details are slightly
> tricky:
>
> unroll the interpreter main loop that executes statements by something
> like 20 times,
> and after the 20 iterations check *once* for externally-set flags:
> timeout, Ctrl+C, etc.
> The tricky part is: you also need to handle the case where you are
> somewhere in the middle
> of the 20 unrolled iterations but suddenly there are no more statements to
> execute.
>
> The skeleton code might look similar to
> https://github.com/cosmos72/gomacro/blob/master/fast/code.go#L169
> or https://github.com/cosmos72/gomacro/blob/master/fast/code.go#L204
>
> Regards,
> Cosmos72
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/d7484265-0a61-4c48-9e4e-0d6aaa3d447b%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/d7484265-0a61-4c48-9e4e-0d6aaa3d447b%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAJhgacjjBFaMVXskfkWo_%3DRBFcWe-_bqoxEguUNAhXT6Qtsa5Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to