diff --git a/mercurial/bundle2.py b/mercurial/bundle2.py
index 1fb0846ad792ff28983ebbdd2f05c0c6e5787a4e_bWVyY3VyaWFsL2J1bmRsZTIucHk=..764e3ad1cf54e9d6c7a74c70051820d422adadfd_bWVyY3VyaWFsL2J1bmRsZTIucHk= 100644
--- a/mercurial/bundle2.py
+++ b/mercurial/bundle2.py
@@ -1196,7 +1196,10 @@
     dolog = ui.configbool('devel', 'bundle2.debug')
     debug = ui.debug
 
-    headersize = struct.calcsize(_fpayloadsize)
+    headerstruct = struct.Struct(_fpayloadsize)
+    headersize = headerstruct.size
+    unpack = headerstruct.unpack
+
     readexactly = changegroup.readexactly
     read = fh.read
 
@@ -1200,7 +1203,7 @@
     readexactly = changegroup.readexactly
     read = fh.read
 
-    chunksize = _unpack(_fpayloadsize, readexactly(fh, headersize))[0]
+    chunksize = unpack(readexactly(fh, headersize))[0]
     indebug(ui, 'payload chunk size: %i' % chunksize)
 
     # changegroup.readexactly() is inlined below for performance.
@@ -1227,7 +1230,7 @@
                                 ' (got %d bytes, expected %d)') %
                               (len(s), chunksize))
 
-        chunksize = _unpack(_fpayloadsize, s)[0]
+        chunksize = unpack(s)[0]
 
         # indebug() inlined for performance.
         if dolog: