Skip to content

Commit 0a3d575

Browse files
authored
perf: dataSetHasCDNMetadataKey (#274)
Reviewer @rvagg @ZenGround0 This avoids loading all of the metadata keys from storage into memory. This reduces codesize 24,190 -> 23,991 (-199) The codesize reduces because the storage loading into memory and the `dataSetMetadataKeys` mapping lookup are currently duplicated in three places. #### Changes * use separate function dataSetHasCDNMetadataKey
1 parent 32d6ca4 commit 0a3d575

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

service_contracts/src/FilecoinWarmStorageService.sol

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ contract FilecoinWarmStorageService is
704704

705705
// Clean up rail mappings
706706
delete railToDataSet[info.pdpRailId];
707-
if (hasCDNMetadataKey(dataSetMetadataKeys[dataSetId])) {
707+
if (dataSetHasCDNMetadataKey(dataSetId)) {
708708
delete railToDataSet[info.cacheMissRailId];
709709
delete railToDataSet[info.cdnRailId];
710710
}
@@ -1059,7 +1059,7 @@ contract FilecoinWarmStorageService is
10591059
require(msg.sender == info.payer, Errors.CallerNotPayer(dataSetId, info.payer, msg.sender));
10601060

10611061
// Check if CDN service is configured
1062-
require(hasCDNMetadataKey(dataSetMetadataKeys[dataSetId]), Errors.FilBeamServiceNotConfigured(dataSetId));
1062+
require(dataSetHasCDNMetadataKey(dataSetId), Errors.FilBeamServiceNotConfigured(dataSetId));
10631063

10641064
// Check if cache miss and CDN rails are configured
10651065
require(info.cacheMissRailId != 0 && info.cdnRailId != 0, Errors.InvalidDataSetId(dataSetId));
@@ -1149,7 +1149,7 @@ contract FilecoinWarmStorageService is
11491149
emit RailRateUpdated(dataSetId, pdpRailId, newStorageRatePerEpoch);
11501150

11511151
// Update the CDN rail payment rates, if applicable
1152-
if (hasCDNMetadataKey(dataSetMetadataKeys[dataSetId])) {
1152+
if (dataSetHasCDNMetadataKey(dataSetId)) {
11531153
(uint256 newCacheMissRatePerEpoch, uint256 newCDNRatePerEpoch) = _calculateCDNRates(totalBytes);
11541154

11551155
uint256 cacheMissRailId = dataSetInfo[dataSetId].cacheMissRailId;
@@ -1335,6 +1335,29 @@ contract FilecoinWarmStorageService is
13351335
return false;
13361336
}
13371337

1338+
/**
1339+
* @notice Returns true if key `withCDN` exists in the metadata keys of the data set.
1340+
* @param dataSetId The sequential data set identifier
1341+
* @return True if key exists; false otherwise.
1342+
*/
1343+
function dataSetHasCDNMetadataKey(uint256 dataSetId) internal view returns (bool) {
1344+
string[] storage metadataKeys = dataSetMetadataKeys[dataSetId];
1345+
unchecked {
1346+
uint256 len = metadataKeys.length;
1347+
for (uint256 i = 0; i < len; i++) {
1348+
string storage metadataKey = metadataKeys[i];
1349+
bytes32 repr;
1350+
assembly ("memory-safe") {
1351+
repr := sload(metadataKey.slot)
1352+
}
1353+
if (repr == WITH_CDN_STRING_STORAGE_REPR) {
1354+
return true;
1355+
}
1356+
}
1357+
}
1358+
return false;
1359+
}
1360+
13381361
/**
13391362
* @notice Deletes key `withCDN` if it exists in `metadataKeys`.
13401363
* @param metadataKeys The array of metadata keys to modify

0 commit comments

Comments
 (0)