Optimized varint routines and tests added. (CVS 1380)

FossilOrigin-Name: d4e0933dc72b66157164610e0b03f339bc535fb9
This commit is contained in:
drh 2004-05-14 16:50:06 +00:00
parent 10617cddf6
commit 6d2fb15475
7 changed files with 169 additions and 71 deletions

View File

@ -1,5 +1,5 @@
C Performance\simprovements\s(CVS\s1379)
D 2004-05-14T15:27:28
C Optimized\svarint\sroutines\sand\stests\sadded.\s(CVS\s1380)
D 2004-05-14T16:50:06
F Makefile.in ab7b0d5118e2da97bac66be8684a1034e3500f5a
F Makefile.linux-gcc b86a99c493a5bfb402d1d9178dcdc4bd4b32f906
F README f1de682fbbd94899d50aca13d387d1b3fd3be2dd
@ -23,7 +23,7 @@ F sqlite.def fc4f5734786fe4743cfe2aa98eb2da4b089edb5f
F sqlite.pc.in 30552343140c53304c2a658c080fbe810cd09ca2
F src/attach.c c315c58cb16fd6e913b3bfa6412aedecb4567fa5
F src/auth.c 5c2f0bea4729c98c2be3b69d6b466fc51448fe79
F src/btree.c 085a020c22aceb08c42004ffdbfc4daad68fd7eb
F src/btree.c 71c689f16d00febbdee5f474b9bc570df404a7fc
F src/btree.h 6f51ad0ffebfba71295fcacdbe86007512200050
F src/btree_rb.c 9d7973e266ee6f9c61ce592f68742ce9cd5b10e5
F src/build.c e93f443a20eab57ffb77ff6244b1e09a1f7d9390
@ -49,19 +49,19 @@ F src/random.c eff68e3f257e05e81eae6c4d50a51eb88beb4ff3
F src/select.c ca99ae4db14a45a436ec51d3e6bd48d44a3efb3c
F src/shell.c 0c4662e13bfbfd3d13b066c5859cc97ad2f95d21
F src/sqlite.h.in 799c5e726296ec7bc20e6407cdf4df0e0bc00c0c
F src/sqliteInt.h 3b593addbd54228a545ec3ea4f7689c261ae5fa1
F src/sqliteInt.h e94edafb9924f22f038c6a8209c29ddd04cd326a
F src/table.c af14284fa36c8d41f6829e3f2819dce07d3e2de2
F src/tclsqlite.c fbf0fac73624ae246551a6c671f1de0235b5faa1
F src/test1.c 12ef76b8aaba4408422f21f269256b630d4dd627
F src/test2.c 6195a1ca2c8d0d2d93644e86da3289b403486872
F src/test3.c 1b3ac2199169fb331685c7bef15ad54721db278b
F src/test3.c 0752af52c02d6b0053809ecf36adbdc81b90a402
F src/test4.c b3fab9aea7a8940a8a7386ce1c7e2157b09bd296
F src/test5.c eb39aac8fed61bd930b92613cd705c145244074a
F src/tokenize.c e7536dd31205d5afb76c1bdc832dea009c7a3847
F src/trigger.c 8df308e09113410bb895e88a2db65b55490268db
F src/update.c 0441f8b64d616ef244583449e66c984e536c6c9b
F src/utf.c fc799748d43fe1982d157b871e3e420a19c85d4f
F src/util.c 58407b133dfe0b21af23e0aa89c058a2b3d8cb0f
F src/util.c f9511ffba78e6cf71a28774c2820d7750b5bacdf
F src/vacuum.c c134702e023db8778e6be59ac0ea7b02315b5476
F src/vdbe.c fff79c08b3063d8d6f7b600fc6896c255c72238c
F src/vdbe.h 94457ca73bae972dc61bca33a4dccc2e6e14e2f8
@ -75,7 +75,7 @@ F test/auth.test 5c4d95cdaf539c0c236e20ce1f71a93e7dde9185
F test/bigfile.test ea904b853ce2d703b16c5ce90e2b54951bc1ae81
F test/bigrow.test 8ab252dba108f12ad64e337b0f2ff31a807ac578
F test/bind.test 56a57043b42c4664ca705f6050e56717a8a6699a
F test/btree.test ed5781db83b6c1de02e62781d44915a9abe3450a
F test/btree.test d865f501a5aa818d138be06a007b9457053ca701
F test/btree2.test aa4a6d05b1ea90b1acaf83ba89039dd302a88635
F test/btree4.test 3797b4305694c7af6828675b0f4b1424b8ca30e4
F test/btree5.test 8e5ff32c02e685d36516c6499add9375fe1377f2
@ -191,7 +191,7 @@ F www/sqlite.tcl 3c83b08cf9f18aa2d69453ff441a36c40e431604
F www/tclsqlite.tcl b9271d44dcf147a93c98f8ecf28c927307abd6da
F www/vdbe.tcl 9b9095d4495f37697fd1935d10e14c6015e80aa1
F www/whentouse.tcl a8335bce47cc2fddb07f19052cb0cb4d9129a8e4
P 45b60de5c7deb83d10ab54759434e32847f0c2ef
R 623a239c3e485e25d3fc8763ec1b417b
P cad47917267d32ab00c8b949151c8bc3c6638479
R 2f283a670b4184db072fa201634f4b5a
U drh
Z c81eb274ca540aa5e441cb23ee2aa0aa
Z a43ba40aa94121177ed5f96de31aa3ea

View File

@ -1 +1 @@
cad47917267d32ab00c8b949151c8bc3c6638479
d4e0933dc72b66157164610e0b03f339bc535fb9

View File

@ -9,7 +9,7 @@
** May you share freely, never taking more than you give.
**
*************************************************************************
** $Id: btree.c,v 1.136 2004/05/14 15:27:28 drh Exp $
** $Id: btree.c,v 1.137 2004/05/14 16:50:06 drh Exp $
**
** This file implements a external (disk-based) database using BTrees.
** For a detailed discussion of BTrees, refer to
@ -334,50 +334,11 @@ static void put4byte(unsigned char *p, u32 v){
}
/*
** Read a variable-length integer. Store the result in *pResult.
** Return the number of bytes in the integer.
** Routines to read and write variable-length integers.
*/
static unsigned int getVarint(unsigned char *p, u64 *pResult){
u64 x = 0;
int n = 0;
unsigned char c;
do{
c = p[n++];
x = (x<<7) | (c & 0x7f);
}while( (c & 0x80)!=0 );
*pResult = x;
return n;
}
static unsigned int getVarint32(unsigned char *p, u32 *pResult){
u32 x = 0;
int n = 0;
unsigned char c;
do{
c = p[n++];
x = (x<<7) | (c & 0x7f);
}while( (c & 0x80)!=0 );
*pResult = x;
return n;
}
/*
** Write a variable length integer with value v into p[]. Return
** the number of bytes written.
*/
static unsigned int putVarint(unsigned char *p, u64 v){
int i, j, n;
u8 buf[10];
n = 0;
do{
buf[n++] = (v & 0x7f) | 0x80;
v >>= 7;
}while( v!=0 );
buf[0] &= 0x7f;
for(i=0, j=n-1; j>=0; j--, i++){
p[i] = buf[j];
}
return n;
}
#define getVarint sqlite3GetVarint
#define getVarint32 sqlite3GetVarint32
#define putVarint sqlite3PutVarint
/*
** Parse a cell header and fill in the CellInfo structure.

View File

@ -11,7 +11,7 @@
*************************************************************************
** Internal interface definitions for SQLite.
**
** @(#) $Id: sqliteInt.h,v 1.232 2004/05/14 11:00:53 danielk1977 Exp $
** @(#) $Id: sqliteInt.h,v 1.233 2004/05/14 16:50:06 drh Exp $
*/
#include "config.h"
#include "sqlite.h"
@ -1289,7 +1289,6 @@ void sqlite3utf16to16le(void *pData, int N);
void sqlite3utf16to16be(void *pData, int N);
int sqlite3PutVarint(unsigned char *, u64);
int sqlite3GetVarint(const unsigned char *, u64 *);
int sqlite3GetVarint32(const unsigned char *, u32 *);
int sqlite3VarintLen(u64 v);
int sqlite3AddRecordType(Vdbe*, Table*);

View File

@ -13,7 +13,7 @@
** is not included in the SQLite library. It is used for automated
** testing of the SQLite library.
**
** $Id: test3.c,v 1.36 2004/05/11 02:10:07 danielk1977 Exp $
** $Id: test3.c,v 1.37 2004/05/14 16:50:06 drh Exp $
*/
#include "sqliteInt.h"
#include "pager.h"
@ -1029,7 +1029,8 @@ static int btree_fetch_data(
if( argc!=3 ){
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" ID AMT \"", 0);
" ID AMT
\"", 0);
return TCL_ERROR;
}
if( Tcl_GetInt(interp, argv[1], (int*)&pCur) ) return TCL_ERROR;
@ -1144,6 +1145,62 @@ static int btree_breakpoint(
return TCL_OK;
}
/*
** usage: varint_test START MULTIPLIER COUNT INCREMENT
**
** This command tests the sqlite3PutVarint() and sqlite3GetVarint()
** routines, both for accuracy and for speed.
**
** An integer is written using PutVarint() and read back with
** GetVarint() and varified to be unchanged. This repeats COUNT
** times. The first integer is START*MULTIPLIER. Each iteration
** increases the integer by INCREMENT.
**
** This command returns nothing if it works. It returns an error message
** if something goes wrong.
*/
static int btree_varint_test(
void *NotUsed,
Tcl_Interp *interp, /* The TCL interpreter that invoked this command */
int argc, /* Number of arguments */
const char **argv /* Text of each argument */
){
u32 start, mult, count, incr;
u64 in, out;
int n1, n2, i;
unsigned char zBuf[100];
if( argc!=5 ){
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" START MULTIPLIER COUNT INCREMENT\"", 0);
return TCL_ERROR;
}
if( Tcl_GetInt(interp, argv[1], (int*)&start) ) return TCL_ERROR;
if( Tcl_GetInt(interp, argv[2], (int*)&mult) ) return TCL_ERROR;
if( Tcl_GetInt(interp, argv[3], (int*)&count) ) return TCL_ERROR;
if( Tcl_GetInt(interp, argv[4], (int*)&incr) ) return TCL_ERROR;
in = start;
in *= mult;
for(i=0; i<count; i++){
n1 = sqlite3PutVarint(zBuf, in);
if( n1>9 || n1<1 ){
Tcl_AppendResult(interp, "PutVarint returned value out of range", 0);
return TCL_ERROR;
}
n2 = sqlite3GetVarint(zBuf, &out);
if( n1!=n2 ){
Tcl_AppendResult(interp, \
"GetVarint returned value different from PutVarint", 0);
return TCL_ERROR;
}
if( in!=out ){
Tcl_AppendResult(interp, \
"Value read is different from value written", 0);
return TCL_ERROR;
}
in += incr;
}
return TCL_OK;
}
/*
** Register commands with the TCL interpreter.
@ -1188,6 +1245,7 @@ int Sqlitetest3_Init(Tcl_Interp *interp){
{ "btree_cursor_list", (Tcl_CmdProc*)btree_cursor_list },
{ "btree_integrity_check", (Tcl_CmdProc*)btree_integrity_check },
{ "btree_breakpoint", (Tcl_CmdProc*)btree_breakpoint },
{ "btree_varint_test", (Tcl_CmdProc*)btree_varint_test },
};
int i;

View File

@ -14,7 +14,7 @@
** This file contains functions for allocating memory, comparing
** strings, and stuff like that.
**
** $Id: util.c,v 1.81 2004/05/14 11:00:53 danielk1977 Exp $
** $Id: util.c,v 1.82 2004/05/14 16:50:06 drh Exp $
*/
#include "sqliteInt.h"
#include <stdarg.h>
@ -1139,26 +1139,92 @@ int sqlite3SafetyCheck(sqlite *db){
return 0;
}
/*
** Write a 64-bit variable-length integer to memory starting at p[0].
** The length of data write will be between 1 and 9 bytes. The number
** of bytes written is returned.
**
** A variable-length integer consists of the lower 7 bits of each byte
** for all bytes that have the 8th bit set and one byte with the 8th
** bit clear.
*/
int sqlite3PutVarint(unsigned char *p, u64 v){
int i = 0;
int i, j, n;
u8 buf[10];
n = 0;
do{
p[i++] = (v & 0x7f) | 0x80;
buf[n++] = (v & 0x7f) | 0x80;
v >>= 7;
}while( v!=0 );
p[i-1] &= 0x7f;
return i;
buf[0] &= 0x7f;
for(i=0, j=n-1; j>=0; j--, i++){
p[i] = buf[j];
}
return n;
}
/*
** 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){
u64 x = p[0] & 0x7f;
int n = 0;
while( (p[n++]&0x80)!=0 ){
x |= ((u64)(p[n]&0x7f))<<(n*7);
u32 x;
u64 x64;
int n;
unsigned char c;
c = p[0];
if( (c & 0x80)==0 ){
*v = c;
return 1;
}
x = c & 0x7f;
c = p[1];
if( (c & 0x80)==0 ){
*v = (x<<7) | c;
return 2;
}
x = (x<<7) | (c&0x7f);
c = p[2];
if( (c & 0x80)==0 ){
*v = (x<<7) | c;
return 3;
}
x = (x<<7) | (c&0x7f);
c = p[3];
if( (c & 0x80)==0 ){
*v = (x<<7) | c;
return 4;
}
x64 = (x<<7) | (c&0x7f);
n = 4;
do{
c = p[n++];
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.
*/
int sqlite3GetVarint32(const unsigned char *p, u32 *v){
int n = 1;
unsigned char c = p[0];
u32 x = c & 0x7f;
while( (c & 0x80)!=0 ){
c = p[n++];
x = (x<<7) | (c & 0x7f);
}
*v = x;
return n;
}
/*
** Return the number of bytes that will be needed to store the given
** 64-bit integer.
*/
int sqlite3VarintLen(u64 v){
int i = 0;
do{
@ -1167,4 +1233,3 @@ int sqlite3VarintLen(u64 v){
}while( v!=0 );
return i;
}

View File

@ -11,12 +11,27 @@
# This file implements regression tests for SQLite library. The
# focus of this script is btree database backend
#
# $Id: btree.test,v 1.23 2004/05/09 11:51:40 drh Exp $
# $Id: btree.test,v 1.24 2004/05/14 16:50:07 drh Exp $
set testdir [file dirname $argv0]
source $testdir/tester.tcl
# Testing of varint reading writing.
#
do_test btree-0.1 {
btree_varint_test 0 0 500 1
} {}
do_test btree-0.2 {
btree_varint_test 1024 1 500 1
} {}
do_test btree-0.3 {
btree_varint_test -1 1 500 1
} {}
do_test btree-0.4 {
btree_varint_test -1 100000 500 1
} {}
# Basic functionality. Open and close a database.
#
do_test btree-1.1 {