Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f057d5c

Browse files
committed
Python: Speed up MRO calculation a bit.
1 parent 685826e commit f057d5c

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

  • python/ql/src/semmle/python/pointsto

python/ql/src/semmle/python/pointsto/MRO.qll

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ cached private newtype TClassList = Empty()
3333

3434
/* Keep ClassList finite and as small as possible */
3535
private predicate required_cons(ClassObjectInternal head, ClassList tail) {
36+
tail = Mro::newStyleMro(sole_base(head))
37+
or
3638
tail = merge_of_linearization_of_bases(head)
3739
or
3840
exists(ClassObjectInternal cls, int n |
@@ -61,6 +63,11 @@ private predicate required_cons(ClassObjectInternal head, ClassList tail) {
6163
tail = list_old_style_base_mros(head).flatten()
6264
}
6365

66+
private ClassObjectInternal sole_base(ClassObjectInternal cls) {
67+
Types::base_count(cls) = 1 and
68+
result = Types::getBase(cls, 0)
69+
}
70+
6471
/** A list of classes, used to represent the MRO of a class */
6572
class ClassList extends TClassList {
6673

@@ -413,7 +420,7 @@ private ClassListList list_of_linearization_of_bases_plus_bases(ClassObjectInter
413420
}
414421

415422
private ClassListList list_of_linearization_of_bases_plus_bases(ClassObjectInternal cls, int n) {
416-
result = ConsList(bases(cls), EmptyList()) and n = Types::base_count(cls)
423+
result = ConsList(bases(cls), EmptyList()) and n = Types::base_count(cls) and n > 1
417424
or
418425
exists(ClassListList partial |
419426
partial = list_of_linearization_of_bases_plus_bases(cls, n+1) and
@@ -476,6 +483,8 @@ module Mro {
476483
cls = ObjectInternal::builtin("object") and result = Cons(cls, Empty())
477484
or
478485
result = Cons(cls, merge_of_linearization_of_bases(cls))
486+
or
487+
result = Cons(cls, newStyleMro(sole_base(cls)))
479488
}
480489

481490
cached ClassList oldStyleMro(ClassObjectInternal cls) {

0 commit comments

Comments
 (0)