{"action":"opened","issue":{"url":"https://api.github.com/repos/mgravell/protobuf-net/issues/471","repository_url":"https://api.github.com/repos/mgravell/protobuf-net","labels_url":"https://api.github.com/repos/mgravell/protobuf-net/issues/471/labels{/name}","comments_url":"https://api.github.com/repos/mgravell/protobuf-net/issues/471/comments","events_url":"https://api.github.com/repos/mgravell/protobuf-net/issues/471/events","html_url":"https://github.com/mgravell/protobuf-net/issues/471","id":370572436,"node_id":"MDU6SXNzdWUzNzA1NzI0MzY=","number":471,"title":"Efficient handling of structs","user":{"login":"bmesetovic","id":17201234,"node_id":"MDQ6VXNlcjE3MjAxMjM0","avatar_url":"https://avatars0.githubusercontent.com/u/17201234?v=4","gravatar_id":"","url":"https://api.github.com/users/bmesetovic","html_url":"https://github.com/bmesetovic","followers_url":"https://api.github.com/users/bmesetovic/followers","following_url":"https://api.github.com/users/bmesetovic/following{/other_user}","gists_url":"https://api.github.com/users/bmesetovic/gists{/gist_id}","starred_url":"https://api.github.com/users/bmesetovic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bmesetovic/subscriptions","organizations_url":"https://api.github.com/users/bmesetovic/orgs","repos_url":"https://api.github.com/users/bmesetovic/repos","events_url":"https://api.github.com/users/bmesetovic/events{/privacy}","received_events_url":"https://api.github.com/users/bmesetovic/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-10-16T11:47:22Z","updated_at":"2018-10-16T11:47:22Z","closed_at":null,"author_association":"NONE","body":"As part of an effort to reduce GC frequency and impact on our application, we wanted to model several proto serializable DTOs as structs instead of classes. This would reduce the number of objects allocated on the heap and in turn reduce the frequency and duration of GCs.\r\n\r\nA typical example would be Decimal or DateTime, both of which semantically should be structs anyway. We have DTOs that contain 20-30 decimals, so a switch to structs would provide a substantial reduction in object allocations. \r\n\r\nHowever, benchmarks done with BenchmarkDotNet show that the amount of allocated memory actually doubles when we switch to struct decimal DTO. Looking at the library code, it seems that structs are always boxed during deserialization and there is no way to avoid it. I've found several comments online that suggest otherwise, but have not been able to reproduce it.\r\n\r\nHere are the benchmark results:\r\n\r\n\r\nBenchmarkDotNet=v0.11.1, OS=Windows 10.0.16299.611 (1709/FallCreatorsUpdate/Redstone3)\r\nIntel Xeon CPU E5-1620 v2 3.70GHz, 1 CPU, 8 logical and 4 physical cores\r\nFrequency=3613282 Hz, Resolution=276.7567 ns, Timer=TSC\r\n.NET Core SDK=2.1.403\r\n  [Host]     : .NET Core 2.1.5 (CoreCLR 4.6.26919.02, CoreFX 4.6.26919.02), 64bit RyuJIT\r\n  DefaultJob : .NET Core 2.1.5 (CoreCLR 4.6.26919.02, CoreFX 4.6.26919.02), 64bit RyuJIT\r\n\r\n\r\n```\r\n|           Method |     Mean |     Error |    StdDev |  Gen 0 | Allocated |\r\n|----------------- |---------:|----------:|----------:|-------:|----------:|\r\n|     ValueDecimal | 6.173 us | 0.0231 us | 0.0216 us | 0.5188 |   2.68 KB |\r\n| ReferenceDecimal | 7.445 us | 0.0306 us | 0.0287 us | 0.2518 |   1.31 KB |\r\n\r\n```\r\n\r\nI am also attaching the benchmark code ([BoxingBenchmark.zip](https://github.com/mgravell/protobuf-net/files/2482839/BoxingBenchmark.zip)).\r\n\r\nIs there a way to avoid boxing in this scenario?\r\n"}}
{"actor":{"display_login":"bmesetovic"}}