Skip to content

feat(Python): Support caching const variables #6134

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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions Source/DafnyCore/Backends/Python/PythonCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ private void EmitImports(string moduleName, string pythonModuleName, ConcreteSyn
wr.WriteLine("from typing import Callable, Any, TypeVar, NamedTuple");
wr.WriteLine("from math import floor");
wr.WriteLine("from itertools import count");
// If (cacheConstVariablesCliFlag):
wr.WriteLine("from functools import lru_cache");
wr.WriteLine();
// Don't emit `import module_` for generated modules in the DafnyRuntimePython.
// The DafnyRuntimePython doesn't have a module.py file, so the import isn't valid.
Expand Down Expand Up @@ -563,7 +565,7 @@ public ConcreteSyntaxTree CreateFunction(string name, List<TypeArgumentInstantia

public ConcreteSyntaxTree CreateGetter(string name, TopLevelDecl enclosingDecl, Type resultType, IOrigin tok,
bool isStatic, bool isConst, bool createBody, MemberDecl member, bool forBodyInheritance) {
return Compiler.CreateGetter(name, resultType, tok, isStatic, createBody, MethodWriter);
return Compiler.CreateGetter(name, resultType, tok, isStatic, isConst, createBody, MethodWriter);
}

public ConcreteSyntaxTree CreateGetterSetter(string name, Type resultType, IOrigin tok,
Expand Down Expand Up @@ -617,9 +619,17 @@ private ConcreteSyntaxTree CreateGetterSetter(string name, bool createBody, out
return null;
}

private ConcreteSyntaxTree CreateGetter(string name, Type resultType, IOrigin tok, bool isStatic, bool createBody, ConcreteSyntaxTree methodWriter) {
private ConcreteSyntaxTree CreateGetter(string name, Type resultType, IOrigin tok, bool isStatic, bool isConst, bool createBody, ConcreteSyntaxTree methodWriter) {
if (!createBody) { return null; }
methodWriter.WriteLine(isStatic ? $"@{DafnyRuntimeModule}.classproperty" : "@property");
// if (isConst and cacheConstVariablesCliFlag)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think always caching is ok, so let's remove the remains of the flag everywhere.

if (isConst) {
// maxsize's value represents the number of unique inputs to the function whose outputs are cached.
// Setting this to 1 means that only 1 input/output pair is cached.
// Since const methods only take `self` or `instance` and no other parameters, there is only 1 input/output pair.
// maxsize can be fixed to 1.
methodWriter.WriteLine("@lru_cache(maxsize=1)");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you test this with inheritance?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A child class accessing an inherited cached property results in a cache miss. It overwrites the cached value from the parent class.

}
return methodWriter.NewBlockPy(header: $"def {name}({(isStatic ? "instance" : "self")}):");
}

Expand Down
1 change: 1 addition & 0 deletions Source/DafnyRuntime/DafnyRuntimePython/System_/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Callable, Any, TypeVar, NamedTuple
from math import floor
from itertools import count
from functools import lru_cache

import _dafny as _dafny

Expand Down
Loading