Skip to content

Commit

Permalink
kmgen: [all] optimize array read
Browse files Browse the repository at this point in the history
  • Loading branch information
inkeliz authored Aug 17, 2022
2 parents d133a96 + 0888b4a commit dbf5f9a
Show file tree
Hide file tree
Showing 16 changed files with 399 additions and 527 deletions.
7 changes: 4 additions & 3 deletions benchmark/c/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ float KBenchmarkDecodeSumVec3(uint32_t size) {

Vec3Viewer * path = MonsterDataViewer_Path(data, &_Reader);
while (j < jl) {
sum.X += Vec3Viewer_X(&path[j]);
sum.Y += Vec3Viewer_Y(&path[j]);
sum.Z += Vec3Viewer_Z(&path[j]);
Vec3Viewer * pp = &path[j];
sum.X += Vec3Viewer_X(pp);
sum.Y += Vec3Viewer_Y(pp);
sum.Z += Vec3Viewer_Z(pp);
j++;
}
i++;
Expand Down
13 changes: 13 additions & 0 deletions benchmark/km/game_generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,9 @@ public void Read(MonsterDataViewer viewer, Karmem.Reader reader) {
this._Color = (Color)(viewer.Color());
var __HitboxSlice = viewer.Hitbox();
var __HitboxLen = __HitboxSlice.Length;
if (__HitboxLen > 5) {
__HitboxLen = 5;
}
if (this._Hitbox.Count != __HitboxLen) {
if (__HitboxLen > this._Hitbox.Capacity) {
this._Hitbox.EnsureCapacity(__HitboxLen);
Expand All @@ -394,6 +397,9 @@ public void Read(MonsterDataViewer viewer, Karmem.Reader reader) {
for (var i = 0; i < __HitboxLen; i++) {
this._Hitbox[i] = __HitboxSlice[i];
}
for (var i = __HitboxLen; i < this._Hitbox.Count; i++) {
this._Hitbox[i] = 0;
}
var __StatusSlice = viewer.Status(reader);
var __StatusLen = __StatusSlice.Length;
if (this._Status.Count != __StatusLen) {
Expand All @@ -410,6 +416,9 @@ public void Read(MonsterDataViewer viewer, Karmem.Reader reader) {
}
var __WeaponsSlice = viewer.Weapons();
var __WeaponsLen = __WeaponsSlice.Length;
if (__WeaponsLen > 4) {
__WeaponsLen = 4;
}
if (this._Weapons.Count != __WeaponsLen) {
if (__WeaponsLen > this._Weapons.Capacity) {
this._Weapons.EnsureCapacity(__WeaponsLen);
Expand All @@ -424,6 +433,10 @@ public void Read(MonsterDataViewer viewer, Karmem.Reader reader) {
ref var __WeaponsItem = ref __WeaponsSpan[i];
__WeaponsItem.Read(__WeaponsSlice[i], reader);
}
for (var i = __WeaponsLen; i < this._Weapons.Count; i++) {
ref var __WeaponsItem = ref __WeaponsSpan[i];
__WeaponsItem.Reset();
}
var __PathSlice = viewer.Path(reader);
var __PathLen = __PathSlice.Length;
if (this._Path.Count != __PathLen) {
Expand Down
51 changes: 23 additions & 28 deletions benchmark/km/game_generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,17 +329,18 @@ func (x *MonsterData) Read(viewer *MonsterDataViewer, reader *karmem.Reader) {
__InventoryLen := len(__InventorySlice)
if __InventoryLen > cap(x.Inventory) {
x.Inventory = append(x.Inventory, make([]byte, __InventoryLen-len(x.Inventory))...)
} else if __InventoryLen > len(x.Inventory) {
}
if __InventoryLen > len(x.Inventory) {
x.Inventory = x.Inventory[:__InventoryLen]
}
copy(x.Inventory, __InventorySlice)
for i := __InventoryLen; i < len(x.Inventory); i++ {
x.Inventory[i] = 0
}
x.Inventory = x.Inventory[:__InventoryLen]
x.Color = Color(viewer.Color())
__HitboxSlice := viewer.Hitbox()
__HitboxLen := len(__HitboxSlice)
if __HitboxLen > 5 {
__HitboxLen = 5
}
copy(x.Hitbox[:], __HitboxSlice)
for i := __HitboxLen; i < len(x.Hitbox); i++ {
x.Hitbox[i] = 0
Expand All @@ -348,36 +349,33 @@ func (x *MonsterData) Read(viewer *MonsterDataViewer, reader *karmem.Reader) {
__StatusLen := len(__StatusSlice)
if __StatusLen > cap(x.Status) {
x.Status = append(x.Status, make([]int32, __StatusLen-len(x.Status))...)
} else if __StatusLen > len(x.Status) {
}
if __StatusLen > len(x.Status) {
x.Status = x.Status[:__StatusLen]
}
copy(x.Status, __StatusSlice)
for i := __StatusLen; i < len(x.Status); i++ {
x.Status[i] = 0
}
x.Status = x.Status[:__StatusLen]
__WeaponsSlice := viewer.Weapons()
__WeaponsLen := len(__WeaponsSlice)
for i := range x.Weapons {
if i >= __WeaponsLen {
x.Weapons[i].Reset()
} else {
x.Weapons[i].Read(&__WeaponsSlice[i], reader)
}
if __WeaponsLen > 4 {
__WeaponsLen = 4
}
for i := 0; i < __WeaponsLen; i++ {
x.Weapons[i].Read(&__WeaponsSlice[i], reader)
}
for i := __WeaponsLen; i < len(x.Weapons); i++ {
x.Weapons[i].Reset()
}
__PathSlice := viewer.Path(reader)
__PathLen := len(__PathSlice)
if __PathLen > cap(x.Path) {
x.Path = append(x.Path, make([]Vec3, __PathLen-len(x.Path))...)
} else if __PathLen > len(x.Path) {
}
if __PathLen > len(x.Path) {
x.Path = x.Path[:__PathLen]
}
for i := range x.Path {
if i >= __PathLen {
x.Path[i].Reset()
} else {
x.Path[i].Read(&__PathSlice[i], reader)
}
for i := 0; i < __PathLen; i++ {
x.Path[i].Read(&__PathSlice[i], reader)
}
x.Path = x.Path[:__PathLen]
x.IsAlive = viewer.IsAlive()
Expand Down Expand Up @@ -490,15 +488,12 @@ func (x *Monsters) Read(viewer *MonstersViewer, reader *karmem.Reader) {
__MonstersLen := len(__MonstersSlice)
if __MonstersLen > cap(x.Monsters) {
x.Monsters = append(x.Monsters, make([]Monster, __MonstersLen-len(x.Monsters))...)
} else if __MonstersLen > len(x.Monsters) {
}
if __MonstersLen > len(x.Monsters) {
x.Monsters = x.Monsters[:__MonstersLen]
}
for i := range x.Monsters {
if i >= __MonstersLen {
x.Monsters[i].Reset()
} else {
x.Monsters[i].Read(&__MonstersSlice[i], reader)
}
for i := 0; i < __MonstersLen; i++ {
x.Monsters[i].Read(&__MonstersSlice[i], reader)
}
x.Monsters = x.Monsters[:__MonstersLen]
}
Expand Down
60 changes: 24 additions & 36 deletions benchmark/km/game_generated.odin
Original file line number Diff line number Diff line change
Expand Up @@ -322,22 +322,20 @@ MonsterDataRead :: proc(x: ^MonsterData, viewer: ^MonsterDataViewer, reader: ^ka
if x.Inventory != nil {
(cast(^[3]int)(&x.Inventory))[1] = __InventoryLen
}
for _, i in x.Inventory {
if i >= __InventoryLen {
x.Inventory[i] = 0
} else {
x.Inventory[i] = __InventorySlice[i]
}
for i := 0; i < __InventoryLen; i += 1{
x.Inventory[i] = __InventorySlice[i]
}
x.Color = EnumColor(MonsterDataViewerColor(viewer))
__HitboxSlice := MonsterDataViewerHitbox(viewer)
__HitboxLen := len(__HitboxSlice)
for _, i in x.Hitbox {
if i >= __HitboxLen {
x.Hitbox[i] = 0
} else {
x.Hitbox[i] = __HitboxSlice[i]
}
if (__HitboxLen > 5) {
__HitboxLen = 5
}
for i := 0; i < __HitboxLen; i += 1{
x.Hitbox[i] = __HitboxSlice[i]
}
for i := __HitboxLen; i < len(x.Hitbox); i += 1 {
x.Hitbox[i] = 0
}
__StatusSlice := MonsterDataViewerStatus(viewer, reader)
__StatusLen := len(__StatusSlice)
Expand All @@ -355,21 +353,19 @@ MonsterDataRead :: proc(x: ^MonsterData, viewer: ^MonsterDataViewer, reader: ^ka
if x.Status != nil {
(cast(^[3]int)(&x.Status))[1] = __StatusLen
}
for _, i in x.Status {
if i >= __StatusLen {
x.Status[i] = 0
} else {
x.Status[i] = __StatusSlice[i]
}
for i := 0; i < __StatusLen; i += 1{
x.Status[i] = __StatusSlice[i]
}
__WeaponsSlice := MonsterDataViewerWeapons(viewer)
__WeaponsLen := len(__WeaponsSlice)
for _, i in x.Weapons {
if i >= __WeaponsLen {
WeaponReset(&x.Weapons[i])
} else {
WeaponRead(&x.Weapons[i], &__WeaponsSlice[i], reader)
}
if (__WeaponsLen > 4) {
__WeaponsLen = 4
}
for i := 0; i < __WeaponsLen; i += 1 {
WeaponRead(&x.Weapons[i], &__WeaponsSlice[i], reader)
}
for i := __WeaponsLen; i < len(x.Weapons); i += 1 {
WeaponReset(&x.Weapons[i])
}
__PathSlice := MonsterDataViewerPath(viewer, reader)
__PathLen := len(__PathSlice)
Expand All @@ -387,12 +383,8 @@ MonsterDataRead :: proc(x: ^MonsterData, viewer: ^MonsterDataViewer, reader: ^ka
if x.Path != nil {
(cast(^[3]int)(&x.Path))[1] = __PathLen
}
for _, i in x.Path {
if i >= __PathLen {
Vec3Reset(&x.Path[i])
} else {
Vec3Read(&x.Path[i], &__PathSlice[i], reader)
}
for i := 0; i < __PathLen; i += 1 {
Vec3Read(&x.Path[i], &__PathSlice[i], reader)
}
x.IsAlive = MonsterDataViewerIsAlive(viewer)
}
Expand Down Expand Up @@ -508,12 +500,8 @@ MonstersRead :: proc(x: ^Monsters, viewer: ^MonstersViewer, reader: ^karmem.Read
if x.Monsters != nil {
(cast(^[3]int)(&x.Monsters))[1] = __MonstersLen
}
for _, i in x.Monsters {
if i >= __MonstersLen {
MonsterReset(&x.Monsters[i])
} else {
MonsterRead(&x.Monsters[i], &__MonstersSlice[i], reader)
}
for i := 0; i < __MonstersLen; i += 1 {
MonsterRead(&x.Monsters[i], &__MonstersSlice[i], reader)
}
}

Expand Down
Loading

0 comments on commit dbf5f9a

Please sign in to comment.