Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factorize assertions + Option "checks-mode=unsafe" to optimize runtime #2266

Open
Tracked by #2250
denis-migdal opened this issue Oct 8, 2023 · 3 comments
Open
Tracked by #2250

Comments

@denis-migdal
Copy link
Contributor

Hi,

In production, we sometime want something that runs fast. One solution is to remove parameters (type) checks performed at the start of functions. The rational being : we test it in dev mode, in prod mode there should be no bug (which is false ofc). If there are, let it explode, it's your fault. It is a little like what JS is doing when used without TS.

Therefore I suggest to use a checks-mode=unsafe option that'd remove all checks when building brython.js and brython_stdlib.js.
Checks could be identified by comments (e.g. for JS code) :

// @checks:start
check_str(old, "replace() argument 1 ")
    check_str(_new, "replace() argument 2 ")
    // Validate instance type of 'count'
    if(! _b_.isinstance(count, [_b_.int, _b_.float])){
        throw _b_.TypeError.$factory("'" + $B.class_name(count) +
            "' object cannot be interpreted as an integer")
    }else if(_b_.isinstance(count, _b_.float)){
        throw _b_.TypeError.$factory("integer argument expected, got float")
    }
// @checks:end

Cordially,

@denis-migdal

This comment was marked as outdated.

@denis-migdal
Copy link
Contributor Author

denis-migdal commented Oct 8, 2023

Okay, I'm an idiot : https://jsperf.app/yucaxu/2

Just do :

// by default :
assert = {
    isNumber: (i) => {if( ! Number.isInteger(i)) throw new Error('nok')}
};
// if check-mode = unsafe :
assert = {
    isNumber: (_i) => {}
};
  • Using a function will help factorizing your code making it :

    • a little faster (but not much).
    • more readable.
    • easier to maintain.
    • reduce your code size (so lesser download and parsing time).
  • Using an empty assert :

    • increase runtime execution (checking is 28% slower).
    • allows to be enabled/disabled during runtime.
    • still takes some code space (but less as this is always the same small function to call).
    • still takes some performances (strangely enough doing nothing is 37% slower in my tests - stupid browser runtime code optimizations xD).
    • could enable to remove such lines from the code through a command-line tool (but better not doing it - I think the gain wouldn't be that great, and that'd be a little troublesome).

@denis-migdal denis-migdal changed the title Option "checks-mode=unsafe" to optimize runtime Factorize assertions + Option "checks-mode=unsafe" to optimize runtime Oct 11, 2023
@denis-migdal
Copy link
Contributor Author

Update: disabling checks during production runtime is not a priority.
Though, factorizing has advantages for code maintainability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant