Expand description
Redis’s double printer, which is Grisu2 by way of fpconv.
Redis 7 stopped writing doubles with %.17g and started calling
fpconv_dtoa, a small Grisu2 implementation it vendors under
deps/fpconv. Every double a client reads comes out of it: a sorted set
score, an INCRBYFLOAT reply, a geo distance, a RESP3 double. So the exact
bytes it produces are part of the protocol, and there are two reasons a
shortest round trip printer from somewhere else does not reproduce them.
The first is presentation. fpconv switches to an exponent for a large or a
small magnitude and Rust’s Display never does, so 1e+30 and
1000000000000000000000000000000 are the same number written two ways and a
client comparing bytes sees two different answers. That part could have been
fixed by reshaping Rust’s digits.
The second is the digits themselves, and it could not. Grisu2 is not always shortest. It finds the shortest representation for about nineteen values in twenty and emits one extra digit for the rest, and which values fall in which set is a property of the algorithm rather than of the number. Rust’s printer is always shortest. So no amount of reformatting closes the gap, and the only way to answer what Redis answers is to run what Redis runs.
This is that port, kept deliberately line for line with the C so that the
next time Redis changes it the diff is readable. The one thing that could
not be carried across literally is unsigned overflow: C leaves it wrapping
and Rust panics on it in a debug build, so every place the original relies
on wrapping is spelled out with a wrapping_ call.
Copyright (c) 2021, Redis Labs Copyright (c) 2013-2019, night-shift <as.smljk at gmail dot com> Copyright (c) 2009, Florian Loitsch < florian.loitsch at inria dot fr > All rights reserved.
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and accompanying documentation covered by this license (the “Software”) to use, reproduce, display, distribute, execute, and transmit the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the Software is furnished to do so, all subject to the following:
The copyright notices in the Software and this entire statement, including the above license grant, this restriction and the following disclaimer, must be included in all copies of the Software, in whole or in part, and all derivative works of the Software, unless such copies or derivative works are solely in the form of machine-executable object code generated by a source language processor.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Constants§
Functions§
- dtoa
- Writes
dintodestexactly as Redis’sfpconv_dtoawould, and answers how many bytes it wrote.