65 Commits
Author SHA1 Message Date
Alexander Tumin 3bd36b7ac9 Make current token kind public and accessible via Lexer.CurrentToken
Updated implementation of #308
2024-01-06 18:17:55 +03:00
Erik DubbelboerandGitHub a209843d8e Fix bytesToStr (#358)
The current implementation of bytesToStr uses an unsafe
reflect.StringHeader value. Change the implementation of this function
to a safe and simpler version.

To explain what could go wrong here is some example code:

  var d []byte
  d = someFunctionThatReturnsBytes()

  s := bytesToStr(d)

  doSomethingWith(s)

When this code gets compiled bytesToStr would get inlined and the
code would be like the following. I have included in comments at which
point things could go wrong:

  var d []byte
  d = someFunctionThatReturnsBytes()

  h := (*reflect.SliceHeader)(unsafe.Pointer(&d))
  shdr := reflect.StringHeader{Data: h.Data, Len: h.Len}

  // At this point in time d and d.Data have nothing referencing them anymore
  // shdr.Data is an uintptr so it will be ignored by the GC.
  // This means d and d.Data can be garbage collected here.
  // Internally strings don't use a uintptr for the data, but since this is
  // just a reflect.StringHeader and not a real string yet that doesn't apply
  // here.
  // This is why https://pkg.go.dev/unsafe#Pointer says:
  //   In general, reflect.SliceHeader and reflect.StringHeader should be
  //   used only as *reflect.SliceHeader and *reflect.StringHeader pointing
  //   at actual slices or strings, never as plain structs.

  s := *(*string)(unsafe.Pointer(&shdr))

  // Only at this point s.Data points to d.Data again and the backing storage
  // of d won't be garbage collected anymore.

  doSomethingWith(s)

The chance of this going wrong is probably so small that nobody ever
noticed it happening, but it is there.
2022-04-04 11:41:36 +03:00
a833663add Fixed lexer in decoding base64-encoded []byte field with forward slash in it. (#328)
Co-authored-by: Maksim Kochkin <maksim.kochkin@magiclab.co>
2021-02-06 21:55:43 +03:00
fca00f44f1 jlexer skipped json value validation added (#309)
* jlexer skipped json value validation added

* jlexer skipping invalid json tests added

Co-authored-by: michael <michael@tutti.ch>
2020-10-07 20:59:05 +03:00
YaroslavPodorvanovandGitHub 45d2d57d45 fix mailru/easyjson#310 (#311)
* fix mailru/easyjson#310

* reset byteValueCloned on lexer consume, after review #310
2020-09-16 21:26:36 +03:00
kirillxandGitHub f0a000e7a8 Previous optimisation in findStringLen has broken unescaping of \\\" sequences (#284)
* tests: don't ignore errors, verify them carefully

* fix unescaping of \\\\\" and such sequences

* tests: add Unmarshal test cases for escaped sequences
2020-04-24 20:26:02 +03:00
Alexandr Mayorskiy d2e87d0b0f fix cafxx-intern PR 2020-04-12 17:59:45 +03:00
Alexandr Mayorskiy 6ea07b37bb Merge branch 'cafxx-intern' of git://github.com/CAFxX/easyjson into CAFxX-cafxx-intern 2020-04-12 17:56:50 +03:00
Kirill Korotaev 33d35f870a unescaping: should process string numbers in unescaped member names mode
There was a small glitch:
numbers as strings where not unescaped in -disable_members_unescape
mode, though this mode was implied to affect field names only.
2020-04-11 20:20:50 +03:00
Kirill Korotaev 9a01c9afdb Add new -disable_members_unescape option to avoid unescaping of member names, ints, ...
Improves performance for about 15% on BenchmarkEJ_Unmarshal_M-8
2020-04-11 19:11:39 +03:00
Kirill Korotaev 3a0ce97e28 slightly simplify unescapeStringToken() and remove wasEscaped flag 2020-04-11 19:08:38 +03:00
Kirill Korotaev 36293b60e3 Preallocate string before unescaping
This reduces number of allocations in BenchmarkEJ_Unmarshal_M-8 test
from 52 down to 46.

Signed-off-by: Kirill Korotaev <kirillx@gmail.com>
2020-04-11 19:08:38 +03:00
Kirill Korotaev efc4b46d03 Optimisations: use bytes.IndexByte() + delay unescaping
There are 2 issues with current implementation:

1. It performs a plain byte to byte loop to find string boundaries and
perform unescaping. Replace this with bytes.IndexByte() implementation.

2. It performs unescaping of string values even when this is not really
needed, e.g. for members which are absent in target data structure.

This patch fixes both issues and results in ~12% faster BenchmarkEJ_Unmarshal_M-8,
plus number of allocations goes down from 128 to 52 (!):

benchmark                                     old MB/s     new MB/s     speedup
BenchmarkEJ_Unmarshal_M-8                     317.99       356.27       1.12x
BenchmarkEJ_Unmarshal_S-8                     142.19       139.77       0.98x

benchmark                                     old allocs     new allocs     delta
BenchmarkEJ_Unmarshal_M-8                     128            52             -59.38%
BenchmarkEJ_Unmarshal_S-8                     3              3              +0.00%

The rest of benchmarks are w/o changes.

NOTE: performance can be improved up to 1.24x if unescaping is not
performed for member names.

Signed-off-by: Kirill Korotaev <kirillx@gmail.com>
2020-04-11 19:08:38 +03:00
Carlo Alberto Ferraris c124e4243f Implement optional string interning
Fixes https://github.com/mailru/easyjson/issues/191
2020-04-08 08:08:39 +09:00
Vasily RomanovandGitHub 94de47d64c Merge pull request #190 from WUMUXIAN/master
changed the unmarshalling behaviour for unmarshalling {"key": []} int…
2019-06-14 15:48:28 +03:00
Kirill Motkov aced9b46ed Rewrite some if-else-if-else chains as a switch 2019-03-12 16:16:26 +03:00
Muxian Wu a402cc944c fixed the test case 2018-09-28 09:14:50 +08:00
Muxian Wu 0aa3c88d86 changed the unmarshalling behaviour for unmarshalling {"key": []} into map[string]interface{}. The value for "key" in the map will be an empty slice instead of a nil slice. 2018-09-27 15:16:26 +08:00
alexej-v dd93bf1256 Fixes after review 2018-07-27 11:03:01 +03:00
alexej-v 30bdb172b2 Fix lexer.errInvalidToken panic 2018-07-26 14:30:18 +03:00
Dmitry DoroginandGitHub c33a78ba6e Update lexer.go 2018-07-17 13:41:31 +03:00
Gregory Oschwald 90d1db1043 Correctly consume the null value 2018-03-23 08:08:36 -07:00
Vasily RomanovandGitHub 61f426003d Merge pull request #163 from oschwald/greg/json-number-null
Make json.Number handling similar to encoding/json
2018-03-23 15:57:09 +03:00
Gregory Oschwald 20f1e341b0 Make json.Number handling similar to encoding/json
This change modified the `json.Number` unmarshaling to handle `null`
values without error, similar to `encoding/json`. These values are
become `json.Number("")`.

This also modifies the json.Number value returned on error to be
`json.Number("")` as that is the zero value.

See https://play.golang.org/p/knZLugaqnni
2018-01-24 13:41:38 -08:00
Levi Gross 5fb2687db0 No need to mark as float
Signed-off-by: Levi Gross <levi@levigross.com>
2017-11-27 20:29:13 -05:00