From 9891d03f55e9e097e009d10f1ec3621e268c19e8 Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Sun, 24 Nov 2024 22:15:16 -0800 Subject: [PATCH] reuse cborreader in diskpersister --- events/diskpersist.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/events/diskpersist.go b/events/diskpersist.go index 25eb989af..9c28ffb04 100644 --- a/events/diskpersist.go +++ b/events/diskpersist.go @@ -709,6 +709,7 @@ func (dp *DiskPersistence) readEventsFrom(ctx context.Context, since int64, fn s lastSeq := int64(0) + cr := cbg.NewCborReader(nil) scratch := make([]byte, headerSize) for { h, err := readHeader(bufr, scratch) @@ -731,10 +732,12 @@ func (dp *DiskPersistence) readEventsFrom(ctx context.Context, since int64, fn s continue } + limr := io.LimitReader(bufr, h.Len64()) + cr.SetReader(limr) switch h.Kind { case evtKindCommit: var evt atproto.SyncSubscribeRepos_Commit - if err := evt.UnmarshalCBOR(io.LimitReader(bufr, h.Len64())); err != nil { + if err := evt.UnmarshalCBOR(cr); err != nil { return nil, err } evt.Seq = h.Seq @@ -743,7 +746,7 @@ func (dp *DiskPersistence) readEventsFrom(ctx context.Context, since int64, fn s } case evtKindHandle: var evt atproto.SyncSubscribeRepos_Handle - if err := evt.UnmarshalCBOR(io.LimitReader(bufr, h.Len64())); err != nil { + if err := evt.UnmarshalCBOR(cr); err != nil { return nil, err } evt.Seq = h.Seq @@ -752,7 +755,7 @@ func (dp *DiskPersistence) readEventsFrom(ctx context.Context, since int64, fn s } case evtKindIdentity: var evt atproto.SyncSubscribeRepos_Identity - if err := evt.UnmarshalCBOR(io.LimitReader(bufr, h.Len64())); err != nil { + if err := evt.UnmarshalCBOR(cr); err != nil { return nil, err } evt.Seq = h.Seq @@ -761,7 +764,7 @@ func (dp *DiskPersistence) readEventsFrom(ctx context.Context, since int64, fn s } case evtKindAccount: var evt atproto.SyncSubscribeRepos_Account - if err := evt.UnmarshalCBOR(io.LimitReader(bufr, h.Len64())); err != nil { + if err := evt.UnmarshalCBOR(cr); err != nil { return nil, err } evt.Seq = h.Seq @@ -770,7 +773,7 @@ func (dp *DiskPersistence) readEventsFrom(ctx context.Context, since int64, fn s } case evtKindTombstone: var evt atproto.SyncSubscribeRepos_Tombstone - if err := evt.UnmarshalCBOR(io.LimitReader(bufr, h.Len64())); err != nil { + if err := evt.UnmarshalCBOR(cr); err != nil { return nil, err } evt.Seq = h.Seq