feat: add array/complex-factory#11272
feat: add array/complex-factory#11272udaykakade25 wants to merge 12 commits intostdlib-js:developfrom
array/complex-factory#11272Conversation
array/complex-factoryarray/complex-factory
|
/stdlib update-copyright-years |
Signed-off-by: Uday Kakade <141299403+udaykakade25@users.noreply.github.com>
Signed-off-by: Uday Kakade <141299403+udaykakade25@users.noreply.github.com>
| typeof value._length === 'number' && // eslint-disable-line no-underscore-dangle | ||
| typeof value._buffer === 'object' // eslint-disable-line no-underscore-dangle |
There was a problem hiding this comment.
This alone is too weak a check. There is a reason why we check the constructor name in, e.g., array/complex128.
| // MAIN // | ||
|
|
||
| /** | ||
| * Returns a complex number array constructor. |
There was a problem hiding this comment.
The following is a general comment that is more intended for discussion rather than providing something actionable...
The factory approach does work and it will reduce bundle sizes as the implementation would now be centralized.
That said, the factory approach does suffer from one drawback, which is it doesn't allow for inheritance from a parent constructor (e.g., ComplexArray -> Complex128Array). This lack of inheritance means that brand checks are more expensive (i.e., for many operations, we don't care which type of complex typed array we were given; we just want a complex typed array). If we had a parent class, we could simply check x instanceof ComplexArray (i.e., is this value an instance of the parent class?). With the factory approach, we cannot do that, as each ComplexXXArray is a separate class with no common inheritance.
Furthermore, separate classes are treated as objects having different hidden shapes. This then results in JS optimizing compilers treating input arrays as distinct and thus leading the compiler to mark a function as polymorphic. This can degrade performance. This is also true of built-in arrays, so we are not an outlier here, but the factory approach does introduce a potential performance cliff.
I am not sure that a parent class approach is actually feasible, but I at least wanted to document that there are trade-offs associated with a factory approach.
There was a problem hiding this comment.
Hey Athan,
I have addressed all the ESLint errors and refactored main.js based on your earlier feedback:
- Removed spellcheck.js changes to include
realfandimagf(the change was unnecessary) - Fixed minor issues such as position of Section Header and eslint warnings
isComplexArrayConstructorandisComplexArraycompares against constructor name thus fixing weak checkcomplex-factorynow usesfactory('dtype') similar tofixed-endian-factory`- Forgot to replace
new ComplexArray( tmp )withthis.constructor(we discussed this in the office hours - now fixed
The package is finally ready for the final review.
Regarding discussion:
I see array/fixed-endian-factory and array/little-endian-factory uses Factory approach. While implementing array/float16 polyfill, i had to create all the packges however, with Complex Factory, array/complex32 requires no huge package but simple refactoring to get included in array/complex-factory. This heavily reduces code and bundle size.
With tradeoffs such as performance cliff issue and branch check that you mentioned, i would love to go deep and research on Parent Class Approach. Surely, we can do discussion and get direction on use and possibly implementation of this approach in complex typed array in the next office hours.
Signed-off-by: Uday Kakade <141299403+udaykakade25@users.noreply.github.com>
Signed-off-by: Uday Kakade <141299403+udaykakade25@users.noreply.github.com>
Signed-off-by: Uday Kakade <141299403+udaykakade25@users.noreply.github.com>
Signed-off-by: Uday Kakade <141299403+udaykakade25@users.noreply.github.com>
Coverage ReportNo coverage information available. |
Fix syntax error by adding a semicolon to the example. Signed-off-by: Uday Kakade <141299403+udaykakade25@users.noreply.github.com>
Resolves none.
Description
This pull request:
array/complex-factorypackage.Related Issues
This pull request has the following related issues:
Questions
No.
Other
No.
Checklist
AI Assistance
If you answered "yes" above, how did you use AI assistance?
Disclosure
I used Claude during my R&D by taking references from
array/complex128,array/complex64,array/fix-endian-factory. Later used Claude to automate some of repetitive tasks in docs and benchmark files.@stdlib-js/reviewers