Quote:
Originally Posted by suzzer99
Can you give an example or two? Are we talking short string traversal or potentially long array/hash manipuation, etc?
Everything. Inefficient algorithms, inefficient db schemas, unreadable code.
I spent 80% of last month refactoring database operations/schema. We were inserting 50k+ records 1 at at time using create! (Rails function). I spent weeks transforming/refactoring this into 1 insert, speeding it up from ~3 minutes to 15 seconds. I did this like 4-5 different places.
During that time, I also encountered a situation where table 1 had a postgres array field referencing the ids of table 2. We were trying to query any record from table 1 which based on the ids of table 2. Despite the array field using a GIN index, this was awfully slow (and not conceptual/readable). I spent a week refactoring this by simply introducing a bridge table. This sped up query time from ~15 seconds to ~2, which is huge considering it was part of a view used by a UI.
I know you might be looking for inefficient algorithms, but here's an example of code I rewrote just last week. The code works, but it's just awful. I renamed some stuff before posting (e.g. MyModule, Operation1) and added comments. Without any context, you probably won't understand how bad and hacky this really is:
Code:
def status(session_id = nil)
sq = session_id ?
MyModule::MyClass.where(id: session_id) :
MyModule::MyClass.where('created_at > ?', Time.zone.today)
sessions = sq.to_a.map do |sess|
evh = MyLibrary::Event.last('MyModule::MyClass', sess.id)
next [sess.id, nil, nil, 0] unless evh
err = sess.last_event_error
op = evh['operation']
# gets ugly here
use_op, color, tt = err ?
[op + ' FAILED', 'RED-row', err] :
[(op != 'Operation1' || evh['end_dt'].blank? ? op :
sess.busy? ? 'Operation2' :
MyModule::Scenario.where(session_id: sess.id).empty? ?
'Operation3' : 'Complete'),
'', nil]
count = ['Operation3', 'Operation4'].include?(op) && MyModule::MyClass2.
where(session_id: sess.id).count
[sess.id, use_op, count, color, tt]
end
sessions
end