Thank you for the detailed review . Your points pushed the library forward. Here is the technical breakdown of what shipped in v3.4.0:
Adopted Optimizations
1. Zero-Allocation Serialization (No more strings)
You were absolutely right about IntegerToString() creating temporary MQL strings that pressure the GC. I implemented PutRawInteger and PutRawDouble to write digits directly into the byte buffer.
// Old (Heap Allocation):
PutRaw(IntegerToString(GetInt(idx)), out, pos, cap);
// New v3.4.0 (Zero-Alloc, Direct Buffer Write):
void PutRawInteger(long value, uchar &out[], int &pos, int &cap) {
// ... writes bytes '0'-'9' directly to out[] ...
}
2. Hybrid Long/Double Parsing + Exp10 Table
I adopted your idea of accumulating integers as long (faster ALU ops) and using a lookup table for the fractional part (better FP precision). However, I added safety guards that were missing in your snippet:
// My implementation of your suggestion (with safety):
if (use_long && int_val < 922337203685477580) { // LONG_MAX / 10
int_val = int_val * 10 + (c - '0');
} else {
// Overflow guard: Fallback to double if number > 19 digits
if (use_long) { val = (double)int_val; use_long = false; }
val = val * 10.0 + (c - '0');
}
This gives us the speed of integers for 99.9% of cases, but safely handles massive numbers (e.g. 30-digit bigints) without silent overflow.
Rejected Suggestions (and why)
1. Using c > '9' as delimiter
This weakens RFC 8259 compliance. An input like 123abc would be silently accepted as 123 . I kept the explicit checks:
if (c == '.' || c == 'e' || c == 'E') break; // Strict validation
2. Removing & 0xFF from GetType() It is not redundant. long in MQL5 is signed. If the tape value has the 63rd bit set (large offset), >> 56 performs an arithmetic shift, filling the high bits with 1s. The mask is required for correctness.
3. Hoisting if (i > 0) outside the loop The CPU branch predictor handles this pattern (1 miss per loop) with negligible cost (~15 cycles). It does not justify code duplication.
The RFC 8259 compliance argument is about Robustness Principle (Postel's Law) vs. Strictness for data interchange.
Ref: RFC 8259, Section 6 (Numbers) The grammar is defined as:
number = [ minus ] int [ frac ] [ exp ]
int = zero / ( digit1-9 *DIGIT )
frac = decimal-point 1*DIGIT
The grammar does not allow trailing characters like 'a', 'b', '-', etc. A value like 123abc is not a number. It is a malformed token.
If the parser uses c > '9' as a stop condition, it consumes 123 and leaves abc in the buffer. In a high-speed parser, this behavior is ambiguous:
Inside an array [123abc] : The parser reads 123 , then the next token is abc (invalid). This eventually fails, but the error is "Invalid Token 'abc'" instead of the root cause "Malformed Number '123abc'". This makes debugging harder.
Concatenated JSON (NDJSON): If I send {"val":123}abc , a loose parser might accept the object and ignore the garbage, potentially hiding data corruption issues in the stream.
By enforcing c == '.' || c == 'e' || c == 'E' , we explicitly state: "These are the ONLY valid continuations for a number." Anything else triggers an immediate check against the structural delimiters ( , } ] or whitespace) in the caller loop, or errors out instantly.
It is a design choice: Fail Fast vs. Fail Later. For financial data (prices/volumes), I prioritize Failing Fast on any ambiguity over saving 1 CPU cycle per digit.
We're talking about the parser only in FastAtof, where the length n_len is already known. Therefore, my condition can't create even the slightest problem.
fxsaber # : We're talking about the parser only in FastAtof , where the length n_len is already known. Therefore, my condition can't create even the slightest problem.
You are absolutely right. Since n_len is already determined by the parser before calling FastAtof , the limits are strictly controlled, so the condition you suggested ( c > '9' ) is indeed safe and cannot cause problems.
I applied its optimization in the latest version (v3.4.2). To verify the impact on performance and ensure robustness, I ran a live benchmark test using real market data from the Binance API (OHLCV, Depth, Trades) against `JAson`. The results confirm that the analyzer is stable and incredibly fast.
Para login e uso do site MQL5.com, você deve ativar o uso de cookies.
Ative esta opção no seu navegador, caso contrário você não poderá fazer login.
Você entrou na conta. A autenticação de dois fatores foi desativada. Configure-a novamente e escaneie um novo código QR ou continue com a autenticação de dois fatores desativada.
Thank you for the detailed review . Your points pushed the library forward. Here is the technical breakdown of what shipped in v3.4.0:
Adopted Optimizations
1. Zero-Allocation Serialization (No more strings)
You were absolutely right about IntegerToString() creating temporary MQL strings that pressure the GC. I implemented PutRawInteger and PutRawDouble to write digits directly into the byte buffer.
// Old (Heap Allocation): PutRaw(IntegerToString(GetInt(idx)), out, pos, cap); // New v3.4.0 (Zero-Alloc, Direct Buffer Write): void PutRawInteger(long value, uchar &out[], int &pos, int &cap) { // ... writes bytes '0'-'9' directly to out[] ... }2. Hybrid Long/Double Parsing + Exp10 Table
I adopted your idea of accumulating integers as long (faster ALU ops) and using a lookup table for the fractional part (better FP precision). However, I added safety guards that were missing in your snippet:
// My implementation of your suggestion (with safety): if (use_long && int_val < 922337203685477580) { // LONG_MAX / 10 int_val = int_val * 10 + (c - '0'); } else { // Overflow guard: Fallback to double if number > 19 digits if (use_long) { val = (double)int_val; use_long = false; } val = val * 10.0 + (c - '0'); }This gives us the speed of integers for 99.9% of cases, but safely handles massive numbers (e.g. 30-digit bigints) without silent overflow.
Rejected Suggestions (and why)
1. Using c > '9' as delimiter
This weakens RFC 8259 compliance. An input like 123abc would be silently accepted as 123 . I kept the explicit checks:
2. Removing & 0xFF from GetType()
It is not redundant. long in MQL5 is signed. If the tape value has the 63rd bit set (large offset), >> 56 performs an arithmetic shift, filling the high bits with 1s. The mask is required for correctness.
3. Hoisting if (i > 0) outside the loop
The CPU branch predictor handles this pattern (1 miss per loop) with negligible cost (~15 cycles). It does not justify code duplication.
v3.4.0 is now live. Thanks for the collaboration.
Rejected Suggestions (and why)
1. Using c > '9' as delimiter
This weakens RFC 8259 compliance. An input like 123abc would be silently accepted as 123 . I kept the explicit checks:
The RFC 8259 compliance argument is about Robustness Principle (Postel's Law) vs. Strictness for data interchange.
Ref: RFC 8259, Section 6 (Numbers)
The grammar is defined as:
The grammar does not allow trailing characters like 'a', 'b', '-', etc. A value like 123abc is not a number. It is a malformed token.
If the parser uses c > '9' as a stop condition, it consumes 123 and leaves abc in the buffer. In a high-speed parser, this behavior is ambiguous:
By enforcing c == '.' || c == 'e' || c == 'E' , we explicitly state: "These are the ONLY valid continuations for a number." Anything else triggers an immediate check against the structural delimiters ( , } ] or whitespace) in the caller loop, or errors out instantly.
It is a design choice: Fail Fast vs. Fail Later. For financial data (prices/volumes), I prioritize Failing Fast on any ambiguity over saving 1 CPU cycle per digit.
Hope that clarifies better!
Hope that clarifies better!
We're talking about the parser only in FastAtof , where the length n_len is already known. Therefore, my condition can't create even the slightest problem.
You are absolutely right. Since n_len is already determined by the parser before calling FastAtof , the limits are strictly controlled, so the condition you suggested ( c > '9' ) is indeed safe and cannot cause problems.
I applied its optimization in the latest version (v3.4.2). To verify the impact on performance and ensure robustness, I ran a live benchmark test using real market data from the Binance API (OHLCV, Depth, Trades) against `JAson`. The results confirm that the analyzer is stable and incredibly fast.
Environment: MetaTrader 5 build 5614, Intel Core i7-10750H @ 2.60GHz
100 candles x 12 fields
100 levels (Nested arrays)
100 exchanges (Various items)
Thanks again for insisting on the details. The code is cleaner and faster now.
🔗 Updated repository: GitHub/Forge
the condition you suggested ( c > '9' ) is indeed safe and cannot cause problems.
Interesting result.
Result.
Therefore, double checking can be replaced with single checking.
Result.
It would make sense to create a counter for each condition.
On numbers you do a double pass through the array.
Fórum de negociação, sistemas de negociação automatizados e testes de estratégias de negociação
Bibliotecas: JSON Library for LLMs
fxsaber, 2026.02.19 09:02
A more versatile option.Ideal.