Skip to content

Commit d5cd244

Browse files
author
Rishitha Kalicheti
committed
{181155186} stored procedure's cstring performance
Signed-off-by: Rishitha Kalicheti <[email protected]>
1 parent 2649bd2 commit d5cd244

File tree

11 files changed

+342
-7
lines changed

11 files changed

+342
-7
lines changed

lua/lapi.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,7 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
347347
if (!ttisstring(o)) {
348348
/* COMDB2 MODIFICATION */
349349
if (luabb_iscstring(L, idx)) {
350-
const char *s = luabb_tocstring(L, idx);
351-
if (len) *len = strlen(s);
350+
const char *s = luabb_tolcstring(L, idx, len);
352351
return s;
353352
}
354353
lua_lock(L); /* `luaV_tostring' may create a new string */

lua/ltypes.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -571,16 +571,16 @@ const char *luabb_tostring_noerr(Lua L, int idx)
571571
static int luabb_toblob_int(Lua lua, int idx, blob_t *ret)
572572
{
573573
const char *c;
574-
size_t s;
574+
size_t s = 0;
575575
int type = luabb_type(lua, idx);
576576
if (type == DBTYPES_LSTRING) {
577577
c = lua_tostring(lua, idx);
578-
str: s = strlen(c);
579-
ret->data = strdup(c);
578+
str: ret->data = strdup(c);
580579
ret->length = s;
581580
return 0;
582581
} else if (type == DBTYPES_CSTRING) {
583582
c = ((lua_cstring_t*)lua_touserdata(lua, idx))->val;
583+
s = ((lua_cstring_t*)lua_touserdata(lua, idx))->len;
584584
goto str;
585585
} else if (type == DBTYPES_BLOB) {
586586
const lua_blob_t *blob = lua_topointer(lua, idx);
@@ -765,7 +765,8 @@ static int luabb_todatetime_int(lua_State *lua, int idx, datetime_t *ret)
765765
return 0;
766766
case DBTYPES_CSTRING:
767767
str = ((lua_cstring_t*)lua_topointer(lua, idx))->val;
768-
if (str_to_dttz(str, strlen(str), tzname, &dt, precision) != 0)
768+
int len = ((lua_cstring_t*)lua_topointer(lua, idx))->len;
769+
if (str_to_dttz(str, len, tzname, &dt, precision) != 0)
769770
goto err;
770771
break;
771772
case DBTYPES_REAL:
@@ -1394,6 +1395,7 @@ int l_cstring_new(lua_State *lua) {
13941395
lua_cstring_t *s;
13951396
new_lua_t(s, lua_cstring_t, DBTYPES_CSTRING);
13961397
s->val = NULL;
1398+
s->len = 0;
13971399
return 1;
13981400
}
13991401

@@ -2113,6 +2115,7 @@ void luabb_pushcstring(lua_State *lua, const char* cstrval) {
21132115
l_cstring_new(lua);
21142116
lua_cstring_t *s = (lua_cstring_t *) lua_topointer(lua, -1);
21152117
s->val = strdup(cstrval);
2118+
s->len = strlen(cstrval);
21162119
}
21172120

21182121
void luabb_pushcstringlen(lua_State *lua, const char* cstrval, int len)
@@ -2130,6 +2133,7 @@ void luabb_pushcstring_dl(lua_State *lua, const char* cstrval)
21302133
l_cstring_new(lua);
21312134
lua_cstring_t *s = (lua_cstring_t *) lua_topointer(lua, -1);
21322135
s->val = (char *)cstrval;
2136+
s->len = (int)strlen(cstrval);
21332137
}
21342138

21352139
void luabb_pushblob(lua_State *lua, const blob_t *val)

lua/ltypes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ typedef struct {
134134
typedef struct {
135135
DBTYPES_COMMON;
136136
char* val;
137+
int len;
137138
} lua_cstring_t;
138139

139140
typedef struct {

lua/luaglue.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ int luabb_isnumber(struct lua_State *, int idx);
4040
double luabb_tonumber(struct lua_State *, int idx);
4141
int luabb_iscstring(struct lua_State *, int idx);
4242
const char *luabb_tocstring(struct lua_State *, int idx);
43+
const char *luabb_tolcstring(struct lua_State *, int idx, size_t *);
4344
int luabb_isnull(struct lua_State *, int idx);
4445
void luabb_tointeger(struct lua_State *, int idx, long long *val);
4546

lua/luautil.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ HashType luabb_hashinfo(void *udata, double *d, const char **c, size_t *l)
422422
if (c != NULL && l != NULL) {
423423
if (t->dbtype == DBTYPES_CSTRING) {
424424
*c = ((lua_cstring_t *)t)->val;
425-
*l = strlen(*c);
425+
*l = ((lua_cstring_t *)t)->len;
426426
} else {
427427
*c = ((lua_blob_t *)t)->val.data;
428428
*l = ((lua_blob_t *)t)->val.length;
@@ -591,6 +591,16 @@ const char *luabb_tocstring(Lua l, int idx)
591591
return ptr->val;
592592
}
593593

594+
const char *luabb_tolcstring(Lua l, int idx, size_t *len)
595+
{
596+
const TValue *o = index2adr(l, idx);
597+
lua_dbtypes_t *bb = luabb_todbpointer(o);
598+
if (bb->dbtype != DBTYPES_CSTRING) abort();
599+
lua_cstring_t *ptr = (lua_cstring_t *)bb;
600+
if (len != NULL) *len = ptr->len;
601+
return ptr->val;
602+
}
603+
594604
/* out should be appropriately sized */
595605
void luabb_fromhex(uint8_t *out, const uint8_t *in, size_t len)
596606
{

lua/sp.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4508,6 +4508,7 @@ static int db_setnull(Lua lua)
45084508
c = (lua_cstring_t *)t;
45094509
free(c->val);
45104510
c->val = NULL;
4511+
c->len = 0;
45114512
break;
45124513
case DBTYPES_BLOB:
45134514
b = (lua_blob_t *)t;

tests/sp_perf.test/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ifeq ($(TESTSROOTDIR),)
2+
include ../testcase.mk
3+
else
4+
include $(TESTSROOTDIR)/testcase.mk
5+
endif

tests/sp_perf.test/runit

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#!/bin/bash
2+
3+
bash -n "$0" | exit 1
4+
# set -x x
5+
6+
DBNAME=$1
7+
TESTNAME="sp_perf"
8+
output="$TESTNAME.out"
9+
expected="$TESTNAME.exp"
10+
tier="default"
11+
if [ -f $output ] ; then
12+
rm -f $output
13+
fi
14+
15+
if [[ "x${DBNAME}" == "x" ]] ; then
16+
echo "need a DB name"
17+
exit 1
18+
fi
19+
20+
longoremptystringTest() {
21+
22+
23+
sp=$(cat << 'EOF'
24+
CREATE PROCEDURE foo VERSION 'test' {
25+
local function main(x, input)
26+
db:setmaxinstructions(10000000)
27+
local t = {}
28+
local s = nil
29+
if input ~= nil then
30+
s = input
31+
else
32+
s = string.format("%s%s", string.rep('a,', 999999), 'a')
33+
end
34+
if x == 'cstring' then s = db:cast(s, 'cstring') end
35+
local start = db:now()
36+
for str in string.gmatch(s, "([^,]+)") do table.insert(t, str) end
37+
db:column_name("count", 1) db:column_name("time", 2) db:column_name("type", 3)
38+
db:emit({count = #t, time = db:now() - start, type = type(s)})
39+
end
40+
}
41+
EOF
42+
)
43+
${CDB2SQL_EXE} -r 1 ${CDB2_OPTIONS} $DBNAME $tier "$sp"
44+
time ${CDB2SQL_EXE} -r 1 ${CDB2_OPTIONS} $DBNAME $tier "exec procedure foo()"
45+
time ${CDB2SQL_EXE} -r 1 ${CDB2_OPTIONS} $DBNAME $tier "exec procedure foo('cstring')"
46+
time ${CDB2SQL_EXE} -r 1 ${CDB2_OPTIONS} $DBNAME $tier "exec procedure foo('cstring', 'spookytext')"
47+
time ${CDB2SQL_EXE} -r 1 ${CDB2_OPTIONS} $DBNAME $tier "exec procedure foo('cstring', '')"
48+
}
49+
50+
51+
bindparamTest() {
52+
53+
$CDB2SQL_EXE ${CDB2_OPTIONS} $DBNAME $tier - <<EOF
54+
drop table if exists bindt
55+
create table bindt {
56+
schema {
57+
cstring i[32] null=yes
58+
cstring j[32] null=yes
59+
}}\$\$
60+
EOF
61+
62+
sp_bind_pos=$(cat << 'EOF'
63+
CREATE PROCEDURE bind_sp VERSION 'test' {
64+
local function main()
65+
local t, r
66+
t = db:prepare("INSERT INTO bindt(i, j) values(?,?)")
67+
t:bind(1, "1") -- bind by pos
68+
t:bind(2, "")
69+
t:exec()
70+
end
71+
}
72+
EOF
73+
)
74+
75+
sp_bind_name=$(cat << 'EOF'
76+
CREATE PROCEDURE bind_sp_named VERSION 'test' {
77+
local function main()
78+
local t, r, a, b
79+
t = db:prepare("INSERT INTO bindt(i, j) values(@a, @b)")
80+
t:bind("a", "5") -- bind by name
81+
t:bind("b", "6")
82+
t:exec()
83+
end
84+
}
85+
EOF
86+
)
87+
88+
${CDB2SQL_EXE} -r 1 ${CDB2_OPTIONS} $DBNAME $tier "$sp_bind_pos"
89+
${CDB2SQL_EXE} -r 1 ${CDB2_OPTIONS} $DBNAME $tier "exec procedure bind_sp()"
90+
${CDB2SQL_EXE} -r 1 ${CDB2_OPTIONS} $DBNAME $tier "SELECT i, j FROM bindt"
91+
${CDB2SQL_EXE} -r 1 ${CDB2_OPTIONS} $DBNAME $tier "$sp_bind_name"
92+
${CDB2SQL_EXE} -r 1 ${CDB2_OPTIONS} $DBNAME $tier "exec procedure bind_sp_named()"
93+
${CDB2SQL_EXE} -r 1 ${CDB2_OPTIONS} $DBNAME $tier "SELECT i, j FROM bindt"
94+
${CDB2SQL_EXE} -r 1 ${CDB2_OPTIONS} $DBNAME $tier - <<EOF
95+
INSERT INTO bindt (i, j) VALUES ('5', NULL)
96+
INSERT INTO bindt (i, j) VALUES ('', '6')
97+
EOF
98+
99+
}
100+
101+
checkdiff() {
102+
if [ -f "$1" ]; then
103+
if ! diff -w $2 $1 > /dev/null 2>&1 ; then
104+
echo "FAILED: output $1 diff from expected $2"
105+
echo "diff:"
106+
diff -w $2 $1
107+
exit 1
108+
else
109+
echo "PASSED!"
110+
fi
111+
else
112+
echo "ERR: No expected output file ($2) found for runit"
113+
exit 1
114+
fi
115+
116+
}
117+
118+
longoremptystringTest
119+
120+
bindparamTest >> "$output" 2>&1
121+
checkdiff "$output" "$expected"
122+
123+
exit 0
124+
125+

tests/sp_perf.test/sp_perf.exp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[drop table if exists bindt] rc 0
2+
[create table bindt {
3+
schema {
4+
cstring i[32] null=yes
5+
cstring j[32] null=yes
6+
}}] rc 0
7+
(version='test')
8+
(i='1', j='')
9+
(version='test')
10+
(i='1', j='')
11+
(i='5', j='6')
12+
(rows inserted=1)
13+
[INSERT INTO bindt (i, j) VALUES ('5', NULL)] rc 0
14+
(rows inserted=1)
15+
[INSERT INTO bindt (i, j) VALUES ('', '6')] rc 0

tests/tools/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ add_exe(simple_ssl simple_ssl.c)
7575
add_exe(sirace sirace.c)
7676
add_exe(slowreaders slowreaders.c)
7777
add_exe(smartbeats smartbeats.c)
78+
add_exe(sp sp.c)
7879
add_exe(sqlite_clnt sqlite_clnt.c)
7980
add_exe(ssl_multi_certs_one_process ssl_multi_certs_one_process.c)
8081
add_exe(stepper stepper.c stepper_client.c)

0 commit comments

Comments
 (0)