Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not work in AOT(IL2CPP) #15

Closed
dzmitry-lahoda opened this issue Mar 5, 2019 · 14 comments
Closed

Does not work in AOT(IL2CPP) #15

dzmitry-lahoda opened this issue Mar 5, 2019 · 14 comments

Comments

@dzmitry-lahoda
Copy link
Contributor

dzmitry-lahoda commented Mar 5, 2019

May be create separate method for structs? Or may be there is general method to work with all types?

UPDATE: moved code into pull

@SergeyTeplyakov
Copy link
Owner

@dzmitry-lahoda to be honest, I'm not 100% sure what is the question:((

@dzmitry-lahoda
Copy link
Contributor Author

TypeLayout generates IL during run time. Is it possible to generate IL during compilation to allow use of TypeLayout in AOT?

I have found that for unmanaged structs I can use unsafe to get layout. I guess it can be supported up to managed structs, but I have not done that.

I propose TypeLayout via Unsafe could be nice addition to ObjectLayoutInspector

@dzmitry-lahoda
Copy link
Contributor Author

dzmitry-lahoda commented Mar 7, 2019

@SergeyTeplyakov, would you accept a patch with StructLayoutInspector similar to TypeLayout (with same output pattern), but working by mechanism as described above? (of course code will be optimized, tested and cleaned)

@SergeyTeplyakov
Copy link
Owner

I propose TypeLayout via Unsafe could be nice addition to ObjectLayoutInspector

I think it would be a nice addition and I will definitely appreciate such kind of change.

@dzmitry-lahoda
Copy link
Contributor Author

I guess I can support classes either, but need find out how to access no blittable classes on the heap

@dzmitry-lahoda
Copy link
Contributor Author

@SergeyTeplyakov would you mind to look into my pull?
It does not supports classes (as I do no know how to add these), partially supports nullables (can fix some time later), and works on Unity IL2CPP ARM 32/64 bit Android/iOS (will retest this again soon).

@SergeyTeplyakov
Copy link
Owner

@dzmitry-lahoda I've added a bunch of comments. None of them are critical, but I would like to address at least some of them. And I'll take a look at other changes because I reviewed the PR yesterday and you've pushed a bunch of changes after that.

@dzmitry-lahoda
Copy link
Contributor Author

thanks. I will fix these. Want to add that nullables totally broken recursion and need double level of it to pass all info (may be wrap - field and property into something) - so it looks not good (but needed to support nullables - test commented out now).

@dzmitry-lahoda
Copy link
Contributor Author

dzmitry-lahoda commented Mar 13, 2019

I do not know now how to differentiate what structure owns padding for all types (inluding not blittable) in portable way. See padding 2 and 4. Such padding are irrelevant for some purpose (my main is serialization), but for showing layout may have some meaning.

Type layout for 'VeryVeryComplexStruct'
 Size: 128 bytes. Paddings: 33 bytes (%25 of empty space)
 |=====================================================|
 |  0-11: StructWithExplicitLayout one (12 bytes)      |
 | |================================|                  |
 | |     0: Byte m_byte1 (1 byte)   |                  |
 | |--------------------------------|                  |
 | |     0: Byte m_byte1_1 (1 byte) |                  |
 | |--------------------------------|                  |
 | |     1: Byte m_byte2 (1 byte)   |                  |
 | |--------------------------------|                  |
 | |   2-3: Int16 m_short (2 bytes) |                  |
 | |--------------------------------|                  |
 | |   4-5: padding (2 bytes)       |                  |
 | |--------------------------------|                  |
 | |   6-9: Int32 m_int (4 bytes)   |                  |
 | |--------------------------------|                  |
 | | 10-11: padding (2 bytes)       |                  |
 | |================================|                  |
 |-----------------------------------------------------|
 | 12-15: padding (4 bytes)                            |
 |-----------------------------------------------------|
 | 16-39: DoubleByteStructByteStruct two (24 bytes)    |
 | |========================================|          |
 | |  0-15: DoubleByteStruct one (16 bytes) |          |

@dzmitry-lahoda
Copy link
Contributor Author

dzmitry-lahoda commented Mar 13, 2019

@dzmitry-lahoda
Copy link
Contributor Author

Recursive is no much slower than flat. So will be on by default always. As then goes to flat as needed. Will simplify code. Will have real tree inside (need to exected FieldLayoutBase to support parent child?). For crossed paddings (as there is no need to be 100% sure where to split - will 2 default padding rulse for x86 or ARM - somthing like to next 4 bytes split (or use Explicit layout if exists)).

                              Method |     Mean |     Error |    StdDev | Gen 0/1k Op | Gen 1/1k Op | Gen 2/1k Op | Allocated Memory/Op |
------------------------------------ |---------:|----------:|----------:|------------:|------------:|------------:|--------------------:|
                    TypeLayoutStruct | 5.031 ms | 0.1232 ms | 0.1152 ms |     46.8750 |     23.4375 |           - |           149.12 KB |
            TypeLayoutPaddingsStruct | 5.008 ms | 0.0681 ms | 0.0569 ms |     46.8750 |     23.4375 |           - |           149.12 KB |
                  UnsafeLayoutStruct | 1.067 ms | 0.0065 ms | 0.0058 ms |    148.4375 |           - |           - |           459.23 KB |
          UnsafeLayoutPaddingsStruct | 1.092 ms | 0.0067 ms | 0.0063 ms |    148.4375 |           - |           - |           459.59 KB |
 UnsafeLayoutPaddingsRecursiveStruct | 1.110 ms | 0.0221 ms | 0.0410 ms |    171.8750 |           - |           - |           530.68 KB |

@dzmitry-lahoda
Copy link
Contributor Author

i am testing UnsafeLayout on devices (common iOSes and Androids with IL2CPP). Will close story than.

@dzmitry-lahoda
Copy link
Contributor Author

#26

SergeyTeplyakov added a commit that referenced this issue Apr 11, 2019
@dzmitry-lahoda
Copy link
Contributor Author

dzmitry-lahoda commented May 10, 2019

Combo of UnsafeLayout and https://github.com/dzmitry-lahoda/NetStack allowed to create ultra fast generic struct serializes. And now seems UnsafeLayout will work for creating ultra fast tagged Union and OverlappedUnion for C#, kind of closed world fast marker interface pattern but 2X faster and closed(closed in runtime, but can have Roslyn analyzer to close in compile time), also as fast as manual coding of all boilerplate (which can be generated in some time in future). For overlapped it could be intresting to have suggestion for best compression in memory or for best performance on CPU.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants