-
Notifications
You must be signed in to change notification settings - Fork 274
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
base: master
Are you sure you want to change the base?
Changes from 10 commits
6e256e8
76f1cdb
1869723
6d4d63f
dc1e5be
5fda704
0e37865
6bb4823
41f86d6
2a08dac
d5f2354
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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, | ||
|
@@ -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) | ||
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)"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you test this with inheritance? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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")}):"); | ||
} | ||
|
||
|
There was a problem hiding this comment.
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.