Skip to content

Optimize boolean short-circuiting #29

@gene-pavlovsky

Description

@gene-pavlovsky

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions