@@ -585,55 +585,84 @@ static_assert(std::is_nothrow_default_constructible_v<TOwnedCellVec>, "Expected
585
585
// When loading from a buffer the cells will point to the buffer contents
586
586
class TSerializedCellVec {
587
587
public:
588
+ TSerializedCellVec () = default ;
589
+
588
590
explicit TSerializedCellVec (TConstArrayRef<TCell> cells);
589
591
590
- explicit TSerializedCellVec (const TString& buf)
592
+ explicit TSerializedCellVec (TString&& buf)
593
+ : Buf(std::move(buf))
591
594
{
592
- Parse (buf );
595
+ Y_ABORT_UNLESS ( DoTryParse () );
593
596
}
594
597
595
- TSerializedCellVec () = default ;
596
-
597
- TSerializedCellVec (const TSerializedCellVec &other)
598
- : Buf(other.Buf)
599
- , Cells(other.Cells)
598
+ explicit TSerializedCellVec (const TString& buf)
599
+ : Buf(buf)
600
600
{
601
- Y_ABORT_UNLESS (Buf. data () == other. Buf . data (), " Buffer must be shared " );
601
+ Y_ABORT_UNLESS (DoTryParse () );
602
602
}
603
603
604
- TSerializedCellVec (TSerializedCellVec && other)
604
+ TSerializedCellVec (TSerializedCellVec&& other)
605
605
{
606
606
*this = std::move (other);
607
607
}
608
608
609
- TSerializedCellVec &operator =(const TSerializedCellVec &other)
609
+ TSerializedCellVec (const TSerializedCellVec& other)
610
+ {
611
+ *this = other;
612
+ }
613
+
614
+ TSerializedCellVec& operator =(TSerializedCellVec&& other)
610
615
{
611
616
if (this == &other)
612
617
return *this ;
613
618
614
- TSerializedCellVec tmp (other);
615
- *this = std::move (tmp);
619
+ const char * prevPtr = other.Buf .data ();
620
+ Buf = std::move (other.Buf );
621
+ if (Buf.data () != prevPtr) {
622
+ // Data address changed, e.g. when TString is a small std::string
623
+ Y_ABORT_UNLESS (DoTryParse (), " Failed to re-parse TSerializedCellVec" );
624
+ other.Cells .clear ();
625
+ } else {
626
+ // Data address unchanged, reuse parsed Cells
627
+ Cells = std::move (other.Cells );
628
+ }
616
629
return *this ;
617
630
}
618
631
619
- TSerializedCellVec & operator =(TSerializedCellVec && other)
632
+ TSerializedCellVec& operator =(const TSerializedCellVec& other)
620
633
{
621
634
if (this == &other)
622
635
return *this ;
623
636
624
- const char * otherPtr = other.Buf .data ();
625
- Buf = std::move (other.Buf );
626
- Y_ABORT_UNLESS (Buf.data () == otherPtr, " Buffer address must not change" );
627
- Cells = std::move (other.Cells );
637
+ Buf = other.Buf ;
638
+ if (Buf.data () != other.Buf .data ()) {
639
+ // Data address changed, e.g. when TString is std::string
640
+ Y_ABORT_UNLESS (DoTryParse (), " Failed to re-parse TSerializedCellVec" );
641
+ } else {
642
+ // Data address unchanged, reuse parsed Cells
643
+ Cells = other.Cells ;
644
+ }
628
645
return *this ;
629
646
}
630
647
648
+ static bool TryParse (TString&& data, TSerializedCellVec& vec) {
649
+ vec.Buf = std::move (data);
650
+ return vec.DoTryParse ();
651
+ }
652
+
631
653
static bool TryParse (const TString& data, TSerializedCellVec& vec) {
632
- return vec.DoTryParse (data);
654
+ vec.Buf = data;
655
+ return vec.DoTryParse ();
656
+ }
657
+
658
+ void Parse (TString&& buf) {
659
+ Buf = std::move (buf);
660
+ Y_ABORT_UNLESS (DoTryParse ());
633
661
}
634
662
635
- void Parse (const TString &buf) {
636
- Y_ABORT_UNLESS (DoTryParse (buf));
663
+ void Parse (const TString& buf) {
664
+ Buf = buf;
665
+ Y_ABORT_UNLESS (DoTryParse ());
637
666
}
638
667
639
668
TConstArrayRef<TCell> GetCells () const {
@@ -654,15 +683,15 @@ class TSerializedCellVec {
654
683
655
684
static size_t SerializedSize (TConstArrayRef<TCell> cells);
656
685
657
- const TString & GetBuffer () const { return Buf; }
686
+ const TString& GetBuffer () const { return Buf; }
658
687
659
688
TString ReleaseBuffer () {
660
689
Cells.clear ();
661
690
return std::move (Buf);
662
691
}
663
692
664
693
private:
665
- bool DoTryParse (const TString& data );
694
+ bool DoTryParse ();
666
695
667
696
private:
668
697
TString Buf;
@@ -673,14 +702,21 @@ class TSerializedCellVec {
673
702
// When loading from a buffer the cells will point to the buffer contents
674
703
class TSerializedCellMatrix {
675
704
public:
705
+ TSerializedCellMatrix () = default ;
706
+
676
707
explicit TSerializedCellMatrix (TConstArrayRef<TCell> cells, ui32 rowCount, ui16 colCount);
677
708
678
- explicit TSerializedCellMatrix (const TString& buf)
709
+ explicit TSerializedCellMatrix (TString&& buf)
710
+ : Buf(std::move(buf))
679
711
{
680
- Parse (buf );
712
+ Y_ABORT_UNLESS ( DoTryParse () );
681
713
}
682
714
683
- TSerializedCellMatrix () = default ;
715
+ explicit TSerializedCellMatrix (const TString& buf)
716
+ : Buf(buf)
717
+ {
718
+ Y_ABORT_UNLESS (DoTryParse ());
719
+ }
684
720
685
721
TSerializedCellMatrix (const TSerializedCellMatrix& other)
686
722
: Buf(other.Buf)
@@ -696,36 +732,64 @@ class TSerializedCellMatrix {
696
732
*this = std::move (other);
697
733
}
698
734
699
- TSerializedCellMatrix& operator =(const TSerializedCellMatrix& other)
735
+ TSerializedCellMatrix& operator =(TSerializedCellMatrix& & other)
700
736
{
701
737
if (this == &other)
702
738
return *this ;
703
739
704
- TSerializedCellMatrix tmp (other);
705
- *this = std::move (tmp);
740
+ const char * prevPtr = other.Buf .data ();
741
+ Buf = std::move (other.Buf );
742
+ if (Buf.data () != prevPtr) {
743
+ // Data address changed, e.g. when TString is a small std::string
744
+ Y_ABORT_UNLESS (DoTryParse (), " Failed to re-parse TSerializedCellMatrix" );
745
+ other.Cells .clear ();
746
+ } else {
747
+ // Data address unchanged, reuse parsed cells
748
+ Cells = std::move (other.Cells );
749
+ RowCount = other.RowCount ;
750
+ ColCount = other.ColCount ;
751
+ }
752
+ other.RowCount = 0 ;
753
+ other.ColCount = 0 ;
706
754
return *this ;
707
755
}
708
756
709
- TSerializedCellMatrix& operator =(TSerializedCellMatrix& & other)
757
+ TSerializedCellMatrix& operator =(const TSerializedCellMatrix& other)
710
758
{
711
759
if (this == &other)
712
760
return *this ;
713
761
714
- const char * otherPtr = other.Buf .data ();
715
- Buf = std::move (other.Buf );
716
- Y_ABORT_UNLESS (Buf.data () == otherPtr, " Buffer address must not change" );
717
- Cells = std::move (other.Cells );
718
- RowCount = std::move (other.RowCount );
719
- ColCount = std::move (other.ColCount );
762
+ Buf = other.Buf ;
763
+ if (Buf.data () != other.Buf .data ()) {
764
+ // Data address changed, e.g. when TString is std::string
765
+ Y_ABORT_UNLESS (DoTryParse (), " Failed to re-parse TSerializedCellMatrix" );
766
+ } else {
767
+ // Data address unchanged, reuse parsed cells
768
+ Cells = other.Cells ;
769
+ RowCount = other.RowCount ;
770
+ ColCount = other.ColCount ;
771
+ }
720
772
return *this ;
721
773
}
722
774
775
+ static bool TryParse (TString&& data, TSerializedCellMatrix& vec) {
776
+ vec.Buf = std::move (data);
777
+ return vec.DoTryParse ();
778
+ }
779
+
723
780
static bool TryParse (const TString& data, TSerializedCellMatrix& vec) {
724
- return vec.DoTryParse (data);
781
+ vec.Buf = data;
782
+ return vec.DoTryParse ();
783
+ }
784
+
785
+ void Parse (TString&& buf) {
786
+ Buf = std::move (buf);
787
+ Y_ABORT_UNLESS (DoTryParse ());
725
788
}
726
789
727
790
void Parse (const TString& buf) {
728
- Y_ABORT_UNLESS (DoTryParse (buf));
791
+ Buf = buf;
792
+ Y_ABORT_UNLESS (DoTryParse ());
729
793
}
730
794
731
795
TConstArrayRef<TCell> GetCells () const { return Cells; }
@@ -753,7 +817,7 @@ class TSerializedCellMatrix {
753
817
}
754
818
755
819
private:
756
- bool DoTryParse (const TString& data );
820
+ bool DoTryParse ();
757
821
758
822
private:
759
823
TString Buf;
0 commit comments