Skip to content

Commit e334b5d

Browse files
committed
Convert cellExecutionMetadataTable into lru-cache
1 parent bec532a commit e334b5d

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/index.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,12 @@ const extension: JupyterFrontEndPlugin<void> = {
103103
let reportCellExecutionTime = true;
104104
let reportCellNumber = true;
105105
let cellNumberType = 'cell_index';
106-
const cellExecutionMetadataTable: {
107-
[cellId: string]: ICellExecutionMetadata;
108-
} = {};
106+
const cellExecutionMetadataTable: LRU<
107+
string,
108+
ICellExecutionMetadata
109+
> = new LRU({
110+
max: 500 * 5 // to save 500 notebooks x 5 cells
111+
});
109112
const recentNotebookExecutionTimes: LRU<string, Date> = new LRU({
110113
max: 500
111114
});
@@ -129,10 +132,10 @@ const extension: JupyterFrontEndPlugin<void> = {
129132
NotebookActions.executionScheduled.connect((_, args) => {
130133
const { cell, notebook } = args;
131134
if (enabled) {
132-
cellExecutionMetadataTable[cell.model.id] = {
135+
cellExecutionMetadataTable.set(cell.model.id, {
133136
index: notebook.activeCellIndex,
134137
scheduledTime: new Date()
135-
};
138+
});
136139
}
137140
});
138141

@@ -142,28 +145,33 @@ const extension: JupyterFrontEndPlugin<void> = {
142145
const { cell, notebook, success, error } = args;
143146
const cellId = cell.model.id;
144147
const notebookId = notebook.id;
145-
const scheduledTime = cellExecutionMetadataTable[cellId].scheduledTime;
148+
const cellExecutionMetadata = cellExecutionMetadataTable.get(cellId);
149+
const scheduledTime = cellExecutionMetadata.scheduledTime;
150+
// Get the cell's execution scheduled time if the recent notebook execution state doesn't exist.
151+
// This happens commonly for first time notebook executions or notebooks that haven't been executed for a while.
146152
const recentExecutedCellTime =
147153
recentNotebookExecutionTimes.get(notebookId) || scheduledTime;
148-
cellExecutionMetadataTable[cellId].startTime =
154+
155+
// Multiple cells can be scheduled at the same time, and the schedule time doesn't necessarily equate to the actual start time.
156+
// If another cell has been executed more recently than the current cell's scheduled time, treat the recent execution as the cell's start time.
157+
cellExecutionMetadata.startTime =
149158
scheduledTime >= recentExecutedCellTime
150159
? scheduledTime
151160
: recentExecutedCellTime;
152-
cellExecutionMetadataTable[cellId].endTime = cellEndTime;
161+
cellExecutionMetadata.endTime = cellEndTime;
153162
recentNotebookExecutionTimes.set(notebookId, cellEndTime);
154163

155164
triggerNotification(
156165
cell,
157166
notebook,
158-
cellExecutionMetadataTable[cellId],
167+
cellExecutionMetadata,
159168
minimumCellExecutionTime,
160169
reportCellNumber,
161170
reportCellExecutionTime,
162171
cellNumberType,
163172
!success,
164173
error
165174
);
166-
delete cellExecutionMetadataTable[cellId];
167175
}
168176
});
169177
}

0 commit comments

Comments
 (0)