Skip to content

Commit 74b865c

Browse files
authored
Merge pull request #696 from zickgraf/caches
Fix bug in caching mechanism and add Object/MorphismConstructor to non-cached operations
2 parents a57615f + dcc0ec8 commit 74b865c

File tree

8 files changed

+65
-35
lines changed

8 files changed

+65
-35
lines changed

CAP/PackageInfo.g

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Version := Maximum( [
2121
## this line prevents merge conflicts
2222
"2020.04-16", ## Sepp's version
2323
## this line prevents merge conflicts
24-
"2021.07-01", ## Fabian's version
24+
"2021.07-02", ## Fabian's version
2525
## this line prevents merge conflicts
2626
"2021.05-04", ## Kamal's version
2727
] ),

CAP/gap/CAP.gi

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,40 @@ InstallTrueMethod( IsPreAbelianCategory, IsAbelianCategory );
2727

2828
##
2929
InstallValue( CAP_INTERNAL,
30-
rec(
31-
name_counter := 0,
32-
default_cache_type := "weak",
30+
rec(
31+
name_counter := 0,
32+
default_cache_type := "weak",
33+
operation_names_with_cache_disabled_by_default := [
34+
# the following operations are needed for comparison in caches
35+
"IsEqualForObjects",
36+
"IsEqualForMorphisms",
37+
"IsEqualForMorphismsOnMor",
38+
"IsEqualForCacheForObjects",
39+
"IsEqualForCacheForMorphisms",
40+
# it is unclear how `IsEqualForCacheForObjects` and `IsEqualForCacheForMorphisms`
41+
# would behave on non-well-defined objects/morphisms, so exclude `IsWellDefined*`
42+
"IsWellDefinedForObjects",
43+
"IsWellDefinedForMorphisms",
44+
"IsWellDefinedForTwoCells",
45+
# do not cache operations returning random data
46+
"RandomObjectByInteger",
47+
"RandomMorphismByInteger",
48+
"RandomMorphismWithFixedSourceByInteger",
49+
"RandomMorphismWithFixedRangeByInteger",
50+
"RandomMorphismWithFixedSourceAndRangeByInteger",
51+
"RandomObjectByList",
52+
"RandomMorphismByList",
53+
"RandomMorphismWithFixedSourceByList",
54+
"RandomMorphismWithFixedRangeByList",
55+
"RandomMorphismWithFixedSourceAndRangeByList",
56+
# by default, do not cache constructors and object/morphism data
57+
# because in general these operations are cheap,
58+
# so caching would not improve the performance
59+
"ObjectConstructor",
60+
"ObjectDatum",
61+
"MorphismConstructor",
62+
"MorphismDatum",
63+
],
3364
)
3465
);
3566

@@ -80,7 +111,7 @@ InstallGlobalFunction( GET_METHOD_CACHE,
80111
cache := CreateWeakCachingObject( number );
81112
elif cache_type = "crisp" then
82113
cache := CreateCrispCachingObject( number );
83-
elif cache_type = "none" or cache_type = "never" then
114+
elif cache_type = "none" then
84115
cache := CreateCrispCachingObject( number );
85116
DeactivateCachingObject( cache );
86117
else
@@ -184,7 +215,7 @@ end );
184215
InstallGlobalFunction( "CREATE_CAP_CATEGORY_OBJECT",
185216

186217
function( obj_rec, attr_list )
187-
local i, flatted_attribute_list, obj;
218+
local i, flatted_attribute_list, obj, operation_name;
188219

189220
for i in [ 1 .. Length( attr_list ) ] do
190221

@@ -215,30 +246,13 @@ InstallGlobalFunction( "CREATE_CAP_CATEGORY_OBJECT",
215246

216247
obj!.derivations_weight_list := MakeOperationWeightList( obj, CAP_INTERNAL_DERIVATION_GRAPH );
217248

218-
obj!.caches := rec( IsEqualForObjects := "never",
219-
IsEqualForMorphisms := "never",
220-
IsEqualForMorphismsOnMor := "never",
221-
IsEqualForCacheForObjects := "never",
222-
IsEqualForCacheForMorphisms := "never",
223-
IsWellDefinedForObjects := "never",
224-
IsWellDefinedForMorphisms := "never",
225-
IsWellDefinedForTwoCells := "never",
226-
RandomObjectByInteger := "never",
227-
RandomMorphismByInteger := "never",
228-
RandomMorphismWithFixedSourceByInteger := "never",
229-
RandomMorphismWithFixedRangeByInteger := "never",
230-
RandomMorphismWithFixedSourceAndRangeByInteger := "never",
231-
RandomObjectByList := "never",
232-
RandomMorphismByList := "never",
233-
RandomMorphismWithFixedSourceByList := "never",
234-
RandomMorphismWithFixedRangeByList := "never",
235-
RandomMorphismWithFixedSourceAndRangeByList := "never",
236-
# object and morphism data must never be cashed
237-
# because `IsEqualForCache*` might involve them,
238-
# possibly leading to an infinite recursion
239-
ObjectDatum := "never",
240-
MorphismDatum := "never",
241-
);
249+
obj!.caches := rec( );
250+
251+
for operation_name in CAP_INTERNAL.operation_names_with_cache_disabled_by_default do
252+
253+
obj!.caches.(operation_name) := "none";
254+
255+
od;
242256

243257
obj!.redirects := rec( );
244258

@@ -385,8 +399,7 @@ InstallMethod( SetCaching,
385399

386400
fi;
387401

388-
if not IsBound( category!.caches.( function_name ) ) or
389-
( IsString( category!.caches.( function_name ) ) and category!.caches.( function_name ) <> "never" ) then
402+
if not IsBound( category!.caches.( function_name ) ) or IsString( category!.caches.( function_name ) ) then
390403

391404
category!.caches.( function_name ) := caching_info;
392405

@@ -465,8 +478,8 @@ InstallGlobalFunction( SetCachingOfCategory,
465478

466479
for current_name in RecNames( category!.caches ) do
467480

468-
if current_name in [ "IsEqualForMorphisms", "IsEqualForObjects", "IsEqualForMorphismsOnMor", "IsEqualForCacheForMorphisms", "IsEqualForCacheForObjects" ] then
469-
continue; ## Those are needed for comparison in caches
481+
if current_name in CAP_INTERNAL.operation_names_with_cache_disabled_by_default then
482+
continue;
470483
fi;
471484

472485
SetCaching( category, current_name, type );

CAP/gap/CategoryMorphisms.gd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ DeclareGlobalVariable( "PROPAGATION_LIST_FOR_EQUAL_MORPHISMS" );
6767
#! The arguments are two objects $S$ and $T$ in a category,
6868
#! and a morphism datum $a$ (type and semantics of the morphism datum depend on the category).
6969
#! The output is a morphism in $\mathrm{Hom}(S,T)$ defined by $a$.
70+
#! Note that by default this CAP operation is not cached. You can change this behaviour
71+
#! by calling `SetCachingToWeak( C, "MorphismConstructor" )` resp. `SetCachingToCrisp( C, "MorphismConstructor" )`.
7072
#! @Returns a morphism in $\mathrm{Hom}(S,T)$
7173
#! @Arguments S, a, T
7274
DeclareOperation( "MorphismConstructor",
@@ -95,6 +97,8 @@ DeclareOperation( "AddMorphismConstructor",
9597
#! The argument is a CAP category morphism <A>mor</A>.
9698
#! The output is a datum which can be used to construct <A>mor</A>, that is,
9799
#! `IsEqualForMorphisms( `<A>mor</A>`, MorphismConstructor( Source( `<A>mor</A>` ), MorphismDatum( `<A>mor</A>` ), Range( `<A>mor</A>` ) ) )`.
100+
#! Note that by default this CAP operation is not cached. You can change this behaviour
101+
#! by calling `SetCachingToWeak( C, "MorphismDatum" )` resp. `SetCachingToCrisp( C, "MorphismDatum" )`.
98102
#! @Returns depends on the category
99103
#! @Arguments mor
100104
DeclareAttribute( "MorphismDatum",

CAP/gap/CategoryObjects.gd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,8 @@ DeclareGlobalFunction( "ObjectifyObjectForCAPWithAttributes" );
399399
#! The arguments are a category $C$ and an object datum $a$
400400
#! (type and semantics of the object datum depend on the category).
401401
#! The output is an object of $C$ defined by $a$.
402+
#! Note that by default this CAP operation is not cached. You can change this behaviour
403+
#! by calling `SetCachingToWeak( C, "ObjectConstructor" )` resp. `SetCachingToCrisp( C, "ObjectConstructor" )`.
402404
#! @Returns an object
403405
#! @Arguments C, a
404406
DeclareOperation( "ObjectConstructor",
@@ -427,6 +429,8 @@ DeclareOperation( "AddObjectConstructor",
427429
#! The argument is a CAP category object <A>obj</A>.
428430
#! The output is a datum which can be used to construct <A>obj</A>, that is,
429431
#! `IsEqualForObjects( `<A>obj</A>`, ObjectConstructor( CapCategory( `<A>obj</A>` ), ObjectDatum( `<A>obj</A>` ) ) )`.
432+
#! Note that by default this CAP operation is not cached. You can change this behaviour
433+
#! by calling `SetCachingToWeak( C, "ObjectDatum" )` resp. `SetCachingToCrisp( C, "ObjectDatum" )`.
430434
#! @Returns depends on the category
431435
#! @Arguments obj
432436
DeclareAttribute( "ObjectDatum",

FreydCategoriesForCAP/PackageInfo.g

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Version := Maximum( [
1717
## this line prevents merge conflicts
1818
"2020.05-17", ## Mohamed's version
1919
## this line prevents merge conflicts
20-
"2021.07-02", ## Fabian's version
20+
"2021.07-03", ## Fabian's version
2121
## this line prevents merge conflicts
2222
"2020.04-18", ## Kamal's version
2323
] ),

FreydCategoriesForCAP/gap/CategoryOfColumns.gi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ InstallMethod( CategoryOfColumns,
3737
),
3838
);
3939

40+
# this cache replaces the KeyDependentOperation caching when using ObjectConstructor directly instead of CategoryOfColumnsObject
41+
SetCachingToWeak( category, "ObjectConstructor" );
42+
4043
SetFilterObj( category, IsCategoryOfColumns );
4144

4245
if HasHasInvariantBasisProperty( homalg_ring ) and HasInvariantBasisProperty( homalg_ring ) then

FreydCategoriesForCAP/gap/CategoryOfRows.gi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ InstallMethod( CategoryOfRows,
3737
),
3838
);
3939

40+
# this cache replaces the KeyDependentOperation caching when using ObjectConstructor directly instead of CategoryOfRowsObject
41+
SetCachingToWeak( category, "ObjectConstructor" );
42+
4043
SetFilterObj( category, IsCategoryOfRows );
4144

4245
if HasHasInvariantBasisProperty( homalg_ring ) and HasInvariantBasisProperty( homalg_ring ) then

LinearAlgebraForCAP/gap/LinearAlgebraForCAP.gi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ InstallGlobalFunction( MATRIX_CATEGORY,
4545
),
4646
);
4747

48+
# this cache replaces the KeyDependentOperation caching when using ObjectConstructor directly instead of MatrixCategoryObject
49+
SetCachingToWeak( category, "ObjectConstructor" );
50+
4851
SetFilterObj( category, IsMatrixCategory );
4952

5053
AddObjectRepresentation( category, IsVectorSpaceObject and HasIsProjective and IsProjective );

0 commit comments

Comments
 (0)