Skip to content

Commit 7157679

Browse files
committed
Avoid palloc() when emitting floats and doubles.
Follow-up to #4 / #5
1 parent 8f515f7 commit 7157679

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

generate_test_cases.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ def pg_quote(s)
199199
with_proto('scalars { double_field: 123.456 }') do
200200
test_query('pgpb.test.ExampleMessage:scalars.double_field', ['123.456'])
201201
end
202+
with_proto('scalars { double_field: 0.0000001 }') do
203+
test_query('pgpb.test.ExampleMessage:scalars.double_field', ['1e-07'])
204+
end
202205

203206
with_proto('scalars { bool_field: true }') do
204207
test_query('pgpb.test.ExampleMessage:scalars.bool_field', ['true'])

querying.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -465,14 +465,16 @@ class PrimitiveEmitter : public Emitter {
465465
switch (ty_) {
466466
case T::TYPE_DOUBLE:
467467
{
468-
char *cstr = double_to_shortest_decimal(WFL::DecodeDouble(field.value.as_uint64));
469-
EmitStr(std::move(std::string(cstr)));
468+
char buf[DOUBLE_SHORTEST_DECIMAL_LEN];
469+
double_to_shortest_decimal_buf(WFL::DecodeDouble(field.value.as_uint64), buf);
470+
EmitStr(std::move(std::string(buf)));
470471
}
471472
break;
472473
case T::TYPE_FLOAT:
473474
{
474-
char *cstr = float_to_shortest_decimal(WFL::DecodeFloat(field.value.as_uint32));
475-
EmitStr(std::move(std::string(cstr)));
475+
char buf[FLOAT_SHORTEST_DECIMAL_LEN];
476+
float_to_shortest_decimal_buf(WFL::DecodeFloat(field.value.as_uint32), buf);
477+
EmitStr(std::move(std::string(buf)));
476478
}
477479
break;
478480
case T::TYPE_INT64:

0 commit comments

Comments
 (0)