Skip to content

Commit c9846c9

Browse files
authored
perf(storage): use google_crc32c.value() for simpler crc32c calculation (#16761)
## Summary - Simplified CRC32C calculation by using `google_crc32c.value()`. - Improved code readability and reduced complexity in checksum logic.
1 parent 7073be1 commit c9846c9

4 files changed

Lines changed: 8 additions & 15 deletions

File tree

‎packages/google-cloud-storage/google/cloud/storage/asyncio/retry/writes_resumption_strategy.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ def generate_requests(
8484
break
8585

8686
checksummed_data=storage_type.ChecksummedData(content=chunk)
87-
checksum=google_crc32c.Checksum(chunk)
88-
checksummed_data.crc32c=int.from_bytes(checksum.digest(), "big")
87+
checksummed_data.crc32c=google_crc32c.value(chunk)
8988

9089
request=storage_type.BidiWriteObjectRequest(
9190
write_offset=write_state.bytes_sent,

‎packages/google-cloud-storage/tests/unit/asyncio/retry/test_reads_resumption_strategy.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
importunittest
1818

1919
fromgoogle.api_coreimportexceptions
20-
fromgoogle_crc32cimportChecksum
20+
importgoogle_crc32c
2121

2222
fromgoogle.cloudimport_storage_v2asstorage_v2
2323
fromgoogle.cloud._storage_v2.types.storageimportBidiReadObjectRedirectedError
@@ -77,8 +77,7 @@ def _create_response(
7777
checksummed_data=None
7878
ifcontentisnotNone:
7979
ifcrcisNone:
80-
c=Checksum(content)
81-
crc=int.from_bytes(c.digest(), "big")
80+
crc=google_crc32c.value(content)
8281
checksummed_data=storage_v2.ChecksummedData(content=content, crc32c=crc)
8382

8483
read_range=None

‎packages/google-cloud-storage/tests/unit/asyncio/retry/test_writes_resumption_strategy.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ def test_generate_requests_checksum_verification(self, strategy):
125125

126126
requests=strategy.generate_requests(state)
127127

128-
expected_crc=google_crc32c.Checksum(chunk_data).digest()
129-
expected_int=int.from_bytes(expected_crc, "big")
128+
expected_int=google_crc32c.value(chunk_data)
130129
assertrequests[0].checksummed_data.crc32c==expected_int
131130

132131
deftest_generate_requests_flush_logic_exact_interval(self, strategy):

‎packages/google-cloud-storage/tests/unit/asyncio/test_async_multi_range_downloader.py‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
importpytest
2121
fromgoogle.api_coreimportexceptions
22-
fromgoogle_crc32cimportChecksum
22+
importgoogle_crc32c
2323

2424
fromgoogle.cloudimport_storage_v2
2525
fromgoogle.cloud.storage.asyncioimportasync_read_object_stream
@@ -106,11 +106,8 @@ async def test_download_ranges_via_async_gather(
106106
self, mock_cls_async_read_object_stream, mock_random_int
107107
):
108108
data=b"these_are_18_chars"
109-
crc32c=Checksum(data).digest()
110-
crc32c_int=int.from_bytes(crc32c, "big")
111-
crc32c_checksum_for_data_slice=int.from_bytes(
112-
Checksum(data[10:16]).digest(), "big"
113-
)
109+
crc32c_int=google_crc32c.value(data)
110+
crc32c_checksum_for_data_slice=google_crc32c.value(data[10:16])
114111

115112
mock_mrd, _=awaitself._make_mock_mrd(mock_cls_async_read_object_stream)
116113
mock_random_int.side_effect= [456, 91011]
@@ -187,8 +184,7 @@ async def test_download_ranges(
187184
):
188185
# Arrange
189186
data=b"these_are_18_chars"
190-
crc32c=Checksum(data).digest()
191-
crc32c_int=int.from_bytes(crc32c, "big")
187+
crc32c_int=google_crc32c.value(data)
192188

193189
mock_mrd, _=awaitself._make_mock_mrd(mock_cls_async_read_object_stream)
194190
mock_random_int.side_effect= [456]

0 commit comments

Comments
 (0)