[Tarantool-patches] [PATCH v5 27/52] sql: introduce mem_set_str_*() functions
Mergen Imeev
imeevma at tarantool.org
Thu Apr 15 04:25:54 MSK 2021
Thank you for the review. My answer and two diffs below. First diff for this
patch, second - for similar patch with binary.
On Thu, Apr 15, 2021 at 01:49:46AM +0200, Vladislav Shpilevoy wrote:
> Thanks for working on this!
>
> >>> index ae55e4c29..171cb8946 100644
> >>> --- a/src/box/sql/vdbeaux.c
> >>> +++ b/src/box/sql/vdbeaux.c
> >>> @@ -1333,41 +1331,34 @@ sqlVdbeList(Vdbe * p)
> >>> mem_set_int(pMem, pOp->p3, pOp->p3 < 0);
> >>> pMem++;
> >>>
> >>> - if (sqlVdbeMemClearAndResize(pMem, 256)) {
> >>> - assert(p->db->mallocFailed);
> >>> + char *buf = sqlDbMallocRaw(sql_get(), 256);
> >>
> >> 2. I think you need some kind of mem_set_strlen(), or mem_grow()/mem_reserve(),
> >> or something else to reserve the memory. To extend zMalloc. Otherwise you
> >> can't reuse the memory which might already be in the mem object.
> > Wouldn't mem_copy_*() be enough? In general, allocated by MEM memory should not
> > be accessed for changing (except for MEM_Agg).
>
> Both your current solution and mem_copy suffer from unnecessary copying.
> In the old code the string was created in-place. This is what I am talking
> about.
>
> But nevermind, it is not a perf-critical place as I see. Can be tweaked
> later.
>
> > diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c
> > index 6c08e772d..484c66b29 100644
> > --- a/src/box/sql/vdbeapi.c
> > +++ b/src/box/sql/vdbeapi.c
> > @@ -125,6 +125,12 @@ setResultStrOrError(sql_context * pCtx, /* Function context */
> > void (*xDel) (void *) /* Destructor function */
> > )
> > {
> > + if (xDel == SQL_STATIC)
> > + return mem_set_strl_static(pCtx->pOut, (char *)z, n);
> > + if (xDel == SQL_DYNAMIC)
> > + return mem_set_strl_allocated(pCtx->pOut, (char *)z, n);
> > + if (xDel != SQL_TRANSIENT)
> > + return mem_set_strl_dynamic(pCtx->pOut, (char *)z, n);
>
> I just can't help myself. It makes my eyes bleed - for dynamic we use
> allocated, and for non-transient we use dynamic! I see why, but I would
> want us to hide it as deep inside of mem.c as possible, so as we at
> least could change the names in a not very intrusive manner in the
> future. Or delete it in future entirely.
>
> Please, move this hunk and the one below to a separate functions
> like mem_set_strl(Mem, str, len_hint, destructor). Then it would be in
> one terrible place instead of 2 terrible places.
>
> The same for the bins.
>
Done.
> > if (sqlVdbeMemSetStr(pCtx->pOut, z, n, 1, xDel) != 0)
> > pCtx->is_aborted = true;
> > }
> > @@ -762,7 +768,13 @@ bindText(sql_stmt * pStmt, /* The statement to bind against */
> > if (zData == NULL)
> > return 0;
> > pVar = &p->aVar[i - 1];
> > - if (sqlVdbeMemSetStr(pVar, zData, nData, 1, xDel) != 0)
> > + if (xDel == SQL_STATIC)
> > + mem_set_strl_static(pVar, (char *)zData, nData);
> > + else if (xDel == SQL_DYNAMIC)
> > + mem_set_strl_allocated(pVar, (char *)zData, nData);
> > + else if (xDel != SQL_TRANSIENT)
> > + mem_set_strl_dynamic(pVar, (char *)zData, nData);
> > + else if (sqlVdbeMemSetStr(pVar, zData, nData, 1, xDel) != 0)
> > return -1;
> > return sql_bind_type(p, i, "text");
> > }
Diff for str:
diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h
index f029ee0e5..258bb11e6 100644
--- a/src/box/sql/mem.h
+++ b/src/box/sql/mem.h
@@ -409,6 +409,18 @@ mem_set_strl_allocated(struct Mem *mem, char *value, int len_hint)
mem_set_str_allocated(mem, value, len_hint);
}
+static inline void
+mem_set_strl(struct Mem *mem, char *value, int len_hint,
+ void (*custom_free)(void *))
+{
+ if (custom_free == SQL_STATIC)
+ return mem_set_strl_static(mem, value, len_hint);
+ if (custom_free == SQL_DYNAMIC)
+ return mem_set_strl_allocated(mem, value, len_hint);
+ if (custom_free != SQL_TRANSIENT)
+ return mem_set_strl_dynamic(mem, value, len_hint);
+}
+
/**
* Copy content of MEM from one MEM to another. In case source MEM contains
* string or binary and allocation type is not STATIC, this value is copied to
diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c
index 3cd60864f..ae65a53d8 100644
--- a/src/box/sql/vdbeapi.c
+++ b/src/box/sql/vdbeapi.c
@@ -125,12 +125,8 @@ setResultStrOrError(sql_context * pCtx, /* Function context */
void (*xDel) (void *) /* Destructor function */
)
{
- if (xDel == SQL_STATIC)
- return mem_set_strl_static(pCtx->pOut, (char *)z, n);
- if (xDel == SQL_DYNAMIC)
- return mem_set_strl_allocated(pCtx->pOut, (char *)z, n);
if (xDel != SQL_TRANSIENT)
- return mem_set_strl_dynamic(pCtx->pOut, (char *)z, n);
+ return mem_set_strl(pCtx->pOut, (char *)z, n, xDel);
if (sqlVdbeMemSetStr(pCtx->pOut, z, n, 1, xDel) != 0)
pCtx->is_aborted = true;
}
@@ -768,12 +764,8 @@ bindText(sql_stmt * pStmt, /* The statement to bind against */
if (zData == NULL)
return 0;
pVar = &p->aVar[i - 1];
- if (xDel == SQL_STATIC)
- mem_set_strl_static(pVar, (char *)zData, nData);
- else if (xDel == SQL_DYNAMIC)
- mem_set_strl_allocated(pVar, (char *)zData, nData);
- else if (xDel != SQL_TRANSIENT)
- mem_set_strl_dynamic(pVar, (char *)zData, nData);
+ if (xDel != SQL_TRANSIENT)
+ mem_set_strl(pVar, (char *)zData, nData, xDel);
else if (sqlVdbeMemSetStr(pVar, zData, nData, 1, xDel) != 0)
return -1;
return sql_bind_type(p, i, "text");
Diff for bin:
diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h
index f728ac58a..5b9ac6a96 100644
--- a/src/box/sql/mem.h
+++ b/src/box/sql/mem.h
@@ -468,6 +468,18 @@ mem_set_bin_dynamic(struct Mem *mem, char *value, uint32_t size);
void
mem_set_bin_allocated(struct Mem *mem, char *value, uint32_t size);
+static inline void
+mem_set_binl(struct Mem *mem, char *value, uint32_t size,
+ void (*custom_free)(void *))
+{
+ if (custom_free == SQL_STATIC)
+ return mem_set_bin_static(mem, value, size);
+ if (custom_free == SQL_DYNAMIC)
+ return mem_set_bin_allocated(mem, value, size);
+ if (custom_free != SQL_TRANSIENT)
+ return mem_set_bin_dynamic(mem, value, size);
+}
+
/**
* Copy content of MEM from one MEM to another. In case source MEM contains
* string or binary and allocation type is not STATIC, this value is copied to
diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c
index 1ba2f4260..66908e2ec 100644
--- a/src/box/sql/vdbeapi.c
+++ b/src/box/sql/vdbeapi.c
@@ -158,12 +158,8 @@ sql_result_blob(sql_context * pCtx,
)
{
assert(n >= 0);
- if (xDel == SQL_STATIC)
- mem_set_bin_static(pCtx->pOut, (char *)z, n);
- else if (xDel == SQL_DYNAMIC)
- mem_set_bin_allocated(pCtx->pOut, (char *)z, n);
- else if (xDel != SQL_TRANSIENT)
- mem_set_bin_dynamic(pCtx->pOut, (char *)z, n);
+ if (xDel != SQL_TRANSIENT)
+ mem_set_binl(pCtx->pOut, (char *)z, n, xDel);
else if (sqlVdbeMemSetStr(pCtx->pOut, z, n, 0, xDel) != 0)
pCtx->is_aborted = true;
}
@@ -793,12 +789,8 @@ sql_bind_blob(sql_stmt * pStmt,
if (zData == NULL)
return 0;
struct Mem *var = &p->aVar[i - 1];
- if (xDel == SQL_STATIC)
- mem_set_bin_static(var, (char *)zData, nData);
- else if (xDel == SQL_DYNAMIC)
- mem_set_bin_allocated(var, (char *)zData, nData);
- else if (xDel != SQL_TRANSIENT)
- mem_set_bin_dynamic(var, (char *)zData, nData);
+ if (xDel != SQL_TRANSIENT)
+ mem_set_binl(var, (char *)zData, nData, xDel);
else if (sqlVdbeMemSetStr(var, zData, nData, 0, xDel) != 0)
return -1;
return sql_bind_type(p, i, "varbinary");
More information about the Tarantool-patches
mailing list