SQLite

Check-in [682dc24dbe]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Test versions of getVarint functions. The updates essentially utilize loop unrolling and some shifting/anding tricks to minimize the number of logical operations required. (CVS 5072)
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 682dc24dbe82d0326377e27c5ff97db3499873b0
User & Date: shane 2008-05-01 02:47:04.000
Context
2008-05-01
17:03
Fix harmless compiler warnings. (CVS 5073) (check-in: 227a6f67c2 user: drh tags: trunk)
02:47
Test versions of getVarint functions. The updates essentially utilize loop unrolling and some shifting/anding tricks to minimize the number of logical operations required. (CVS 5072) (check-in: 682dc24dbe user: shane tags: trunk)
2008-04-30
16:38
Add comment to speculate when setting journal_mode=OFF on VACUUM does not help performance. No changes to code. (CVS 5071) (check-in: 9c8b4babb2 user: drh tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/util.c.
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
**
*************************************************************************
** Utility functions used throughout sqlite.
**
** This file contains functions for allocating memory, comparing
** strings, and stuff like that.
**
** $Id: util.c,v 1.224 2008/04/28 17:41:31 shane Exp $
*/
#include "sqliteInt.h"
#include <stdarg.h>
#include <ctype.h>


/*







|







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
**
*************************************************************************
** Utility functions used throughout sqlite.
**
** This file contains functions for allocating memory, comparing
** strings, and stuff like that.
**
** $Id: util.c,v 1.225 2008/05/01 02:47:04 shane Exp $
*/
#include "sqliteInt.h"
#include <stdarg.h>
#include <ctype.h>


/*
529
530
531
532
533
534
535
536
537
538
539
540

541
542
543






544
545

546
547
548
549




550





551
552
553
554






555






556
557
558
559






560

561















562




563















564














565
















566
567




568



569










570
571
572
573
574
575
576
577
578
579
580
581
582

583
584
585


586
587
588
589






590




591









592
593
594
595








596



597












598












599









600
601

























602
603
604
605
606
607
608
}

/*
** Read a 64-bit variable-length integer from memory starting at p[0].
** Return the number of bytes read.  The value is stored in *v.
*/
int sqlite3GetVarint(const unsigned char *p, u64 *v){
  u32 x;
  u64 x64;
  int n;
  unsigned char c;
  if( ((c = p[0]) & 0x80)==0 ){

    *v = c;
    return 1;
  }






  x = c & 0x7f;
  if( ((c = p[1]) & 0x80)==0 ){

    *v = (x<<7) | c;
    return 2;
  }
  x = (x<<7) | (c&0x7f);




  if( ((c = p[2]) & 0x80)==0 ){





    *v = (x<<7) | c;
    return 3;
  }
  x = (x<<7) | (c&0x7f);






  if( ((c = p[3]) & 0x80)==0 ){






    *v = (x<<7) | c;
    return 4;
  }
  x64 = (x<<7) | (c&0x7f);






  n = 4;

  do{















    c = p[n++];




    if( n==9 ){















      x64 = (x64<<8) | c;














      break;
















    }
    x64 = (x64<<7) | (c&0x7f);




  }while( (c & 0x80)!=0 );



  *v = x64;










  return n;
}

/*
** Read a 32-bit variable-length integer from memory starting at p[0].
** Return the number of bytes read.  The value is stored in *v.
** A MACRO version, getVarint32, is provided which inlines the 
** single-byte case.  All code should use the MACRO version as 
** this function assumes the single-byte case has already been handled.
*/
int sqlite3GetVarint32(const unsigned char *p, u32 *v){
  u32 x;
  int n;

  unsigned char c;
#ifndef getVarint32
  if( ((signed char*)p)[0]>=0 ){


    *v = p[0];
    return 1;
  }
#endif






  x = p[0] & 0x7f;




  if( ((signed char*)p)[1]>=0 ){









    *v = (x<<7) | p[1];
    return 2;
  }
  x = (x<<7) | (p[1] & 0x7f);








  n = 2;



  do{












    x = (x<<7) | ((c = p[n++])&0x7f);












  }while( (c & 0x80)!=0 && n<9 );









  *v = x;
  return n;

























}

/*
** Return the number of bytes that will be needed to store the given
** 64-bit integer.
*/
int sqlite3VarintLen(u64 v){







|
|
|
|
|
>
|


>
>
>
>
>
>
|
|
>
|


|
>
>
>
>
|
>
>
>
>
>
|


|
>
>
>
>
>
>
|
>
>
>
>
>
>
|


|
>
>
>
>
>
>
|
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
>
>
>
>
|
>
>
>
|
>
>
>
>
>
>
>
>
>
>
|










|
|
>
|

<
>
>
|



>
>
>
>
>
>
|
>
>
>
>
|
>
>
>
>
>
>
>
>
>
|
|

|
>
>
>
>
>
>
>
>
|
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702

703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
}

/*
** Read a 64-bit variable-length integer from memory starting at p[0].
** Return the number of bytes read.  The value is stored in *v.
*/
int sqlite3GetVarint(const unsigned char *p, u64 *v){
  u32 a,b,s;

  a = *p;
  // a: p0 (unmasked)
  if (!(a&0x80))
  {
    *v = a;
    return 1;
  }

  p++;
  b = *p;
  // b: p1 (unmasked)
  if (!(b&0x80))
  {
    a &= 0x7f;
    a = a<<7;
    a |= b;
    *v = a;
    return 2;
  }

  p++;
  a = a<<14;
  a |= *p;
  // a: p0<<14 | p2 (unmasked)
  if (!(a&0x80))
  {
    a &= (0x7f<<14)|(0x7f);
    b &= 0x7f;
    b = b<<7;
    a |= b;
    *v = a;
    return 3;
  }

  // CSE1 from below
  a &= (0x7f<<14)|(0x7f);
  p++;
  b = b<<14;
  b |= *p;
  // b: p1<<14 | p3 (unmasked)
  if (!(b&0x80))
  {
    b &= (0x7f<<14)|(0x7f);
    // moved CSE1 up
    // a &= (0x7f<<14)|(0x7f);
    a = a<<7;
    a |= b;
    *v = a;
    return 4;
  }

  // a: p0<<14 | p2 (masked)
  // b: p1<<14 | p3 (unmasked)
  // 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked)
  // moved CSE1 up
  // a &= (0x7f<<14)|(0x7f);
  b &= (0x7f<<14)|(0x7f);
  s = a;
  // s: p0<<14 | p2 (masked)

  p++;
  a = a<<14;
  a |= *p;
  // a: p0<<28 | p2<<14 | p4 (unmasked)
  if (!(a&0x80))
  {
    // we can skip these cause they were (effectively) done above in calc'ing s
    // a &= (0x7f<<28)|(0x7f<<14)|(0x7f);
    // b &= (0x7f<<14)|(0x7f);
    b = b<<7;
    a |= b;
    s = s>>18;
    *v = ((u64)s)<<32 | a;
    return 5;
  }

  // 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked)
  s = s<<7;
  s |= b;
  // s: p0<<21 | p1<<14 | p2<<7 | p3 (masked)

  p++;
  b = b<<14;
  b |= *p;
  // b: p1<<28 | p3<<14 | p5 (unmasked)
  if (!(b&0x80))
  {
    // we can skip this cause it was (effectively) done above in calc'ing s
    // b &= (0x7f<<28)|(0x7f<<14)|(0x7f);
    a &= (0x7f<<14)|(0x7f);
    a = a<<7;
    a |= b;
    s = s>>18;
    *v = ((u64)s)<<32 | a;
    return 6;
  }

  p++;
  a = a<<14;
  a |= *p;
  // a: p2<<28 | p4<<14 | p6 (unmasked)
  if (!(a&0x80))
  {
    a &= (0x7f<<28)|(0x7f<<14)|(0x7f);
    b &= (0x7f<<14)|(0x7f);
    b = b<<7;
    a |= b;
    s = s>>11;
    *v = ((u64)s)<<32 | a;
    return 7;
  }

  // CSE2 from below
  a &= (0x7f<<14)|(0x7f);
  p++;
  b = b<<14;
  b |= *p;
  // b: p3<<28 | p5<<14 | p7 (unmasked)
  if (!(b&0x80))
  {
    b &= (0x7f<<28)|(0x7f<<14)|(0x7f);
    // moved CSE2 up
    // a &= (0x7f<<14)|(0x7f);
    a = a<<7;
    a |= b;
    s = s>>4;
    *v = ((u64)s)<<32 | a;
    return 8;
  }

  p++;
  a = a<<15;
  a |= *p;
  // a: p4<<29 | p6<<15 | p8 (unmasked)

  // moved CSE2 up
  // a &= (0x7f<<29)|(0x7f<<15)|(0xff);
  b &= (0x7f<<14)|(0x7f);
  b = b<<8;
  a |= b;

  s = s<<4;
  b = p[-4];
  b &= 0x7f;
  b = b>>3;
  s |= b;

  *v = ((u64)s)<<32 | a;

  return 9;
}

/*
** Read a 32-bit variable-length integer from memory starting at p[0].
** Return the number of bytes read.  The value is stored in *v.
** A MACRO version, getVarint32, is provided which inlines the 
** single-byte case.  All code should use the MACRO version as 
** this function assumes the single-byte case has already been handled.
*/
int sqlite3GetVarint32(const unsigned char *p, u32 *v){
  u32 a,b;

  a = *p;
  // a: p0 (unmasked)
#ifndef getVarint32

  if (!(a&0x80))
  {
    *v = a;
    return 1;
  }
#endif

  p++;
  b = *p;
  // b: p1 (unmasked)
  if (!(b&0x80))
  {
    a &= 0x7f;
    a = a<<7;
    *v = a | b;
    return 2;
  }

  p++;
  a = a<<14;
  a |= *p;
  // a: p0<<14 | p2 (unmasked)
  if (!(a&0x80))
  {
    a &= (0x7f<<14)|(0x7f);
    b &= 0x7f;
    b = b<<7;
    *v = a | b;
    return 3;
  }

  p++;
  b = b<<14;
  b |= *p;
  // b: p1<<14 | p3 (unmasked)
  if (!(b&0x80))
  {
    b &= (0x7f<<14)|(0x7f);
    a &= (0x7f<<14)|(0x7f);
    a = a<<7;
    *v = a | b;
    return 4;
  }

  p++;
  a = a<<14;
  a |= *p;
  // a: p0<<28 | p2<<14 | p4 (unmasked)
  if (!(a&0x80))
  {
    a &= (0x7f<<28)|(0x7f<<14)|(0x7f);
    b &= (0x7f<<28)|(0x7f<<14)|(0x7f);
    b = b<<7;
    *v = a | b;
    return 5;
  }

  p++;
  b = b<<14;
  b |= *p;
  // b: p1<<28 | p3<<14 | p5 (unmasked)
  if (!(b&0x80))
  {
    b &= (0x7f<<28)|(0x7f<<14)|(0x7f);
    a &= (0x7f<<28)|(0x7f<<14)|(0x7f);
    a = a<<7;
    *v = a | b;
    return 6;
  }

  p++;
  a = a<<14;
  a |= *p;
  // a: p2<<28 | p4<<14 | p6 (unmasked)
  if (!(a&0x80))
  {
    a &= (0x7f<<28)|(0x7f<<14)|(0x7f);
    b &= (0x7f<<28)|(0x7f<<14)|(0x7f);
    b = b<<7;
    *v = a | b;
    return 7;
  }

  p++;
  b = b<<14;
  b |= *p;
  // b: p3<<28 | p5<<14 | p7 (unmasked)
  if (!(b&0x80))
  {
    b &= (0x7f<<28)|(0x7f<<14)|(0x7f);
    a &= (0x7f<<28)|(0x7f<<14)|(0x7f);
    a = a<<7;
    *v = a | b;
    return 8;
  }

  p++;
  a = a<<14;
  a |= *p;
  // a: p4<<28 | p6<<14 | p8 (unmasked)

  a &= (0x7f<<28)|(0x7f<<14)|(0x7f);
  b &= (0x7f<<28)|(0x7f<<14)|(0x7f);
  b = b<<7;
  *v = a | b;
  return 9;
}

/*
** Return the number of bytes that will be needed to store the given
** 64-bit integer.
*/
int sqlite3VarintLen(u64 v){