Skip to content

Commit f1c2f91

Browse files
authored
Merge pull request #1690 from zickgraf/master
Avoid unnecessary trigger of derivations and do not rely on AddPrimitiveOperation when installing final derivations
2 parents 4df2fc6 + f5d096e commit f1c2f91

File tree

5 files changed

+54
-20
lines changed

5 files changed

+54
-20
lines changed

CAP/PackageInfo.g

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ SetPackageInfo( rec(
1010

1111
PackageName := "CAP",
1212
Subtitle := "Categories, Algorithms, Programming",
13-
Version := "2024.09-23",
13+
Version := "2024.09-24",
1414
Date := (function ( ) if IsBound( GAPInfo.SystemEnvironment.GAP_PKG_RELEASE_DATE ) then return GAPInfo.SystemEnvironment.GAP_PKG_RELEASE_DATE; else return Concatenation( ~.Version{[ 1 .. 4 ]}, "-", ~.Version{[ 6, 7 ]}, "-01" ); fi; end)( ),
1515
License := "GPL-2.0-or-later",
1616

CAP/gap/Derivations.gi

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,6 @@ FunctionWithNamedArguments(
108108
function( CAP_NAMED_ARGUMENTS, d, weight, C )
109109
local method_name, func;
110110

111-
Info( DerivationInfo, 1, Concatenation( "install(",
112-
String( weight ),
113-
") ",
114-
TargetOperation( d ),
115-
": ",
116-
Description( d ), "\n" ) );
117-
118111
method_name := TargetOperation( d );
119112
func := DerivationFunction( d );
120113

@@ -459,14 +452,26 @@ BindGlobal( "TryToInstallDerivation", function ( owl, d )
459452

460453
if new_weight < current_weight or (new_weight = current_weight and current_derivation <> fail and d!.position_in_derivations_by_target < current_derivation!.position_in_derivations_by_target) then
461454

455+
Info( DerivationInfo, 1, Concatenation( "install(",
456+
String( new_weight ),
457+
") ",
458+
target,
459+
": ",
460+
Description( d ), "\n" ) );
461+
462462
# Previously, `InstallDerivationForCategory` was called at this point.
463463
# However, this could lead to methods being overwritten if cheaper derivations become available while adding primitive installations to a category.
464464
# Hence, we now install the derivations in `Finalize`.
465465

466466
owl!.operation_weights.( target ) := new_weight;
467467
owl!.operation_derivations.( target ) := d;
468468

469-
InstallDerivationsUsingOperation( owl, target );
469+
# if the weight has not changed, there is no need to re-trigger the chain of derivations
470+
if new_weight <> current_weight then
471+
472+
InstallDerivationsUsingOperation( owl, target );
473+
474+
fi;
470475

471476
fi;
472477

@@ -519,18 +524,26 @@ end );
519524

520525
InstallMethod( AddPrimitiveOperation,
521526
[ IsOperationWeightList, IsString, IsInt ],
522-
function( owl, op_name, weight )
527+
function( owl, op_name, new_weight )
528+
local current_weight;
523529

524530
Info( DerivationInfo, 1, Concatenation( "install(",
525-
String( weight ),
531+
String( new_weight ),
526532
") ",
527533
op_name,
528534
": primitive installation\n" ) );
529535

530-
owl!.operation_weights.( op_name ) := weight;
536+
current_weight := owl!.operation_weights.( op_name );
537+
538+
owl!.operation_weights.( op_name ) := new_weight;
531539
owl!.operation_derivations.( op_name ) := fail;
532540

533-
InstallDerivationsUsingOperation( owl, op_name );
541+
# if the weight has not changed, there is no need to re-trigger the chain of derivations
542+
if new_weight <> current_weight then
543+
544+
InstallDerivationsUsingOperation( owl, op_name );
545+
546+
fi;
534547

535548
end );
536549

CAP/gap/Finalize.gi

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ InstallMethod( Finalize,
292292
[ "FinalizeCategory", true ],
293293
],
294294
function( CAP_NAMED_ARGUMENTS, category )
295-
local derivation_list, weight_list, current_install, current_final_derivation, weight, old_weights, categorical_properties, diff, properties_with_logic, property, i, derivation, operation, property_name, installed_operations_of_homomorphism_structure, original_REORDER_METHODS_SUSPENSION_LEVEL;
295+
local derivation_list, weight_list, current_install, current_final_derivation, op_name, new_weight, current_weight, old_weights, categorical_properties, diff, properties_with_logic, property, i, derivation, operation, property_name, installed_operations_of_homomorphism_structure, original_REORDER_METHODS_SUSPENSION_LEVEL;
296296

297297
if IsFinalized( category ) then
298298

@@ -391,16 +391,35 @@ InstallMethod( Finalize,
391391

392392
for derivation in current_final_derivation.derivations do
393393

394-
weight := OperationWeightUsingDerivation( weight_list, derivation );
394+
op_name := TargetOperation( derivation );
395+
new_weight := OperationWeightUsingDerivation( weight_list, derivation );
396+
current_weight := CurrentOperationWeight( weight_list, op_name );
395397

396-
Assert( 0, weight <> infinity );
398+
Assert( 0, new_weight <> infinity );
397399

398400
# When installing a final derivation bundle, the installation of the first operations in the bundle
399401
# might trigger (normal) derivations of later operations it the bundle, which might be cheaper then
400402
# the derivations provided in the bundle.
401-
if weight <= CurrentOperationWeight( weight_list, TargetOperation( derivation ) ) then
403+
if new_weight <= current_weight then
402404

403-
InstallDerivationForCategory( derivation, weight, category : IsFinalDerivation := true );
405+
Info( DerivationInfo, 1, Concatenation( "install(",
406+
String( new_weight ),
407+
") ",
408+
op_name,
409+
": ",
410+
Description( derivation ), "\n" ) );
411+
412+
InstallDerivationForCategory( derivation, new_weight, category : IsFinalDerivation := true );
413+
414+
weight_list!.operation_weights.( op_name ) := new_weight;
415+
weight_list!.operation_derivations.( op_name ) := fail;
416+
417+
# if the weight has not changed, there is no need to re-trigger the chain of derivations
418+
if new_weight <> current_weight then
419+
420+
InstallDerivationsUsingOperation( weight_list, op_name );
421+
422+
fi;
404423

405424
fi;
406425

CAP/gap/InstallAdds.gi

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,8 @@ InstallMethod( AddCapOperation,
285285

286286
fi;
287287

288-
if not is_derivation then
288+
if not (is_derivation or is_final_derivation) then
289289

290-
# Final derivations are not handled by the original derivation mechanism and are thus just like primitive operations for it.
291290
AddPrimitiveOperation( category!.derivations_weight_list, function_name, weight );
292291

293292
fi;

FreydCategoriesForCAP/gap/CategoryOfRows.gi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ InstallMethod( CategoryOfRows,
4141

4242
SetIsRigidSymmetricCoclosedMonoidalCategory( cat, true );
4343

44+
# since methods have been added before, we have to reevaluate the derivations
45+
Reevaluate( cat!.derivations_weight_list );
46+
4447
fi;
4548

4649
INSTALL_FUNCTIONS_FOR_CATEGORY_OF_ROWS( cat );

0 commit comments

Comments
 (0)