Skip to content

Commit 5bc85ba

Browse files
authored
Remove memory hog reserialization of block data (#1329)
1 parent 1649a58 commit 5bc85ba

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

pydatalab/src/pydatalab/blocks/base.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import functools
2-
import json
32
import random
43
import warnings
54
from collections.abc import Callable, Sequence
@@ -237,9 +236,7 @@ def to_web(self) -> dict[str, Any]:
237236
else:
238237
self.data.pop("warnings", None)
239238

240-
model = self.block_db_model(**self.data)
241-
serialized = json.loads(model.json(exclude_unset=True, exclude_none=True))
242-
return serialized
239+
return self.block_db_model(**self.data).dict(exclude_unset=True, exclude_none=True)
243240

244241
def process_events(self, events: list[dict] | dict):
245242
"""Handle any supported events passed to the block."""

pydatalab/src/pydatalab/utils.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from math import ceil
99

1010
import pandas as pd
11-
from bson import json_util
11+
from bson import ObjectId
1212
from flask.json.provider import DefaultJSONProvider
1313

1414

@@ -43,7 +43,10 @@ def default(o):
4343
if isinstance(o, (datetime.date, datetime.datetime)):
4444
return o.isoformat()
4545

46-
return json_util.default(o)
46+
elif isinstance(o, ObjectId):
47+
return str(o)
48+
49+
raise RuntimeError(f"Type {type(o)} not serializable")
4750

4851

4952
class BSONProvider(DefaultJSONProvider):

pydatalab/tests/server/test_blocks.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ def test_uvvis_block_lifecycle(admin_client, default_sample_dict, example_data_d
256256

257257
assert response.status_code == 200
258258
web_block = response.json["new_block_data"]
259+
assert web_block["file_id"] == example_file_ids[0]
259260
assert "bokeh_plot_data" in web_block
260261
assert web_block.get("errors") is None
261262

@@ -573,7 +574,7 @@ def create_large_xye_file(tmpdir):
573574
yield fname
574575

575576

576-
@pytest.mark.limit_memory("130MB")
577+
@pytest.mark.limit_memory("110MB")
577578
def test_large_fake_xrd_data_block_serialization(
578579
admin_client, default_sample_dict, tmpdir, create_large_xye_file
579580
):

0 commit comments

Comments
 (0)