Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Dark side of the reflect

10,524 views

Published on

Go Conference 2019 Spring
https://gocon.connpass.com/event/124530/

オリジナルのGopherはRenée Frenchによってデザインされ、CC BY 3.0ライセンスが適用されています。

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Dark side of the reflect

  1. 1. Copyright © DeNA Co.,Ltd. All Rights Reserved. Dark side of the reflect Masaki Hitonishi Common Support Services Dept. DeNA Co., Ltd.
  2. 2. Copyright © DeNA Co.,Ltd. All Rights Reserved. About me  Masaki Hitonishi  Software Engineer ⁃ Golang Enthusiast ⁃ Contributor to Gin the framework ⁃ HTML5 Game Developer  twitter.com/@sairoutine  github.com/sairoutine
  3. 3. Copyright © DeNA Co.,Ltd. All Rights Reserved. DeNA.go will be held!  https://dena.connpass.com/event/128034/  Join registration is acceptable until 5/21 (Tue) !
  4. 4. Copyright © DeNA Co.,Ltd. All Rights Reserved. Today’s topic  Introduction to reflect  Building ORM by using reflect  reflect or code generation?
  5. 5. Copyright © DeNA Co.,Ltd. All Rights Reserved. What is reflect?
  6. 6. Copyright © DeNA Co.,Ltd. All Rights Reserved. golang’s reflect  Treats any types at runtime ⁃ Examines types ⁃ Creates and modifies it’s value  makes us enable to do 2 things ⁃ Finding variables types ⁃ Getting field information
  7. 7. Copyright © DeNA Co.,Ltd. All Rights Reserved. reflect package  Provides the way to use reflect  Defines the important 2 types ⁃ reflect.Type ⁃ reflect.Value
  8. 8. Copyright © DeNA Co.,Ltd. All Rights Reserved. reflect.Type  A Type represents golang’s type  An interface with methods to distinguish among types ⁃ Map? Struct? Slice? Function? ⁃ String? Int? Ptr?
  9. 9. Copyright © DeNA Co.,Ltd. All Rights Reserved. reflect.Value  A value represents golang’s all value  It can hold a value of any type
  10. 10. Copyright © DeNA Co.,Ltd. All Rights Reserved. Reason to use reflect  To manipulate dynamic types and values ⁃ Works with variables at runtime using information that didn’t exist when the program was written.  To get meta information through struct tag
  11. 11. Copyright © DeNA Co.,Ltd. All Rights Reserved. Example of dynamic types and values
  12. 12. Copyright © DeNA Co.,Ltd. All Rights Reserved. Example of struct tag information
  13. 13. Copyright © DeNA Co.,Ltd. All Rights Reserved. Example of use case in the real world  ORM like gorm, xorm, gorp and so on... ⁃ Object Relational Mapper ⁃ Gets and sets the structure the user defined  JSON Encoder/Decoder ⁃ Parse any value to JSON value  etc...
  14. 14. Copyright © DeNA Co.,Ltd. All Rights Reserved. Writing ORM with reflect
  15. 15. Copyright © DeNA Co.,Ltd. All Rights Reserved. DeNA Game dept.  Using golang in some products  Example ⁃ Game server framework ⁃ Development workflow platform
  16. 16. Copyright © DeNA Co.,Ltd. All Rights Reserved. Game server framework  Next generation server framework under development  Golang + GCP  Using Google Cloud Spanner as a backend database  Original ORM “Span” to treat Spanner
  17. 17. Copyright © DeNA Co.,Ltd. All Rights Reserved. Development workflow platform  Named “Oyakata”  Written in golang  Using Noms as a backend database  Original ORM “Norm” to use Noms database
  18. 18. Copyright © DeNA Co.,Ltd. All Rights Reserved. Noms  Decentralized database  https://github.com/attic-labs/noms  No-SQL  With Git like version control system  Commit/Merge/Diff and so on in database layer
  19. 19. Copyright © DeNA Co.,Ltd. All Rights Reserved. Norm  ORM for Noms. ⁃ ORM is a library to convert data between incompatible type systems to do object- oriented programming.  Users can define their own model struct.  Maps their original struct to Noms data.  Create/Read/Update/Delete.
  20. 20. Copyright © DeNA Co.,Ltd. All Rights Reserved. Norm
  21. 21. Copyright © DeNA Co.,Ltd. All Rights Reserved. Norm Internal  Create/Update ⁃ Any struct to any data  Read ⁃ Any data to struct
  22. 22. Copyright © DeNA Co.,Ltd. All Rights Reserved. Norm Internal
  23. 23. Copyright © DeNA Co.,Ltd. All Rights Reserved. Norm Internal
  24. 24. Copyright © DeNA Co.,Ltd. All Rights Reserved. Norm Internal
  25. 25. Copyright © DeNA Co.,Ltd. All Rights Reserved. Norm Internal
  26. 26. Copyright © DeNA Co.,Ltd. All Rights Reserved. Continue to use reflect?
  27. 27. Copyright © DeNA Co.,Ltd. All Rights Reserved. Span  O/R Mapper for Google Spanner.  Users can define their own model struct.  Create/Read/Update/Delete.  Currently, It’s implemented by code generator, not by reflect.
  28. 28. Copyright © DeNA Co.,Ltd. All Rights Reserved. Why stop using reflect?  Difficult to read source code ⁃ The reflect code tends to be complexity.  Performance issue ⁃ Reflect is slow  Now, Span generates CRUD code by parsing definition the users defines.
  29. 29. Copyright © DeNA Co.,Ltd. All Rights Reserved. Pros/Cons  Pros ⁃ Even if struct field is changed, there is no need to regenerate code  Cons ⁃ The library code tends to be complexity ⁃ Reflect is slow  Norm is still using reflect approach, Span has changed to use code generation approach
  30. 30. Copyright © DeNA Co.,Ltd. All Rights Reserved. Conclusions  There are some cases the golang’s reflect is efficient.  However, code with reflect is a little difficult to read.  Consider code generating approach by your use cases.  Compare user comfortable and code maintainability to decide either.

×