Skip to content

Commit

Permalink
perf: avoid unnecessary deepcopy
Browse files Browse the repository at this point in the history
Co-authored-by: robo-dani <77256496+robo-dani@users.noreply.github.com>
  • Loading branch information
NateScarlet and robo-dani committed Jul 4, 2022
1 parent 9dcacbf commit bf45a5b
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 19 deletions.
57 changes: 53 additions & 4 deletions auto_derby/single_mode/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
Type,
)


if TYPE_CHECKING:
from . import go_out

from copy import deepcopy

import cast_unknown as cast
import cv2
Expand Down Expand Up @@ -345,6 +345,58 @@ def __init__(self) -> None:
self.items_last_updated_turn = 0
self.item_history = item.History()

def clone(self) -> Context:
obj = self.__class__()
obj.character = self.character
obj.speed = self.speed
obj.stamina = self.stamina
obj.power = self.power
obj.guts = self.guts
obj.wisdom = self.wisdom
obj.date = self.date
obj.vitality = self.vitality
obj.max_vitality = self.max_vitality
obj.mood = self.mood
obj.conditions = self.conditions.copy()
obj.fan_count = self.fan_count
obj.is_after_winning = self.is_after_winning

obj._extra_turn_count = self._extra_turn_count
obj.target_fan_count = self.target_fan_count

obj.turf = self.turf
obj.dart = self.dart

obj.sprint = self.sprint
obj.mile = self.mile
obj.intermediate = self.intermediate
obj.long = self.long

obj.lead = self.lead
obj.head = self.head
obj.middle = self.middle
obj.last = self.last

obj.scene = self.scene
obj.go_out_options = self.go_out_options
obj.scenario = self.scenario

obj.grade_point = self.grade_point
obj.shop_coin = self.shop_coin

obj.training_history = self.training_history
obj.trainings = self.trainings
obj.training_levels = self.training_levels.copy()

obj.race_turns = self.race_turns
obj.race_history = self.race_history

obj.items = self.items.clone()
obj.items_last_updated_turn = 0
obj.item_history = self.item_history

return obj

def target_grade_point(self) -> int:
if self.date[1:] == (0, 0):
return 0
Expand Down Expand Up @@ -641,9 +693,6 @@ def to_dict(self) -> Dict[Text, Any]:
d["shopCoin"] = self.shop_coin
return d

def clone(self):
return deepcopy(self)

@classmethod
def status_by_name(cls, name: Text) -> Tuple[int, Text]:
for i in cls.ALL_STATUSES:
Expand Down
39 changes: 35 additions & 4 deletions auto_derby/single_mode/item/effect_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from __future__ import annotations

from collections import defaultdict
from copy import deepcopy
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -55,6 +54,11 @@ class BuffList:
def __init__(self, v: Iterable[Buff] = ()) -> None:
self._l: List[Buff] = list(v)

def clone(self) -> BuffList:
obj = self.__class__()
obj._l = self._l.copy()
return obj

def __iter__(self):
yield from self._l

Expand Down Expand Up @@ -133,6 +137,36 @@ def __init__(self) -> None:
self.unknown_effects: Sequence[Effect] = ()
self.known_effects: Sequence[Effect] = ()

def clone(self) -> EffectSummary:
obj = self.__class__()
obj.speed = self.speed
obj.stamina = self.stamina
obj.power = self.power
obj.guts = self.guts
obj.wisdom = self.wisdom
obj.vitality = self.vitality
obj.max_vitality = self.max_vitality
obj.mood = self.mood
obj.condition_add = self.condition_add.copy()
obj.condition_remove = self.condition_remove.copy()
obj.training_levels = self.training_levels.copy()
obj.training_effect_buff = self.training_effect_buff.copy()
for k, v in obj.training_effect_buff.items():
obj.training_effect_buff[k] = v.clone()
obj.training_vitality_debuff = self.training_vitality_debuff.copy()
for k, v in obj.training_vitality_debuff.items():
obj.training_vitality_debuff[k] = v.clone()
obj.training_partner_reassign = self.training_partner_reassign
obj.training_no_failure = self.training_no_failure
obj.race_fan_buff = self.race_fan_buff.clone()
obj.race_reward_buff = self.race_reward_buff.clone()
obj.support_friendship = self.support_friendship
obj.character_friendship = self.character_friendship.copy()
obj.unknown_effects = self.unknown_effects
obj.known_effects = self.known_effects

return obj

def add(self, item: Item, age: int = 0):
for effect in item.effects:
if effect.turn_count < age:
Expand All @@ -150,9 +184,6 @@ def add(self, item: Item, age: int = 0):
effect,
)

def clone(self) -> EffectSummary:
return deepcopy(self)

def apply_to_training(self, ctx: Context, training: Training) -> Training:
"""
return a copy of given training with effect applied.
Expand Down
18 changes: 14 additions & 4 deletions auto_derby/single_mode/item/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from __future__ import annotations

from copy import deepcopy
from typing import TYPE_CHECKING, Any, Dict, Text, Tuple

import numpy as np
Expand Down Expand Up @@ -40,6 +39,20 @@ def __init__(self) -> None:
self.quantity = 0
self.disabled = False

def clone(self) -> Item:
obj = self.__class__()
obj.id = self.id
obj.name = self.name
obj.description = self.description
obj.original_price = self.original_price
obj.max_quantity = self.max_quantity
obj.effect_priority = self.effect_priority
obj.effects = self.effects
obj.price = self.price
obj.quantity = self.quantity
obj.disabled = self.disabled
return obj

def __eq__(self, other: object) -> bool:
return isinstance(other, Item) and self._equal_key() == other._equal_key()

Expand Down Expand Up @@ -83,9 +96,6 @@ def from_dict(cls, d: Dict[Text, Any]):
v.effects = tuple(Effect.from_dict(i) for i in d["effects"])
return v

def clone(self):
return deepcopy(self)

def effect_summary(self) -> EffectSummary:
es = EffectSummary()
es.add(self)
Expand Down
5 changes: 5 additions & 0 deletions auto_derby/single_mode/item/item_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ class ItemList:
def __init__(self) -> None:
self._m: Dict[int, Item] = {}

def clone(self) -> ItemList:
obj = self.__class__()
obj._m = self._m.copy()
return obj

def __contains__(self, value: Item) -> bool:
return self.get(value.id).quantity > 0

Expand Down
28 changes: 25 additions & 3 deletions auto_derby/single_mode/race/race.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# -*- coding=UTF-8 -*-
from __future__ import annotations

from copy import deepcopy
from typing import TYPE_CHECKING, Any, Dict, Iterator, Optional, Set, Text, Tuple


from auto_derby.constants import RunningStyle


Expand Down Expand Up @@ -92,8 +92,30 @@ def __init__(self):

self.reward_buff = 0.0

def clone(self):
return deepcopy(self)
def clone(self) -> Race:
obj = self.__class__()
obj.name = self.name
obj.stadium = self.stadium
obj.permission = self.permission
obj.month = self.month
obj.half = self.half
obj.grade = self.grade
obj.entry_count = self.entry_count
obj.distance = self.distance
obj.min_fan_count = self.min_fan_count

obj.ground = self.ground
obj.track = self.track
obj.turn = self.turn
obj.target_statuses = self.target_statuses
obj.fan_counts = self.fan_counts
obj.characters = self.characters
obj.grade_points = self.grade_points
obj.shop_coins = self.shop_coins

obj.reward_buff = self.reward_buff

return obj

def to_dict(self) -> Dict[Text, Any]:
return {
Expand Down
23 changes: 19 additions & 4 deletions auto_derby/single_mode/training/training.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# pyright: strict
from __future__ import annotations

from copy import deepcopy
from typing import Tuple

from PIL.Image import Image
Expand Down Expand Up @@ -49,6 +48,25 @@ def __init__(self):
self.confirm_position: Tuple[int, int] = (0, 0)
self.partners: Tuple[Partner, ...] = ()

def clone(self) -> Training:
obj = self.__class__()
obj.level = self.level
obj.type = self.type

obj.speed = self.speed
obj.stamina = self.stamina
obj.power = self.power
obj.guts = self.guts
obj.wisdom = self.wisdom
obj.skill = self.skill
obj.vitality = self.vitality
obj._use_estimate_vitality = self._use_estimate_vitality
obj.failure_rate = self.failure_rate
obj.confirm_position = self.confirm_position
obj.partners = self.partners

return obj

def __str__(self):

named_data = (
Expand Down Expand Up @@ -80,9 +98,6 @@ def __str__(self):
+ ">"
)

def clone(self):
return deepcopy(self)

def score(self, ctx: Context) -> float:
return training_score.compute(ctx, self)

Expand Down

0 comments on commit bf45a5b

Please sign in to comment.