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

Rewrite the type system #119

Open
KCreate opened this issue Dec 17, 2016 · 0 comments
Open

Rewrite the type system #119

KCreate opened this issue Dec 17, 2016 · 0 comments

Comments

@KCreate
Copy link
Collaborator

KCreate commented Dec 17, 2016

Currently all types are boxed inside a class called BaseType. This is super inefficient as it allocates a lot of memory for a single Float64, Bool, String etc.

At the beginning the justification of that was the ease of development, but now it's just a burden on performance.

The interpreter should use the native crystal types for calculations, values etc.

The only exceptions will be the TObject, TArray and the TFunction, as they need more information to work.

Here is the (unoptimized) LLVM-IR generated by the crystal compiler to create a single TNumeric value for charly:

define internal %"Charly::TNumeric"* @"*Charly::TNumeric::new<Float64>:Charly::TNumeric"(double %value) #0 {
alloca:
  %_ = alloca %"Charly::TNumeric"*
  br label %entry

entry:                                            ; preds = %alloca
  %malloccall = tail call i8* @malloc(i32 ptrtoint (%"Charly::TNumeric"* getelementptr (%"Charly::TNumeric", %"Charly::TNumeric"* null, i32 1) to i32))
  %0 = bitcast i8* %malloccall to %"Charly::TNumeric"*
  %1 = bitcast %"Charly::TNumeric"* %0 to i8*
  call void @llvm.memset.p0i8.i32(i8* %1, i8 0, i32 ptrtoint (%"Charly::TNumeric"* getelementptr (%"Charly::TNumeric", %"Charly::TNumeric"* null, i32 1) to i32), i32 4, i1 false)
  %2 = getelementptr inbounds %"Charly::TNumeric", %"Charly::TNumeric"* %0, i32 0, i32 0
  store i32 6, i32* %2
  store %"Charly::TNumeric"* %0, %"Charly::TNumeric"** %_
  %3 = load %"Charly::TNumeric"*, %"Charly::TNumeric"** %_
  %4 = call double @"*Charly::TNumeric#initialize<Float64>:Float64"(%"Charly::TNumeric"* %3, double %value)
  %5 = load %"Charly::TNumeric"*, %"Charly::TNumeric"** %_
  ret %"Charly::TNumeric"* %5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant