Skip to content

Commit

Permalink
HF 30 - Improve private groups performance
Browse files Browse the repository at this point in the history
  • Loading branch information
1aerostorm committed Sep 5, 2024
1 parent e47e553 commit 67201a4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 18 deletions.
5 changes: 2 additions & 3 deletions src/components/pages/Messages.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ class Messages extends React.Component {
searchContacts: null,
notifyErrors: 0,
};
this.preDecoded = {};
this.cachedProfileImages = {};
this.windowFocused = true;
this.newMessages = 0;
Expand Down Expand Up @@ -307,9 +306,9 @@ class Messages extends React.Component {

const updateData = async () => {
const newContacts = contacts.size ?
await normalizeContacts(contacts, accounts, currentUser, this.preDecoded, this.cachedProfileImages) :
await normalizeContacts(contacts, accounts, currentUser, this.cachedProfileImages) :
this.state.contacts
const decoded = await normalizeMessages(messages, accounts, currentUser, prevProps.to, this.preDecoded)
const decoded = await normalizeMessages(messages, accounts, currentUser, prevProps.to)
this.setState({
contacts: newContacts,
messages: decoded,
Expand Down
7 changes: 7 additions & 0 deletions src/redux/FetchDataSaga.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import golos, { api, auth } from 'golos-lib-js'
import tt from 'counterpart'

import g from 'app/redux/GlobalReducer'
import { getSpaceInCache, saveToCache } from 'app/utils/Normalizators'

export function* fetchDataWatches () {
yield fork(watchLocationChange)
Expand Down Expand Up @@ -63,15 +64,18 @@ export function* fetchState(location_change_action) {
const posting = yield select(state => state.user.getIn(['current', 'private_keys', 'posting_private']))

console.time('prof: getContactsAsync')
const conCache = getSpaceInCache(null, 'contacts')
const con = yield call([auth, 'withNodeLogin'], { account, keys: { posting },
call: async (loginData) => {
return await api.getContactsAsync({
...loginData,
owner: account, limit: 100,
cache: Object.keys(conCache),
})
}
})
//alert(JSON.stringify(con))
console.log('procc:' + con._dec_processed)
state.contacts = con.contacts
if (hasErr) return
console.timeEnd('prof: getContactsAsync')
Expand Down Expand Up @@ -106,9 +110,11 @@ export function* fetchState(location_change_action) {
state.the_group = the_group
console.timeEnd('prof: getGroupsAsync')

const space = getSpaceInCache({ group: the_group.name })
console.time('prof: getThreadAsync')
let query = {
group: path,
cache: Object.keys(space),
}
const getThread = async (loginData) => {
query = {...query, ...loginData}
Expand All @@ -123,6 +129,7 @@ export function* fetchState(location_change_action) {
} else {
thRes = yield call(getThread)
}
console.log('proc:' + thRes._dec_processed)
if (the_group && thRes.error) {
the_group.error = thRes.error
}
Expand Down
60 changes: 45 additions & 15 deletions src/utils/Normalizators.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,26 @@ function getProfileImageLazy(account, cachedProfileImages) {
return image;
}

const getCache = () => {
if (!window.preDecoded) window.preDecoded = {}
return window.preDecoded
}

export const getSpaceInCache = (msg, spaceKey = '') => {
const preDecoded = getCache()
const key = spaceKey || (msg.group ? msg.group : '')
if (!preDecoded[key]) preDecoded[key] = {}
const space = preDecoded[key]
return space
}

export const getContactsSpace = (msg) => {
return getSpaceInCache(msg, 'contacts')
}

const cacheKey = (msg) => {
let key = [msg.nonce]
if (msg.group) {
key.push(msg.group)
key.push(msg.receive_date)
key.push(msg.from)
key.push(msg.to)
Expand All @@ -30,25 +46,39 @@ const cacheKey = (msg) => {
return key
}

const saveToCache = (preDecoded, msg) => {
export const saveToCache = (msg, contact = false) => {
if (!msg.message) return false
if (msg.group && msg.decrypt_date !== msg.receive_date) return false
let key = cacheKey(msg)
preDecoded[key] = { message: msg.message }
const space = getSpaceInCache(msg)
const key = cacheKey(msg)
space[key] = { message: msg.message }
if (contact) {
const cont = getContactsSpace(msg)
cont[key] = { message: msg.message }
}
return true
}

const loadFromCache = (preDecoded, msg) => {
let key = cacheKey(msg)
let pd = preDecoded[key];
const loadFromCache = (msg, contact = false) => {
const space = getSpaceInCache(msg)
const key = cacheKey(msg)
const pd = space[key]
if (pd) {
msg.message = pd.message
return true
}
if (contact) {
const cont = getContactsSpace(msg)
const pdc = cont[key]
if (pdc) {
msg.message = pdc.message
return true
}
}
return false
}

export async function normalizeContacts(contacts, accounts, currentUser, preDecoded, cachedProfileImages) {
export async function normalizeContacts(contacts, accounts, currentUser, cachedProfileImages) {
if (!currentUser || !accounts)
return [];

Expand Down Expand Up @@ -87,13 +117,13 @@ export async function normalizeContacts(contacts, accounts, currentUser, preDeco
}
}

if (loadFromCache(preDecoded, msg)) {
if (loadFromCache(msg, true)) {
return true
}
return false;
},
for_each: (msg) => {
saveToCache(preDecoded, msg)
saveToCache(msg, true)
},
on_error: (msg, idx, exception) => {
msg.message = { body: tt_invalid_message, invalid: true, };
Expand All @@ -108,7 +138,7 @@ export async function normalizeContacts(contacts, accounts, currentUser, preDeco
return contactsCopy
}

export async function normalizeMessages(messages, accounts, currentUser, to, preDecoded) {
export async function normalizeMessages(messages, accounts, currentUser, to) {
let isGroup = false
if (to) {
if (to[0] !== '@') isGroup = true
Expand All @@ -130,7 +160,7 @@ export async function normalizeMessages(messages, accounts, currentUser, to, pre
const posting = currentUser.getIn(['private_keys', 'posting_private'])
const privateMemo = currentUser.getIn(['private_keys', 'memo_private']);

console.time('dddm')
console.log('ttt', Date.now())
const decoded = await decodeMsgs({ msgs: messagesCopy,
private_memo: !isGroup && privateMemo,
login: {
Expand All @@ -154,14 +184,14 @@ export async function normalizeMessages(messages, accounts, currentUser, to, pre
}
msg.decrypt_date = null

if (loadFromCache(preDecoded, msg)) {
if (loadFromCache(msg)) {
results.push(msg)
return true
}
return false;
},
for_each: (msg, i) => {
saveToCache(preDecoded, msg)
saveToCache(msg)
},
on_error: (msg, i, err) => {
console.error(err, msg)
Expand All @@ -170,7 +200,7 @@ export async function normalizeMessages(messages, accounts, currentUser, to, pre
begin_idx: messagesCopy.length - 1,
end_idx: -1,
})
console.timeEnd('dddm')
console.log('ttte', Date.now())
return decoded
} catch (ex) {
console.log(ex);
Expand Down

0 comments on commit 67201a4

Please sign in to comment.