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

Optimize boolean short-circuiting #29

Open
gene-pavlovsky opened this issue Feb 23, 2019 · 0 comments
Open

Optimize boolean short-circuiting #29

gene-pavlovsky opened this issue Feb 23, 2019 · 0 comments

Comments

@gene-pavlovsky
Copy link
Member

Consider the following example:

  @async public function deleteUser(id:Int, user:UserSession):Noise {
    var authorized = user.isSuperUser || (user.isAdmin && @await isNotAdmin(id));
    if (!authorized) throw new Error(Unauthorized, "unauthorized");
    return db.User.delete({where: u -> u.id == id}).next(_ -> Noise);
  }

  function isNotAdmin(id:Int):Promise<Bool>
    return db.User.where(User.id == id).first()
      .next(u -> u.roles & (UserRole.ADMIN | UserRole.SUPERUSER) == 0);

If isNotAdmin would be a sync function, it would not be evaluated in case the current user is either a SuperUser (can delete any user), or a regular non-admin user (can't delete any user).
The way tink_await transforms this code, isNotAdmin(id) is eagerly evaluated, and only then the whole expression is evaluated.

Would it be possible to optimize this?

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