Skip to content

Commit 723f90c

Browse files
authored
make observe cheaper by not constructing a whole observation for which only 2 fields are used (#468)
1 parent f031556 commit 723f90c

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
lines changed

contracts/libraries/Oracle.sol

+12-10
Original file line numberDiff line numberDiff line change
@@ -256,23 +256,25 @@ library Oracle {
256256
(Observation memory beforeOrAt, Observation memory atOrAfter) =
257257
getSurroundingObservations(self, time, target, tick, index, liquidity, cardinality);
258258

259-
Oracle.Observation memory at;
260259
if (target == beforeOrAt.blockTimestamp) {
261260
// we're at the left boundary
262-
at = beforeOrAt;
261+
return (beforeOrAt.tickCumulative, beforeOrAt.liquidityCumulative);
263262
} else if (target == atOrAfter.blockTimestamp) {
264263
// we're at the right boundary
265-
at = atOrAfter;
264+
return (atOrAfter.tickCumulative, atOrAfter.liquidityCumulative);
266265
} else {
267266
// we're in the middle
268-
uint32 delta = atOrAfter.blockTimestamp - beforeOrAt.blockTimestamp;
269-
int24 tickDerived = int24((atOrAfter.tickCumulative - beforeOrAt.tickCumulative) / delta);
270-
uint128 liquidityDerived =
271-
uint128((atOrAfter.liquidityCumulative - beforeOrAt.liquidityCumulative) / delta);
272-
at = transform(beforeOrAt, target, tickDerived, liquidityDerived);
267+
uint32 observationTimeDelta = atOrAfter.blockTimestamp - beforeOrAt.blockTimestamp;
268+
uint32 targetDelta = target - beforeOrAt.blockTimestamp;
269+
return (
270+
beforeOrAt.tickCumulative +
271+
((atOrAfter.tickCumulative - beforeOrAt.tickCumulative) / observationTimeDelta) *
272+
targetDelta,
273+
beforeOrAt.liquidityCumulative +
274+
((atOrAfter.liquidityCumulative - beforeOrAt.liquidityCumulative) / observationTimeDelta) *
275+
targetDelta
276+
);
273277
}
274-
275-
return (at.tickCumulative, at.liquidityCumulative);
276278
}
277279

278280
/// @notice Returns the accumulator values as of each time seconds ago from the given time in the array of `secondsAgos`

test/__snapshots__/Oracle.spec.ts.snap

+9-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ exports[`Oracle #grow gas for growing by 10 slots when index == cardinality - 1
1010

1111
exports[`Oracle #initialize gas 1`] = `70070`;
1212

13-
exports[`Oracle #observe before initialization gas for observe since most recent 1`] = `4215`;
13+
exports[`Oracle #observe before initialization gas for observe since most recent 1`] = `4078`;
1414

1515
exports[`Oracle #observe before initialization gas for single observation at current time 1`] = `2965`;
1616

@@ -39,17 +39,17 @@ Object {
3939
}
4040
`;
4141

42-
exports[`Oracle #observe initialized with 5 observations with starting time of 5 gas all of last 20 seconds 1`] = `146696`;
42+
exports[`Oracle #observe initialized with 5 observations with starting time of 5 gas all of last 20 seconds 1`] = `139585`;
4343

44-
exports[`Oracle #observe initialized with 5 observations with starting time of 5 gas between oldest and oldest + 1 1`] = `11239`;
44+
exports[`Oracle #observe initialized with 5 observations with starting time of 5 gas between oldest and oldest + 1 1`] = `10711`;
4545

4646
exports[`Oracle #observe initialized with 5 observations with starting time of 5 gas latest equal 1`] = `2965`;
4747

4848
exports[`Oracle #observe initialized with 5 observations with starting time of 5 gas latest transform 1`] = `3399`;
4949

50-
exports[`Oracle #observe initialized with 5 observations with starting time of 5 gas middle 1`] = `11414`;
50+
exports[`Oracle #observe initialized with 5 observations with starting time of 5 gas middle 1`] = `10886`;
5151

52-
exports[`Oracle #observe initialized with 5 observations with starting time of 5 gas oldest 1`] = `10575`;
52+
exports[`Oracle #observe initialized with 5 observations with starting time of 5 gas oldest 1`] = `10438`;
5353

5454
exports[`Oracle #observe initialized with 5 observations with starting time of 4294967291 fetch many values 1`] = `
5555
Object {
@@ -74,14 +74,14 @@ Object {
7474
}
7575
`;
7676

77-
exports[`Oracle #observe initialized with 5 observations with starting time of 4294967291 gas all of last 20 seconds 1`] = `146696`;
77+
exports[`Oracle #observe initialized with 5 observations with starting time of 4294967291 gas all of last 20 seconds 1`] = `139585`;
7878

79-
exports[`Oracle #observe initialized with 5 observations with starting time of 4294967291 gas between oldest and oldest + 1 1`] = `11239`;
79+
exports[`Oracle #observe initialized with 5 observations with starting time of 4294967291 gas between oldest and oldest + 1 1`] = `10711`;
8080

8181
exports[`Oracle #observe initialized with 5 observations with starting time of 4294967291 gas latest equal 1`] = `2965`;
8282

8383
exports[`Oracle #observe initialized with 5 observations with starting time of 4294967291 gas latest transform 1`] = `3399`;
8484

85-
exports[`Oracle #observe initialized with 5 observations with starting time of 4294967291 gas middle 1`] = `11414`;
85+
exports[`Oracle #observe initialized with 5 observations with starting time of 4294967291 gas middle 1`] = `10886`;
8686

87-
exports[`Oracle #observe initialized with 5 observations with starting time of 4294967291 gas oldest 1`] = `10575`;
87+
exports[`Oracle #observe initialized with 5 observations with starting time of 4294967291 gas oldest 1`] = `10438`;
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`UniswapV3Factory #createPool gas 1`] = `4510482`;
3+
exports[`UniswapV3Factory #createPool gas 1`] = `4512482`;
44

5-
exports[`UniswapV3Factory factory bytecode size 1`] = `24320`;
5+
exports[`UniswapV3Factory factory bytecode size 1`] = `24330`;
66

7-
exports[`UniswapV3Factory pool bytecode size 1`] = `21897`;
7+
exports[`UniswapV3Factory pool bytecode size 1`] = `21907`;

0 commit comments

Comments
 (0)